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