1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk\Mapping; |
4
|
|
|
|
5
|
|
|
use Mapado\RestClientSdk\Exception\MissingIdentifierException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ClassMetadata |
9
|
|
|
* |
10
|
|
|
* @author Julien Deniau <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class ClassMetadata |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Model name (entity class with full namespace, ie: "Foo\Entity\Article"). |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $modelName; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Model key, used as path prefix for API calls. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $key; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Repository name (repository class with full namespace, ie: "Foo\Repository\ArticleRepository"). |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $repositoryName; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* attributeList |
37
|
|
|
* |
38
|
|
|
* @var Attribute[] |
39
|
|
|
*/ |
40
|
|
|
private $attributeList; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* relationList |
44
|
|
|
* |
45
|
|
|
* @var Relation[] |
46
|
|
|
*/ |
47
|
|
|
private $relationList; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* identifierAttribute |
51
|
|
|
* |
52
|
|
|
* @var ?Attribute |
53
|
|
|
*/ |
54
|
|
|
private $identifierAttribute; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructor. |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* @param string $modelName |
61
|
|
|
* @param string $repositoryName |
62
|
|
|
*/ |
63
|
|
|
public function __construct($key, $modelName, $repositoryName) |
64
|
|
|
{ |
65
|
1 |
|
$this->key = $key; |
66
|
1 |
|
$this->modelName = $modelName; |
67
|
1 |
|
$this->repositoryName = $repositoryName; |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Getter for modelName |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getModelName() |
76
|
|
|
{ |
77
|
|
|
return $this->modelName; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Setter for modelName |
82
|
|
|
* |
83
|
|
|
* @param string $modelName |
84
|
|
|
* |
85
|
|
|
* @return ClassMetadata |
86
|
|
|
*/ |
87
|
|
|
public function setModelName($modelName) |
88
|
|
|
{ |
89
|
|
|
$this->modelName = $modelName; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Getter for key |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getKey() |
100
|
|
|
{ |
101
|
|
|
return $this->key; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Setter for key |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* |
109
|
|
|
* @return ClassMetadata |
110
|
|
|
*/ |
111
|
|
|
public function setKey($key) |
112
|
|
|
{ |
113
|
|
|
$this->key = $key; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* getAttribute |
120
|
|
|
* |
121
|
|
|
* @param string $name |
122
|
|
|
* |
123
|
|
|
* @return ?Attribute |
124
|
|
|
*/ |
125
|
|
|
public function getAttribute($name) |
126
|
|
|
{ |
127
|
|
|
return isset($this->attributeList[$name]) |
128
|
|
|
? $this->attributeList[$name] |
129
|
|
|
: null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function hasIdentifierAttribute(): bool |
133
|
|
|
{ |
134
|
|
|
return (bool) $this->identifierAttribute; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* getIdentifierAttribute |
139
|
|
|
* |
140
|
|
|
* @return Attribute |
141
|
|
|
*/ |
142
|
|
|
public function getIdentifierAttribute() |
143
|
|
|
{ |
144
|
1 |
|
if (!$this->identifierAttribute) { |
145
|
1 |
|
throw new MissingIdentifierException( |
146
|
1 |
|
sprintf( |
147
|
1 |
|
'Ressource "%s" does not contains an identifier. You can not call %s. You may want to call `hasIdentifierAttribute` before.', |
148
|
1 |
|
$this->modelName, |
149
|
1 |
|
__METHOD__ |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->identifierAttribute; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Getter for attributeList |
159
|
|
|
* |
160
|
|
|
* @return Attribute[] |
161
|
|
|
*/ |
162
|
|
|
public function getAttributeList() |
163
|
|
|
{ |
164
|
|
|
return $this->attributeList; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Setter for attributeList |
169
|
|
|
* |
170
|
|
|
* @param Attribute[] $attributeList |
171
|
|
|
* |
172
|
|
|
* @return ClassMetadata |
173
|
|
|
*/ |
174
|
|
|
public function setAttributeList($attributeList) |
175
|
|
|
{ |
176
|
1 |
|
$this->attributeList = []; |
177
|
|
|
|
178
|
1 |
|
foreach ($attributeList as $attribute) { |
179
|
1 |
|
$this->attributeList[$attribute->getSerializedKey()] = $attribute; |
180
|
|
|
|
181
|
1 |
|
if ($attribute->isIdentifier()) { |
182
|
1 |
|
$this->identifierAttribute = $attribute; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Getter for relationList |
191
|
|
|
* |
192
|
|
|
* @return Relation[] |
193
|
|
|
*/ |
194
|
|
|
public function getRelationList() |
195
|
|
|
{ |
196
|
|
|
return $this->relationList; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Setter for relationList |
201
|
|
|
* |
202
|
|
|
* @param Relation[] $relationList |
203
|
|
|
* |
204
|
|
|
* @return ClassMetadata |
205
|
|
|
*/ |
206
|
|
|
public function setRelationList($relationList) |
207
|
|
|
{ |
208
|
|
|
$this->relationList = $relationList; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* getRelation |
215
|
|
|
* |
216
|
|
|
* @param string $key |
217
|
|
|
* |
218
|
|
|
* @return Relation|null |
219
|
|
|
*/ |
220
|
|
|
public function getRelation($key) |
221
|
|
|
{ |
222
|
|
|
if (!empty($this->relationList)) { |
223
|
|
|
foreach ($this->relationList as $relation) { |
224
|
|
|
if ($relation->getSerializedKey() == $key) { |
225
|
|
|
return $relation; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Getter for repositoryName |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function getRepositoryName() |
237
|
|
|
{ |
238
|
|
|
return $this->repositoryName; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Setter for repositoryName |
243
|
|
|
* |
244
|
|
|
* @param string $repositoryName |
245
|
|
|
* |
246
|
|
|
* @return ClassMetadata |
247
|
|
|
*/ |
248
|
|
|
public function setRepositoryName($repositoryName) |
249
|
|
|
{ |
250
|
|
|
$this->repositoryName = $repositoryName; |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function getIdGetter() |
256
|
|
|
{ |
257
|
|
|
return 'get' . ucfirst($this->getIdKey()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function getIdSerializeKey() |
261
|
|
|
{ |
262
|
|
|
return $this->getIdentifierAttribute()->getSerializedKey(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* return default serialize model with null value or empty array on relations |
267
|
|
|
* |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
|
|
public function getDefaultSerializedModel() |
271
|
|
|
{ |
272
|
|
|
$out = []; |
273
|
|
|
$attributeList = $this->getAttributeList(); |
274
|
|
|
if ($attributeList) { |
|
|
|
|
275
|
|
|
foreach ($attributeList as $attribute) { |
276
|
|
|
$out[$attribute->getSerializedKey()] = null; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$relationList = $this->getRelationList(); |
281
|
|
|
if ($relationList) { |
|
|
|
|
282
|
|
|
foreach ($relationList as $relation) { |
283
|
|
|
if ($relation->isOneToMany()) { |
284
|
|
|
$out[$relation->getSerializedKey()] = []; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $out; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
private function getIdKey() |
293
|
|
|
{ |
294
|
|
|
return $this->getIdentifierAttribute()->getAttributeName(); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.