1 | <?php namespace Limoncello\Flute\Validation\JsonApi\Execution; |
||
30 | class JsonApiQueryRulesSerializer extends RulesSerializer implements JsonApiQueryRulesSerializerInterface |
||
31 | { |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $serializedRules = []; |
||
36 | |||
37 | /** Index key */ |
||
38 | protected const IDENTITY_RULE= 0; |
||
39 | |||
40 | /** Index key */ |
||
41 | protected const FILTER_RULES = self::IDENTITY_RULE + 1; |
||
42 | |||
43 | /** Index key */ |
||
44 | protected const FIELD_SET_RULES = self::FILTER_RULES + 1; |
||
45 | |||
46 | /** Index key */ |
||
47 | protected const SORTS_RULE = self::FIELD_SET_RULES + 1; |
||
48 | |||
49 | /** Index key */ |
||
50 | protected const INCLUDES_RULE = self::SORTS_RULE + 1; |
||
51 | |||
52 | /** Index key */ |
||
53 | protected const PAGE_OFFSET_RULE = self::INCLUDES_RULE + 1; |
||
54 | |||
55 | /** Index key */ |
||
56 | protected const PAGE_LIMIT_RULE = self::PAGE_OFFSET_RULE + 1; |
||
57 | |||
58 | /** Index key */ |
||
59 | protected const SINGLE_RULE_INDEX = 0; |
||
60 | |||
61 | /** Serialized indexes key */ |
||
62 | protected const SERIALIZED_RULES = 0; |
||
63 | |||
64 | /** Serialized rules key */ |
||
65 | protected const SERIALIZED_BLOCKS = self::SERIALIZED_RULES + 1; |
||
66 | |||
67 | /** |
||
68 | * @inheritdoc |
||
69 | */ |
||
70 | 43 | public function addRulesFromClass(string $rulesClass): JsonApiQueryRulesSerializerInterface |
|
71 | { |
||
72 | 43 | assert(static::isRulesClass($rulesClass)); |
|
|
|||
73 | |||
74 | 43 | $name = $rulesClass; |
|
75 | |||
76 | /** @var JsonApiQueryRulesInterface $rulesClass */ |
||
77 | |||
78 | 43 | return $this->addQueryRules( |
|
79 | 43 | $name, |
|
80 | 43 | $rulesClass::getIdentityRule(), |
|
81 | 43 | $rulesClass::getFilterRules(), |
|
82 | 43 | $rulesClass::getFieldSetRules(), |
|
83 | 43 | $rulesClass::getSortsRule(), |
|
84 | 43 | $rulesClass::getIncludesRule(), |
|
85 | 43 | $rulesClass::getPageOffsetRule(), |
|
86 | 43 | $rulesClass::getPageLimitRule() |
|
87 | ); |
||
88 | } |
||
89 | |||
90 | /** @noinspection PhpTooManyParametersInspection |
||
91 | * @inheritdoc |
||
92 | * |
||
93 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
94 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
95 | */ |
||
96 | 43 | public function addQueryRules( |
|
97 | string $name, |
||
98 | ?RuleInterface $identityRule, |
||
99 | ?array $filterRules, |
||
100 | ?array $fieldSetRules, |
||
101 | ?RuleInterface $sortsRule, |
||
102 | ?RuleInterface $includesRule, |
||
103 | ?RuleInterface $pageOffsetRule, |
||
104 | ?RuleInterface $pageLimitRule |
||
105 | ): JsonApiQueryRulesSerializerInterface { |
||
106 | 43 | assert(!empty($name)); |
|
107 | 43 | assert(static::hasRules($name, $this->serializedRules) === false); |
|
108 | |||
109 | 43 | $identityRule === null ?: $identityRule->setName(JsonApiQueryValidatingParserInterface::PARAM_IDENTITY); |
|
110 | 43 | $sortsRule === null ?: $sortsRule->setName(JsonApiQueryValidatingParserInterface::PARAM_SORT); |
|
111 | 43 | $includesRule === null ?: $includesRule->setName(JsonApiQueryValidatingParserInterface::PARAM_INCLUDE); |
|
112 | 43 | $pageOffsetRule === null ?: $pageOffsetRule->setName(JsonApiQueryValidatingParserInterface::PARAM_PAGE); |
|
113 | 43 | $pageLimitRule === null ?: $pageLimitRule->setName(JsonApiQueryValidatingParserInterface::PARAM_PAGE); |
|
114 | |||
115 | 43 | $this->serializedRules[$name] = [ |
|
116 | 43 | static::IDENTITY_RULE => |
|
117 | 43 | $identityRule === null ? null : $this->addRules([static::SINGLE_RULE_INDEX => $identityRule]), |
|
118 | 43 | static::FILTER_RULES => |
|
119 | 43 | $filterRules === null ? null : $this->addRules($filterRules), |
|
120 | 43 | static::FIELD_SET_RULES => |
|
121 | 43 | $fieldSetRules === null ? null : $this->addRules($fieldSetRules), |
|
122 | 43 | static::SORTS_RULE => |
|
123 | 43 | $sortsRule === null ? null : $this->addRules([static::SINGLE_RULE_INDEX => $sortsRule]), |
|
124 | 43 | static::INCLUDES_RULE => |
|
125 | 43 | $includesRule === null ? null : $this->addRules([static::SINGLE_RULE_INDEX => $includesRule]), |
|
126 | 43 | static::PAGE_OFFSET_RULE => |
|
127 | 43 | $pageOffsetRule === null ? null : $this->addRules([static::SINGLE_RULE_INDEX => $pageOffsetRule]), |
|
128 | 43 | static::PAGE_LIMIT_RULE => |
|
129 | 43 | $pageLimitRule === null ? null : $this->addRules([static::SINGLE_RULE_INDEX => $pageLimitRule]), |
|
130 | ]; |
||
131 | |||
132 | 43 | return $this; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * @inheritdoc |
||
137 | */ |
||
138 | 43 | public function getData(): array |
|
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | 58 | public static function readBlocks(array $serializedData): array |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 43 | public static function hasRules(string $name, array $serializedData): bool |
|
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | */ |
||
168 | 36 | public static function readRules(string $name, array $serializedData): array |
|
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | */ |
||
178 | 5 | public static function readIdentityRuleIndexes(array $serializedRules): ?array |
|
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | 22 | public static function readFilterRulesIndexes(array $serializedRules): ?array |
|
190 | |||
191 | /** |
||
192 | * @inheritdoc |
||
193 | */ |
||
194 | 2 | public static function readFieldSetRulesIndexes(array $serializedRules): ?array |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | 16 | public static function readSortsRuleIndexes(array $serializedRules): ?array |
|
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | */ |
||
210 | 16 | public static function readIncludesRuleIndexes(array $serializedRules): ?array |
|
214 | |||
215 | /** |
||
216 | * @inheritdoc |
||
217 | */ |
||
218 | 35 | public static function readPageOffsetRuleIndexes(array $serializedRules): ?array |
|
222 | |||
223 | /** |
||
224 | * @inheritdoc |
||
225 | */ |
||
226 | 35 | public static function readPageLimitRuleIndexes(array $serializedRules): ?array |
|
230 | |||
231 | /** |
||
232 | * @inheritdoc |
||
233 | */ |
||
234 | 22 | public static function readRuleMainIndexes(array $ruleIndexes): ?array |
|
238 | |||
239 | /** |
||
240 | * @inheritdoc |
||
241 | */ |
||
242 | 34 | public static function readRuleMainIndex(array $ruleIndexes): ?int |
|
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | */ |
||
253 | 34 | public static function readRuleStartIndexes(array $ruleIndexes): array |
|
257 | |||
258 | /** |
||
259 | * @inheritdoc |
||
260 | */ |
||
261 | 34 | public static function readRuleEndIndexes(array $ruleIndexes): array |
|
265 | |||
266 | /** |
||
267 | * @param string $rulesClass |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | 43 | private static function isRulesClass(string $rulesClass): bool |
|
277 | } |
||
278 |
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: