1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Mapado\RestClientSdk\Mapping; |
||
6 | |||
7 | use Mapado\RestClientSdk\EntityRepository; |
||
8 | use Mapado\RestClientSdk\Exception\MissingIdentifierException; |
||
9 | use Mapado\RestClientSdk\Exception\MoreThanOneIdentifierException; |
||
10 | |||
11 | /** |
||
12 | * Class ClassMetadata |
||
13 | * |
||
14 | * @author Julien Deniau <[email protected]> |
||
15 | */ |
||
16 | class ClassMetadata |
||
17 | { |
||
18 | /** |
||
19 | * Model name (entity class with full namespace, ie: "Foo\Entity\Article"). |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | private $modelName; |
||
24 | |||
25 | /** |
||
26 | * Model key, used as path prefix for API calls. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $key; |
||
31 | |||
32 | /** |
||
33 | * Repository name (repository class with full namespace, ie: "Foo\Repository\ArticleRepository"). |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | private $repositoryName; |
||
38 | |||
39 | /** |
||
40 | * attributeList |
||
41 | * |
||
42 | * @var array<Attribute> |
||
43 | */ |
||
44 | private $attributeList; |
||
45 | |||
46 | /** |
||
47 | * relationList |
||
48 | * |
||
49 | * @var array<Relation> |
||
50 | */ |
||
51 | private $relationList; |
||
52 | |||
53 | /** |
||
54 | * identifierAttribute |
||
55 | * |
||
56 | * @var ?Attribute |
||
57 | */ |
||
58 | private $identifierAttribute; |
||
59 | |||
60 | public function __construct( |
||
61 | string $key, |
||
62 | string $modelName, |
||
63 | ?string $repositoryName = null |
||
64 | ) { |
||
65 | 1 | $this->key = $key; |
|
66 | 1 | $this->modelName = $modelName; |
|
67 | 1 | $this->repositoryName = $repositoryName ?? EntityRepository::class; |
|
68 | 1 | $this->attributeList = []; |
|
69 | 1 | $this->relationList = []; |
|
70 | 1 | } |
|
71 | |||
72 | public function getModelName(): string |
||
73 | { |
||
74 | return $this->modelName; |
||
75 | } |
||
76 | |||
77 | public function setModelName(string $modelName): self |
||
78 | { |
||
79 | $this->modelName = $modelName; |
||
80 | |||
81 | return $this; |
||
82 | } |
||
83 | |||
84 | public function getKey(): string |
||
85 | { |
||
86 | return $this->key; |
||
87 | } |
||
88 | |||
89 | public function setKey(string $key): self |
||
90 | { |
||
91 | $this->key = $key; |
||
92 | |||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | public function getAttribute(string $name): ?Attribute |
||
97 | { |
||
98 | return $this->attributeList[$name] ?? null; |
||
99 | } |
||
100 | |||
101 | public function hasIdentifierAttribute(): bool |
||
102 | { |
||
103 | return (bool) $this->identifierAttribute; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @throws MissingIdentifierException |
||
108 | */ |
||
109 | public function getIdentifierAttribute(): Attribute |
||
110 | { |
||
111 | 1 | if (!$this->identifierAttribute) { |
|
112 | 1 | throw new MissingIdentifierException( |
|
113 | 1 | sprintf( |
|
114 | 1 | 'Ressource "%s" does not contains an identifier. You can not call %s. You may want to call `hasIdentifierAttribute` before.', |
|
115 | 1 | $this->modelName, |
|
116 | 1 | __METHOD__ |
|
117 | ) |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | return $this->identifierAttribute; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return array<Attribute> |
||
126 | */ |
||
127 | public function getAttributeList(): array |
||
128 | { |
||
129 | return $this->attributeList; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Setter for attributeList |
||
134 | * |
||
135 | * @param iterable<Attribute> $attributeList |
||
136 | */ |
||
137 | public function setAttributeList($attributeList): self |
||
138 | { |
||
139 | 1 | $this->attributeList = []; |
|
140 | |||
141 | 1 | foreach ($attributeList as $attribute) { |
|
142 | 1 | $this->attributeList[$attribute->getSerializedKey()] = $attribute; |
|
143 | |||
144 | 1 | if ($attribute->isIdentifier()) { |
|
145 | 1 | if ($this->identifierAttribute) { |
|
146 | 1 | throw new MoreThanOneIdentifierException( |
|
147 | 1 | sprintf( |
|
148 | 1 | 'Class metadata for model "%s" already has an identifier named "%s". Only one identifier is allowed.', |
|
149 | 1 | $this->modelName, |
|
150 | 1 | $this->identifierAttribute->getSerializedKey() |
|
151 | ) |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | 1 | $this->identifierAttribute = $attribute; |
|
156 | } |
||
157 | } |
||
158 | |||
159 | 1 | return $this; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * Getter for relationList |
||
164 | * |
||
165 | * @return array<Relation> |
||
166 | */ |
||
167 | public function getRelationList(): array |
||
168 | { |
||
169 | return $this->relationList; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Setter for relationList |
||
174 | * |
||
175 | * @param array<Relation> $relationList |
||
176 | */ |
||
177 | public function setRelationList($relationList): self |
||
178 | { |
||
179 | $this->relationList = $relationList; |
||
180 | |||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | public function getRelation(string $key): ?Relation |
||
185 | { |
||
186 | if (!empty($this->relationList)) { |
||
187 | foreach ($this->relationList as $relation) { |
||
188 | if ($relation->getSerializedKey() == $key) { |
||
189 | return $relation; |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | return null; |
||
195 | } |
||
196 | |||
197 | public function getRepositoryName(): string |
||
198 | { |
||
199 | return $this->repositoryName; |
||
200 | } |
||
201 | |||
202 | public function setRepositoryName(string $repositoryName): self |
||
203 | { |
||
204 | $this->repositoryName = $repositoryName; |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | public function getIdGetter(): string |
||
210 | { |
||
211 | return 'get' . ucfirst($this->getIdKey()); |
||
212 | } |
||
213 | |||
214 | public function getIdSerializeKey(): string |
||
215 | { |
||
216 | return $this->getIdentifierAttribute()->getSerializedKey(); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * return default serialize model with null value or empty array on relations |
||
221 | * |
||
222 | * @return array<string, array|null> |
||
223 | */ |
||
224 | public function getDefaultSerializedModel(): array |
||
225 | { |
||
226 | $out = []; |
||
227 | $attributeList = $this->getAttributeList(); |
||
228 | if ($attributeList) { |
||
0 ignored issues
–
show
|
|||
229 | foreach ($attributeList as $attribute) { |
||
230 | $out[$attribute->getSerializedKey()] = null; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | $relationList = $this->getRelationList(); |
||
235 | if ($relationList) { |
||
0 ignored issues
–
show
The expression
$relationList of type Mapado\RestClientSdk\Mapping\Relation[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
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 ![]() |
|||
236 | foreach ($relationList as $relation) { |
||
237 | if ($relation->isOneToMany()) { |
||
238 | $out[$relation->getSerializedKey()] = []; |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | |||
243 | return $out; |
||
244 | } |
||
245 | |||
246 | private function getIdKey(): string |
||
247 | { |
||
248 | return $this->getIdentifierAttribute()->getAttributeName(); |
||
249 | } |
||
250 | } |
||
251 |
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.