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