1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Metadata; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Method; |
7
|
|
|
use Metadata\MergeableClassMetadata; |
8
|
|
|
use Metadata\MergeableInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Philip Washington Sorst <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ClassMetadata extends MergeableClassMetadata |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
public $restResource; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $namePrefix; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $pathPrefix; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $idField; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $controller; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Method[]|null |
42
|
|
|
*/ |
43
|
|
|
public $methods; |
44
|
|
|
|
45
|
90 |
|
public function __construct($name) |
46
|
|
|
{ |
47
|
90 |
|
parent::__construct($name); |
48
|
|
|
|
49
|
90 |
|
$this->namePrefix = Inflector::tableize($this->reflection->getShortName()); |
50
|
90 |
|
$this->pathPrefix = Inflector::pluralize(strtolower($this->reflection->getShortName())); |
51
|
90 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
14 |
|
public function merge(MergeableInterface $object) |
57
|
|
|
{ |
58
|
14 |
|
assert($object instanceof ClassMetadata); |
59
|
|
|
|
60
|
14 |
|
$this->name = $object->name; |
61
|
14 |
|
$this->reflection = $object->reflection; |
62
|
14 |
|
$this->methodMetadata = array_merge($this->methodMetadata, $object->methodMetadata); |
63
|
14 |
|
$this->propertyMetadata = $this->mergePropertyMetadata($object); |
64
|
14 |
|
$this->fileResources = array_merge($this->fileResources, $object->fileResources); |
65
|
|
|
|
66
|
14 |
|
if ($object->createdAt < $this->createdAt) { |
67
|
|
|
$this->createdAt = $object->createdAt; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var ClassMetadata $object */ |
71
|
14 |
|
$this->restResource = $this->mergeField($this->restResource, $object->restResource); |
72
|
14 |
|
$this->idField = $this->mergeField($this->idField, $object->idField); |
73
|
14 |
|
$this->methods = $this->mergeField($this->methods, $object->methods); |
74
|
14 |
|
$this->namePrefix = $this->mergeField($this->namePrefix, $object->namePrefix); |
75
|
14 |
|
$this->pathPrefix = $this->mergeField($this->pathPrefix, $object->pathPrefix); |
76
|
14 |
|
$this->controller = $this->mergeField($this->controller, $object->controller); |
77
|
14 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param bool $restResource |
81
|
|
|
*/ |
82
|
90 |
|
public function setRestResource($restResource) |
83
|
|
|
{ |
84
|
90 |
|
$this->restResource = $restResource; |
85
|
90 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
66 |
|
public function isRestResource() |
91
|
|
|
{ |
92
|
66 |
|
return $this->restResource; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
12 |
|
public function getNamePrefix() |
99
|
|
|
{ |
100
|
12 |
|
return $this->namePrefix; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $namePrefix |
105
|
|
|
*/ |
106
|
|
|
public function setNamePrefix($namePrefix) |
107
|
|
|
{ |
108
|
|
|
$this->namePrefix = $namePrefix; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
12 |
|
public function getPathPrefix() |
115
|
|
|
{ |
116
|
12 |
|
return $this->pathPrefix; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $pathPrefix |
121
|
|
|
*/ |
122
|
44 |
|
public function setPathPrefix(string $pathPrefix) |
123
|
|
|
{ |
124
|
44 |
|
$this->pathPrefix = $pathPrefix; |
125
|
44 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
12 |
|
public function getController() |
131
|
|
|
{ |
132
|
12 |
|
return $this->controller; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $controller |
137
|
|
|
*/ |
138
|
|
|
public function setController($controller) |
139
|
|
|
{ |
140
|
|
|
$this->controller = $controller; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return Method[]|null |
145
|
|
|
*/ |
146
|
2 |
|
public function getMethods() |
147
|
|
|
{ |
148
|
2 |
|
return $this->methods; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param Method[]|null $methods |
153
|
|
|
*/ |
154
|
90 |
|
public function setMethods($methods) |
155
|
|
|
{ |
156
|
90 |
|
$this->methods = $methods; |
157
|
90 |
|
} |
158
|
|
|
|
159
|
90 |
|
public function getPropertyMetadata(string $property): ?PropertyMetadata |
160
|
|
|
{ |
161
|
90 |
|
if (array_key_exists($property, $this->propertyMetadata)) { |
162
|
90 |
|
return $this->propertyMetadata[$property]; |
163
|
|
|
} |
164
|
|
|
|
165
|
70 |
|
return null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param MergeableInterface $object |
170
|
|
|
* |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
14 |
|
protected function mergePropertyMetadata(MergeableInterface $object): array |
174
|
|
|
{ |
175
|
14 |
|
assert($object instanceof ClassMetadata); |
176
|
|
|
|
177
|
|
|
/** @var ClassMetadata $object */ |
178
|
|
|
/** @var PropertyMetadata[] $mergedMetadata */ |
179
|
14 |
|
$mergedMetadata = $this->propertyMetadata; |
180
|
|
|
|
181
|
14 |
|
foreach ($object->propertyMetadata as $otherMetadata) { |
182
|
|
|
/** @var PropertyMetadata $otherMetadata */ |
183
|
14 |
|
if (array_key_exists($otherMetadata->name, $mergedMetadata)) { |
184
|
12 |
|
$mergedMetadata[$otherMetadata->name] = $mergedMetadata[$otherMetadata->name]->merge($otherMetadata); |
185
|
|
|
} else { |
186
|
14 |
|
$mergedMetadata[$otherMetadata->name] = $otherMetadata; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
14 |
|
return $mergedMetadata; |
191
|
|
|
} |
192
|
|
|
|
193
|
80 |
|
public function getMethod(string $methodName): ?Method |
194
|
|
|
{ |
195
|
80 |
|
if (null === $this->methods) { |
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
|
199
|
80 |
|
foreach ($this->methods as $method) { |
200
|
80 |
|
if ($methodName === $method->name) { |
201
|
80 |
|
return $method; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
22 |
|
return null; |
206
|
|
|
} |
207
|
|
|
|
208
|
64 |
|
public function hasMethod($methodName) |
209
|
|
|
{ |
210
|
64 |
|
return null !== $this->getMethod($methodName); |
211
|
|
|
} |
212
|
|
|
|
213
|
14 |
|
private function mergeField($existing, $toMerge) |
214
|
|
|
{ |
215
|
14 |
|
if (null !== $toMerge) { |
216
|
14 |
|
return $toMerge; |
217
|
|
|
} |
218
|
|
|
|
219
|
14 |
|
return $existing; |
220
|
|
|
} |
221
|
|
|
|
222
|
2 |
|
public function getIdField(string $default = 'id'): string |
223
|
|
|
{ |
224
|
2 |
|
if (null !== $this->idField) { |
225
|
|
|
return $this->idField; |
226
|
|
|
} |
227
|
|
|
|
228
|
2 |
|
return $default; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritdoc} |
233
|
|
|
*/ |
234
|
4 |
|
public function serialize() |
235
|
|
|
{ |
236
|
4 |
|
return serialize( |
237
|
|
|
[ |
238
|
4 |
|
$this->name, |
239
|
4 |
|
$this->methodMetadata, |
240
|
4 |
|
$this->propertyMetadata, |
241
|
4 |
|
$this->fileResources, |
242
|
4 |
|
$this->createdAt, |
243
|
4 |
|
$this->restResource, |
244
|
4 |
|
$this->namePrefix, |
245
|
4 |
|
$this->pathPrefix, |
246
|
4 |
|
$this->idField, |
247
|
4 |
|
$this->controller, |
248
|
4 |
|
$this->methods |
249
|
|
|
] |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
4 |
|
public function unserialize($str) |
257
|
|
|
{ |
258
|
|
|
list( |
259
|
4 |
|
$this->name, |
260
|
4 |
|
$this->methodMetadata, |
261
|
4 |
|
$this->propertyMetadata, |
262
|
4 |
|
$this->fileResources, |
263
|
4 |
|
$this->createdAt, |
264
|
4 |
|
$this->restResource, |
265
|
4 |
|
$this->namePrefix, |
266
|
4 |
|
$this->pathPrefix, |
267
|
4 |
|
$this->idField, |
268
|
4 |
|
$this->controller, |
269
|
4 |
|
$this->methods |
270
|
4 |
|
) = unserialize($str); |
271
|
|
|
|
272
|
4 |
|
$this->reflection = new \ReflectionClass($this->name); |
273
|
4 |
|
} |
274
|
|
|
} |
275
|
|
|
|