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