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