1
|
|
|
<?php namespace Limoncello\Flute\Validation\JsonApi\Execution; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2018 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Flute\Contracts\Validation\JsonApiDataRulesInterface; |
20
|
|
|
use Limoncello\Flute\Contracts\Validation\JsonApiDataRulesSerializerInterface; |
21
|
|
|
use Limoncello\Flute\Validation\Serialize\RulesSerializer; |
22
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
23
|
|
|
use Neomerx\JsonApi\Contracts\Document\DocumentInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Limoncello\Flute |
27
|
|
|
* |
28
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
29
|
|
|
*/ |
30
|
|
|
class JsonApiDataRulesSerializer extends RulesSerializer implements JsonApiDataRulesSerializerInterface |
31
|
|
|
{ |
32
|
|
|
/** Serialized indexes key */ |
33
|
|
|
protected const SERIALIZED_RULES = 0; |
34
|
|
|
|
35
|
|
|
/** Serialized rules key */ |
36
|
|
|
protected const SERIALIZED_BLOCKS = self::SERIALIZED_RULES + 1; |
37
|
|
|
|
38
|
|
|
/** Index key */ |
39
|
|
|
protected const ID_SERIALIZED = 0; |
40
|
|
|
|
41
|
|
|
/** Index key */ |
42
|
|
|
protected const TYPE_SERIALIZED = self::ID_SERIALIZED + 1; |
43
|
|
|
|
44
|
|
|
/** Index key */ |
45
|
|
|
protected const ATTRIBUTES_SERIALIZED = self::TYPE_SERIALIZED + 1; |
46
|
|
|
|
47
|
|
|
/** Index key */ |
48
|
|
|
protected const TO_ONE_SERIALIZED = self::ATTRIBUTES_SERIALIZED + 1; |
49
|
|
|
|
50
|
|
|
/** Index key */ |
51
|
|
|
protected const TO_MANY_SERIALIZED = self::TO_ONE_SERIALIZED + 1; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $serializedRules = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
28 |
|
public function addRulesFromClass(string $rulesClass): JsonApiDataRulesSerializerInterface |
62
|
|
|
{ |
63
|
28 |
|
assert(static::isRulesClass($rulesClass)); |
|
|
|
|
64
|
|
|
|
65
|
28 |
|
$name = $rulesClass; |
66
|
|
|
|
67
|
|
|
/** @var JsonApiDataRulesInterface $rulesClass */ |
68
|
|
|
|
69
|
28 |
|
return $this->addDataRules( |
70
|
28 |
|
$name, |
71
|
28 |
|
$rulesClass::getIdRule(), |
72
|
28 |
|
$rulesClass::getTypeRule(), |
73
|
28 |
|
$rulesClass::getAttributeRules(), |
74
|
28 |
|
$rulesClass::getToOneRelationshipRules(), |
75
|
28 |
|
$rulesClass::getToManyRelationshipRules() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
44 |
|
public function addDataRules( |
83
|
|
|
string $name, |
84
|
|
|
RuleInterface $idRule, |
85
|
|
|
RuleInterface $typeRule, |
86
|
|
|
array $attributeRules, |
87
|
|
|
array $toOneRules, |
88
|
|
|
array $toManyRules |
89
|
|
|
): JsonApiDataRulesSerializerInterface { |
90
|
44 |
|
$idRule->setName(DocumentInterface::KEYWORD_ID)->enableCapture(); |
91
|
44 |
|
$typeRule->setName(DocumentInterface::KEYWORD_TYPE)->enableCapture(); |
92
|
|
|
|
93
|
|
|
$ruleSet = [ |
94
|
44 |
|
static::ID_SERIALIZED => $this->addRule($idRule), |
95
|
44 |
|
static::TYPE_SERIALIZED => $this->addRule($typeRule), |
96
|
44 |
|
static::ATTRIBUTES_SERIALIZED => $this->addRules($attributeRules), |
97
|
44 |
|
static::TO_ONE_SERIALIZED => $this->addRules($toOneRules), |
98
|
44 |
|
static::TO_MANY_SERIALIZED => $this->addRules($toManyRules), |
99
|
|
|
]; |
100
|
|
|
|
101
|
44 |
|
$this->serializedRules[$name] = $ruleSet; |
102
|
|
|
|
103
|
44 |
|
$this->getSerializer()->clearBlocksWithStart()->clearBlocksWithEnd(); |
104
|
|
|
|
105
|
44 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
* |
111
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
112
|
|
|
*/ |
113
|
44 |
|
public function getData(): array |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
44 |
|
static::SERIALIZED_RULES => $this->serializedRules, |
117
|
44 |
|
static::SERIALIZED_BLOCKS => static::getBlocks(), |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
26 |
|
public static function readRules(string $rulesClass, array $serializedData): array |
125
|
|
|
{ |
126
|
26 |
|
assert(static::hasRules($rulesClass, $serializedData)); |
127
|
|
|
|
128
|
26 |
|
$indexes = $serializedData[static::SERIALIZED_RULES][$rulesClass]; |
129
|
|
|
|
130
|
26 |
|
return $indexes; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritdoc |
135
|
|
|
*/ |
136
|
26 |
|
public static function hasRules(string $name, array $serializedData): bool |
137
|
|
|
{ |
138
|
|
|
// the value could be null so we have to check by key existence. |
139
|
|
|
return |
140
|
26 |
|
array_key_exists(static::SERIALIZED_RULES, $serializedData) === true && |
141
|
26 |
|
array_key_exists($name, $serializedData[static::SERIALIZED_RULES]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
147
|
26 |
|
public static function readBlocks(array $serializedData): array |
148
|
|
|
{ |
149
|
26 |
|
assert(array_key_exists(static::SERIALIZED_BLOCKS, $serializedData)); |
150
|
26 |
|
$serializedRules = $serializedData[static::SERIALIZED_BLOCKS]; |
151
|
26 |
|
assert(is_array($serializedRules)); |
152
|
|
|
|
153
|
26 |
|
return $serializedRules; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritdoc |
158
|
|
|
*/ |
159
|
26 |
|
public static function readIdRuleIndexes(array $serializedRules): array |
160
|
|
|
{ |
161
|
26 |
|
assert(array_key_exists(static::ID_SERIALIZED, $serializedRules)); |
162
|
26 |
|
$rule = $serializedRules[static::ID_SERIALIZED]; |
163
|
26 |
|
assert(is_array($rule)); |
164
|
|
|
|
165
|
26 |
|
return $rule; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @inheritdoc |
170
|
|
|
*/ |
171
|
26 |
|
public static function readTypeRuleIndexes(array $serializedRules): array |
172
|
|
|
{ |
173
|
26 |
|
assert(array_key_exists(static::TYPE_SERIALIZED, $serializedRules)); |
174
|
26 |
|
$rule = $serializedRules[static::TYPE_SERIALIZED]; |
175
|
26 |
|
assert(is_array($rule)); |
176
|
|
|
|
177
|
26 |
|
return $rule; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritdoc |
182
|
|
|
*/ |
183
|
26 |
|
public static function readAttributeRulesIndexes(array $serializedRules): array |
184
|
|
|
{ |
185
|
26 |
|
assert(array_key_exists(static::ATTRIBUTES_SERIALIZED, $serializedRules)); |
186
|
26 |
|
$rules = $serializedRules[static::ATTRIBUTES_SERIALIZED]; |
187
|
26 |
|
assert(is_array($rules)); |
188
|
|
|
|
189
|
26 |
|
return $rules; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritdoc |
194
|
|
|
*/ |
195
|
26 |
|
public static function readToOneRulesIndexes(array $serializedRules): array |
196
|
|
|
{ |
197
|
26 |
|
assert(array_key_exists(static::TO_ONE_SERIALIZED, $serializedRules)); |
198
|
26 |
|
$rules = $serializedRules[static::TO_ONE_SERIALIZED]; |
199
|
26 |
|
assert(is_array($rules)); |
200
|
|
|
|
201
|
26 |
|
return $rules; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @inheritdoc |
206
|
|
|
*/ |
207
|
26 |
|
public static function readToManyRulesIndexes(array $serializedRules): array |
208
|
|
|
{ |
209
|
26 |
|
assert(array_key_exists(static::TO_MANY_SERIALIZED, $serializedRules)); |
210
|
26 |
|
$rules = $serializedRules[static::TO_MANY_SERIALIZED]; |
211
|
26 |
|
assert(is_array($rules)); |
212
|
|
|
|
213
|
26 |
|
return $rules; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritdoc |
218
|
|
|
*/ |
219
|
19 |
|
public static function readRuleIndex(array $ruleIndexes): int |
220
|
|
|
{ |
221
|
19 |
|
return parent::getRuleIndex($ruleIndexes); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @inheritdoc |
226
|
|
|
*/ |
227
|
20 |
|
public static function readRuleStartIndexes(array $ruleIndexes): array |
228
|
|
|
{ |
229
|
20 |
|
return parent::getRuleStartIndexes($ruleIndexes); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @inheritdoc |
234
|
|
|
*/ |
235
|
20 |
|
public static function readRuleEndIndexes(array $ruleIndexes): array |
236
|
|
|
{ |
237
|
20 |
|
return parent::getRuleEndIndexes($ruleIndexes); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @inheritdoc |
242
|
|
|
*/ |
243
|
26 |
|
public static function readRulesIndexes(array $arrayRuleIndexes): array |
244
|
|
|
{ |
245
|
26 |
|
return parent::getRulesIndexes($arrayRuleIndexes); |
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @inheritdoc |
250
|
|
|
*/ |
251
|
26 |
|
public static function readRulesStartIndexes(array $arrayRuleIndexes): array |
252
|
|
|
{ |
253
|
26 |
|
return parent::getRulesStartIndexes($arrayRuleIndexes); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @inheritdoc |
258
|
|
|
*/ |
259
|
26 |
|
public static function readRulesEndIndexes(array $arrayRuleIndexes): array |
260
|
|
|
{ |
261
|
26 |
|
return parent::getRulesEndIndexes($arrayRuleIndexes); |
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @inheritdoc |
266
|
|
|
*/ |
267
|
6 |
|
public static function readSingleRuleIndexes(array $arrayRuleIndexes, string $name): array |
268
|
|
|
{ |
269
|
6 |
|
return parent::geSingleRuleIndexes($arrayRuleIndexes, $name); |
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @inheritdoc |
274
|
|
|
*/ |
275
|
21 |
|
public static function hasRule(int $index, array $blocks): bool |
276
|
|
|
{ |
277
|
21 |
|
$result = array_key_exists($index, $blocks); |
278
|
|
|
|
279
|
21 |
|
return $result; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string $rulesClass |
284
|
|
|
* |
285
|
|
|
* @return bool |
286
|
|
|
*/ |
287
|
28 |
|
private static function isRulesClass(string $rulesClass): bool |
288
|
|
|
{ |
289
|
|
|
return |
290
|
28 |
|
class_exists($rulesClass) === true && |
291
|
28 |
|
in_array(JsonApiDataRulesInterface::class, class_implements($rulesClass)) === true; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: