1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Validator\Mapping; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Validator\Assert; |
14
|
|
|
use Metadata\PropertyMetadata as BasePropertyMetadata; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* PropertyMetadata class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class PropertyMetadata extends BasePropertyMetadata |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $defaultGroup; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $constraintsByGroup = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getClassName() |
37
|
|
|
{ |
38
|
|
|
return $this->class; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getPropertyName() |
45
|
|
|
{ |
46
|
|
|
return $this->name; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getDefaultGroup() |
53
|
|
|
{ |
54
|
|
|
return $this->defaultGroup; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $group |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
protected function getGroup($group = null) |
63
|
|
|
{ |
64
|
|
|
return $group === null || empty($group) ? $this->defaultGroup : $group; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Assert $constraint |
69
|
|
|
* @param string $group |
70
|
|
|
*/ |
71
|
|
|
public function addConstraint(Assert $constraint, $group = null) |
72
|
|
|
{ |
73
|
|
|
$group = $this->getGroup($group); |
74
|
|
|
if (!isset($this->constraintsByGroup[$group])) { |
75
|
|
|
$this->constraintsByGroup[$group] = array(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->constraintsByGroup[$group][] = $constraint; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $constraints |
83
|
|
|
* @param string $group |
84
|
|
|
*/ |
85
|
|
|
public function addConstraints(array $constraints, $group = null) |
86
|
|
|
{ |
87
|
|
|
foreach ($constraints as $constraint) { |
88
|
|
|
$this->addConstraint($constraint, $group); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getConstraints() |
96
|
|
|
{ |
97
|
|
|
return $this->constraintsByGroup; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $group |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getConstraintsByGroup($group) |
106
|
|
|
{ |
107
|
|
|
return isset($this->constraintsByGroup[$group]) ? $this->constraintsByGroup[$group] : array(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Merges the constraints of the given metadata into this object. |
112
|
|
|
* |
113
|
|
|
* @param PropertyMetadata $source |
114
|
|
|
*/ |
115
|
|
|
public function mergeConstraints(PropertyMetadata $source) |
116
|
|
|
{ |
117
|
|
|
foreach ($source->getConstraints() as $group => $constraints) { |
118
|
|
|
$this->addConstraints($constraints, $group); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function serialize() |
126
|
|
|
{ |
127
|
|
|
return serialize(array( |
128
|
|
|
$this->class, |
129
|
|
|
$this->name, |
130
|
|
|
$this->defaultGroup, |
131
|
|
|
$this->constraintsByGroup, |
132
|
|
|
)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function unserialize($str) |
139
|
|
|
{ |
140
|
|
|
list( |
141
|
|
|
$this->class, |
142
|
|
|
$this->name, |
143
|
|
|
$this->defaultGroup, |
144
|
|
|
$this->constraintsByGroup) = unserialize($str); |
145
|
|
|
|
146
|
|
|
$this->reflection = new \ReflectionProperty($this->class, $this->name); |
147
|
|
|
$this->reflection->setAccessible(true); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|