1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Metadata; |
4
|
|
|
|
5
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Method; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Postable; |
7
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Puttable; |
8
|
|
|
use Metadata\MergeableInterface; |
9
|
|
|
use Metadata\PropertyMetadata as BasePropertyMetadata; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Philip Washington Sorst <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class PropertyMetadata extends BasePropertyMetadata implements MergeableInterface |
15
|
|
|
{ |
16
|
90 |
|
public function __construct($class, $name) |
17
|
|
|
{ |
18
|
|
|
try { |
19
|
90 |
|
parent::__construct($class, $name); |
20
|
32 |
|
} catch (\ReflectionException $e) { |
21
|
|
|
/* Ignore missing property definition as they might just be overridden or virtual and therefore only exist |
22
|
|
|
in the parent class. They will be accessible after merging. */ |
23
|
|
|
} |
24
|
90 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
private $type; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $excluded; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Puttable|null |
38
|
|
|
*/ |
39
|
|
|
private $puttable; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Postable|null |
43
|
|
|
*/ |
44
|
|
|
private $postable; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private $includable; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string[]|null |
53
|
|
|
*/ |
54
|
|
|
private $includablePaths; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
private $subResource; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Method[]|null |
63
|
|
|
*/ |
64
|
|
|
private $methods; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
private $association; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var bool |
73
|
|
|
*/ |
74
|
|
|
private $collection; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var bool |
78
|
|
|
*/ |
79
|
|
|
private $virtual; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string|null |
83
|
|
|
*/ |
84
|
|
|
private $subResourcePath; |
85
|
|
|
|
86
|
12 |
|
public function isPuttable(): bool |
87
|
|
|
{ |
88
|
12 |
|
return null !== $this->puttable; |
89
|
|
|
} |
90
|
|
|
|
91
|
86 |
|
public function setPuttable(?Puttable $puttable) |
92
|
|
|
{ |
93
|
86 |
|
$this->puttable = $puttable; |
94
|
86 |
|
} |
95
|
|
|
|
96
|
12 |
|
public function getPuttable(): ?Puttable |
97
|
|
|
{ |
98
|
12 |
|
return $this->puttable; |
99
|
|
|
} |
100
|
|
|
|
101
|
18 |
|
public function isPostable(): bool |
102
|
|
|
{ |
103
|
18 |
|
return null !== $this->postable; |
104
|
|
|
} |
105
|
|
|
|
106
|
86 |
|
public function setPostable(?Postable $postable) |
107
|
|
|
{ |
108
|
86 |
|
$this->postable = $postable; |
109
|
86 |
|
} |
110
|
|
|
|
111
|
18 |
|
public function getPostable(): ?Postable |
112
|
|
|
{ |
113
|
18 |
|
return $this->postable; |
114
|
|
|
} |
115
|
|
|
|
116
|
66 |
|
public function isIncludable(): bool |
117
|
|
|
{ |
118
|
66 |
|
return $this->getBool($this->includable, false); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function isVirtual(): bool |
122
|
|
|
{ |
123
|
|
|
return $this->getBool($this->virtual, false); |
124
|
|
|
} |
125
|
|
|
|
126
|
30 |
|
public function setVirtual(bool $virtual) |
127
|
|
|
{ |
128
|
30 |
|
$this->virtual = $virtual; |
129
|
30 |
|
} |
130
|
|
|
|
131
|
78 |
|
public function setIncludable(bool $includable) |
132
|
|
|
{ |
133
|
78 |
|
$this->includable = $includable; |
134
|
78 |
|
} |
135
|
|
|
|
136
|
12 |
|
public function isSubResource(): bool |
137
|
|
|
{ |
138
|
12 |
|
return $this->getBool($this->subResource, false); |
139
|
|
|
} |
140
|
|
|
|
141
|
74 |
|
public function setSubResource(bool $subResource) |
142
|
|
|
{ |
143
|
74 |
|
$this->subResource = $subResource; |
144
|
74 |
|
} |
145
|
|
|
|
146
|
10 |
|
public function getSubResourcePath(): ?string |
147
|
|
|
{ |
148
|
10 |
|
return $this->subResourcePath; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function setSubResourcePath(string $subResourcePath) |
152
|
|
|
{ |
153
|
|
|
$this->subResourcePath = $subResourcePath; |
154
|
|
|
} |
155
|
|
|
|
156
|
64 |
|
public function isExcluded(): bool |
157
|
|
|
{ |
158
|
64 |
|
return $this->getBool($this->excluded, false); |
159
|
|
|
} |
160
|
|
|
|
161
|
68 |
|
public function setExcluded(bool $excluded) |
162
|
|
|
{ |
163
|
68 |
|
$this->excluded = $excluded; |
164
|
68 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return null|string[] |
168
|
|
|
*/ |
169
|
50 |
|
public function getIncludablePaths(): ?array |
170
|
|
|
{ |
171
|
50 |
|
return $this->includablePaths; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param null|string[] $includablePaths |
176
|
|
|
*/ |
177
|
78 |
|
public function setIncludablePaths(?array $includablePaths) |
178
|
|
|
{ |
179
|
78 |
|
$this->includablePaths = $includablePaths; |
180
|
78 |
|
} |
181
|
|
|
|
182
|
64 |
|
public function isAssociation(): bool |
183
|
|
|
{ |
184
|
64 |
|
return $this->getBool($this->association, false); |
185
|
|
|
} |
186
|
|
|
|
187
|
74 |
|
public function setAssociation(bool $association) |
188
|
|
|
{ |
189
|
74 |
|
$this->association = $association; |
190
|
74 |
|
} |
191
|
|
|
|
192
|
34 |
|
public function isCollection(): bool |
193
|
|
|
{ |
194
|
34 |
|
return $this->getBool($this->collection, false); |
195
|
|
|
} |
196
|
|
|
|
197
|
74 |
|
public function setCollection(bool $collection) |
198
|
|
|
{ |
199
|
74 |
|
$this->collection = $collection; |
200
|
74 |
|
} |
201
|
|
|
|
202
|
66 |
|
public function getType(): ?string |
203
|
|
|
{ |
204
|
66 |
|
return $this->type; |
205
|
|
|
} |
206
|
|
|
|
207
|
90 |
|
public function setType(?string $type) |
208
|
|
|
{ |
209
|
90 |
|
$this->type = $type; |
210
|
90 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param Method[] $methods |
214
|
|
|
*/ |
215
|
74 |
|
public function setMethods(array $methods) |
216
|
|
|
{ |
217
|
74 |
|
$this->methods = $methods; |
218
|
74 |
|
} |
219
|
|
|
|
220
|
12 |
|
public function merge(MergeableInterface $other) |
221
|
|
|
{ |
222
|
12 |
|
if (!$other instanceof PropertyMetadata) { |
223
|
|
|
throw new \InvalidArgumentException('$object must be an instance of PropertyMetadata.'); |
224
|
|
|
} |
225
|
|
|
|
226
|
12 |
|
$this->reflection = $this->mergeField($other->reflection, $this->reflection); |
227
|
12 |
|
$this->type = $this->mergeField($other->type, $this->type); |
228
|
12 |
|
$this->puttable = $this->mergeField($other->puttable, $this->puttable); |
229
|
12 |
|
$this->postable = $this->mergeField($other->postable, $this->postable); |
230
|
12 |
|
$this->excluded = $this->mergeField($other->excluded, $this->excluded); |
231
|
12 |
|
$this->includable = $this->mergeField($other->includable, $this->includable); |
232
|
12 |
|
$this->subResource = $this->mergeField($other->subResource, $this->subResource); |
233
|
12 |
|
$this->includablePaths = $this->mergeField($other->includablePaths, $this->includablePaths); |
234
|
12 |
|
$this->association = $this->mergeField($other->association, $this->association); |
235
|
12 |
|
$this->collection = $this->mergeField($other->collection, $this->collection); |
236
|
12 |
|
$this->subResourcePath = $this->mergeField($other->subResourcePath, $this->subResourcePath); |
237
|
12 |
|
$this->methods = $this->mergeField($other->methods, $this->methods); |
238
|
12 |
|
$this->virtual = $this->mergeField($other->virtual, $this->virtual); |
239
|
|
|
|
240
|
12 |
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
68 |
|
protected function getBool(?bool $value, bool $default) |
244
|
|
|
{ |
245
|
68 |
|
if (null === $value) { |
246
|
66 |
|
return $default; |
247
|
|
|
} |
248
|
|
|
|
249
|
58 |
|
return $value; |
250
|
|
|
} |
251
|
|
|
|
252
|
12 |
|
private function mergeField($thisValue, $otherValue) |
253
|
|
|
{ |
254
|
12 |
|
if (null !== $thisValue) { |
255
|
12 |
|
return $thisValue; |
256
|
|
|
} |
257
|
|
|
|
258
|
12 |
|
return $otherValue; |
259
|
|
|
} |
260
|
|
|
|
261
|
40 |
|
public function getMethod(string $methodName): ?Method |
262
|
|
|
{ |
263
|
40 |
|
if (null === $this->methods) { |
264
|
|
|
return null; |
265
|
|
|
} |
266
|
|
|
|
267
|
40 |
|
foreach ($this->methods as $method) { |
268
|
40 |
|
if ($methodName === $method->name) { |
269
|
40 |
|
return $method; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
10 |
|
return null; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* {@inheritdoc} |
278
|
|
|
*/ |
279
|
4 |
|
public function serialize() |
280
|
|
|
{ |
281
|
4 |
|
return serialize( |
282
|
|
|
[ |
283
|
4 |
|
$this->class, |
284
|
4 |
|
$this->name, |
285
|
4 |
|
$this->type, |
286
|
4 |
|
$this->excluded, |
287
|
4 |
|
$this->puttable, |
288
|
4 |
|
$this->postable, |
289
|
4 |
|
$this->includable, |
290
|
4 |
|
$this->includablePaths, |
291
|
4 |
|
$this->subResource, |
292
|
4 |
|
$this->methods, |
293
|
4 |
|
$this->association, |
294
|
4 |
|
$this->collection, |
295
|
4 |
|
$this->virtual, |
296
|
4 |
|
$this->subResourcePath, |
297
|
|
|
] |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritdoc} |
303
|
|
|
*/ |
304
|
4 |
|
public function unserialize($str) |
305
|
|
|
{ |
306
|
|
|
list( |
307
|
4 |
|
$this->class, |
308
|
4 |
|
$this->name, |
309
|
4 |
|
$this->type, |
310
|
4 |
|
$this->excluded, |
311
|
4 |
|
$this->puttable, |
312
|
4 |
|
$this->postable, |
313
|
4 |
|
$this->includable, |
314
|
4 |
|
$this->includablePaths, |
315
|
4 |
|
$this->subResource, |
316
|
4 |
|
$this->methods, |
317
|
4 |
|
$this->association, |
318
|
4 |
|
$this->collection, |
319
|
4 |
|
$this->virtual, |
320
|
4 |
|
$this->subResourcePath |
321
|
4 |
|
) = unserialize($str); |
322
|
|
|
try { |
323
|
4 |
|
$this->reflection = new \ReflectionProperty($this->class, $this->name); |
324
|
4 |
|
$this->reflection->setAccessible(true); |
325
|
2 |
|
} catch (\ReflectionException $e) { |
|
|
|
|
326
|
|
|
/* Ignore missing property definition as they might just be overridden or virtual and therefore only exist |
327
|
|
|
in the parent class. They will be accessible after merging. */ |
328
|
|
|
} |
329
|
4 |
|
} |
330
|
|
|
} |
331
|
|
|
|