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
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Validator\Mapping; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Validator\Assert; |
15
|
|
|
use Metadata\ClassMetadata as BaseClassMetadata; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ClassMetadata class. |
19
|
|
|
* |
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ClassMetadata extends BaseClassMetadata |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
public $defaultGroup; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function getClassName() |
33
|
|
|
{ |
34
|
|
|
return $this->name; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $property |
39
|
|
|
* |
40
|
|
|
* @return PropertyMetadata|null |
41
|
|
|
*/ |
42
|
|
|
protected function getPropertyMetadata($property) |
43
|
|
|
{ |
44
|
|
|
return isset($this->propertyMetadata[$property]) ? $this->propertyMetadata[$property] : null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $property |
49
|
|
|
* @param Assert $constraint |
50
|
|
|
* @param string $group |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function addPropertyConstraint($property, Assert $constraint, $group = null) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$propertyMetadata = $this->getPropertyMetadata($property); |
55
|
|
|
if ($propertyMetadata === null) { |
56
|
|
|
$propertyMetadata = new PropertyMetadata($this->getClassName(), $property); |
57
|
|
|
$propertyMetadata->defaultGroup = $this->defaultGroup; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$propertyMetadata->addConstraint($constraint, $group); |
61
|
|
|
|
62
|
|
|
$this->addPropertyMetadata($propertyMetadata); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $property |
67
|
|
|
* @param array $constraints |
68
|
|
|
* @param string $group |
69
|
|
|
*/ |
70
|
|
|
public function addPropertyConstraints($property, array $constraints, $group = null) |
71
|
|
|
{ |
72
|
|
|
foreach ($constraints as $constraint) { |
73
|
|
|
$this->addPropertyConstraint($property, $constraint, $group); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return PropertyMetadata[] |
79
|
|
|
*/ |
80
|
|
|
public function getPropertiesMetadata() |
81
|
|
|
{ |
82
|
|
|
return $this->propertyMetadata; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $method |
87
|
|
|
* |
88
|
|
|
* @return MethodMetadata|null |
89
|
|
|
*/ |
90
|
|
|
protected function getMethodMetadata($method) |
91
|
|
|
{ |
92
|
|
|
return isset($this->methodMetadata[$method]) ? $this->methodMetadata[$method] : null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $method |
97
|
|
|
* @param Assert $constraint |
98
|
|
|
* @param string $group |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function addMethodConstraint($method, Assert $constraint, $group = null) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$methodMetadata = $this->getMethodMetadata($method); |
103
|
|
|
if ($methodMetadata === null) { |
104
|
|
|
$methodMetadata = new MethodMetadata($this->getClassName(), $method); |
105
|
|
|
$methodMetadata->defaultGroup = $this->defaultGroup; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$methodMetadata->addConstraint($constraint, $group); |
109
|
|
|
|
110
|
|
|
$this->addMethodMetadata($methodMetadata); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $method |
115
|
|
|
* @param array $constraints |
116
|
|
|
* @param string $group |
117
|
|
|
*/ |
118
|
|
|
public function addMethodConstraints($method, array $constraints, $group = null) |
119
|
|
|
{ |
120
|
|
|
foreach ($constraints as $constraint) { |
121
|
|
|
$this->addMethodConstraint($method, $constraint, $group); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return MethodMetadata[] |
127
|
|
|
*/ |
128
|
|
|
public function getMethodsMetadata() |
129
|
|
|
{ |
130
|
|
|
return $this->methodMetadata; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Merges the constraints of the given metadata into this object. |
135
|
|
|
* |
136
|
|
|
* @param ClassMetadata $source |
137
|
|
|
*/ |
138
|
|
|
public function mergeConstraints(ClassMetadata $source) |
139
|
|
|
{ |
140
|
|
|
foreach ($source->getPropertiesMetadata() as $property => $propertyMetadata) { |
141
|
|
|
foreach ($propertyMetadata->getConstraints() as $group => $constraints) { |
142
|
|
|
$this->addPropertyConstraints($property, $constraints, $group); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
foreach ($source->getMethodsMetadata() as $method => $methodMetadata) { |
147
|
|
|
foreach ($methodMetadata->getConstraints() as $group => $constraints) { |
148
|
|
|
$this->addMethodConstraints($method, $constraints, $group); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function serialize() |
157
|
|
|
{ |
158
|
|
|
return serialize(array( |
159
|
|
|
$this->name, |
160
|
|
|
$this->methodMetadata, |
161
|
|
|
$this->propertyMetadata, |
162
|
|
|
$this->fileResources, |
163
|
|
|
$this->createdAt, |
164
|
|
|
$this->defaultGroup, |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function unserialize($str) |
172
|
|
|
{ |
173
|
|
|
list( |
174
|
|
|
$this->name, |
175
|
|
|
$this->methodMetadata, |
176
|
|
|
$this->propertyMetadata, |
177
|
|
|
$this->fileResources, |
178
|
|
|
$this->createdAt, |
179
|
|
|
$this->defaultGroup) = unserialize($str); |
180
|
|
|
|
181
|
|
|
$this->reflection = new \ReflectionClass($this->name); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.