1 | <?php declare(strict_types=1); |
||
35 | final class BlockSerializer implements BlockSerializerInterface |
||
36 | { |
||
37 | /** |
||
38 | * Index of the first block. |
||
39 | */ |
||
40 | public const FIRST_BLOCK_INDEX = 0; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | private $currentBlockIndex = 0; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $serializedBlocks = []; |
||
51 | |||
52 | /** |
||
53 | * @var int[] |
||
54 | */ |
||
55 | private $blocksWithStart = []; |
||
56 | |||
57 | /** |
||
58 | * @var int[] |
||
59 | */ |
||
60 | private $blocksWithEnd = []; |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | 13 | public function serialize(ExecutionBlockInterface $block): BlockSerializerInterface |
|
71 | |||
72 | /** |
||
73 | * @inheritdoc |
||
74 | * |
||
75 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
76 | */ |
||
77 | 26 | public function addBlock(ExecutionBlockInterface $block): int |
|
94 | |||
95 | /** |
||
96 | * @return array |
||
97 | */ |
||
98 | 25 | public function getSerializedBlocks(): array |
|
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | 25 | public function getBlocksWithStart(): array |
|
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | 25 | public function getBlocksWithEnd(): array |
|
118 | |||
119 | /** |
||
120 | * @inheritdoc |
||
121 | */ |
||
122 | 13 | public function clearBlocks(): BlockSerializerInterface |
|
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 13 | public function clearBlocksWithStart(): BlockSerializerInterface |
|
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | 13 | public function clearBlocksWithEnd(): BlockSerializerInterface |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 25 | public function get(): array |
|
162 | |||
163 | /** |
||
164 | * @inheritdoc |
||
165 | */ |
||
166 | 21 | public static function unserializeBlocks(array $serialized): array |
|
172 | |||
173 | /** |
||
174 | * @inheritdoc |
||
175 | */ |
||
176 | 21 | public static function unserializeBlocksWithStart(array $serialized): array |
|
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | 21 | public static function unserializeBlocksWithEnd(array $serialized): array |
|
192 | |||
193 | /** |
||
194 | * @param int $key |
||
195 | * @param array $properties |
||
196 | * |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 21 | private static function readProperty(int $key, array $properties) |
|
205 | |||
206 | /** |
||
207 | * @return BlockSerializerInterface |
||
208 | */ |
||
209 | 13 | private function reset(): BlockSerializerInterface |
|
213 | |||
214 | /** |
||
215 | * @param ProcedureBlockInterface $procedure |
||
216 | * |
||
217 | * @return int |
||
218 | */ |
||
219 | 25 | private function serializeProcedure(ProcedureBlockInterface $procedure): int |
|
243 | |||
244 | /** |
||
245 | * @param IfExpressionInterface $ifExpression |
||
246 | * |
||
247 | * @return int |
||
248 | */ |
||
249 | 8 | private function serializeIfExpression(IfExpressionInterface $ifExpression): int |
|
274 | |||
275 | /** |
||
276 | * @param AndExpressionInterface $andExpression |
||
277 | * |
||
278 | * @return int |
||
279 | */ |
||
280 | 12 | private function serializeAndExpression(AndExpressionInterface $andExpression): int |
|
281 | { |
||
282 | 12 | $index = $this->allocateIndex(); |
|
283 | |||
284 | $serialized = [ |
||
285 | 12 | static::TYPE => static::TYPE__AND_EXPRESSION, |
|
286 | 12 | static::AND_EXPRESSION_PRIMARY => $this->addBlock($andExpression->getPrimary()), |
|
287 | 12 | static::AND_EXPRESSION_SECONDARY => $this->addBlock($andExpression->getSecondary()), |
|
288 | ]; |
||
289 | 12 | if (empty($andExpression->getProperties()) === false) { |
|
290 | 12 | $serialized[static::PROPERTIES] = $andExpression->getProperties(); |
|
291 | } |
||
292 | |||
293 | 12 | $this->serializedBlocks[$index] = $serialized; |
|
294 | |||
295 | 12 | return $index; |
|
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param OrExpressionInterface $orExpression |
||
300 | * |
||
301 | * @return int |
||
302 | */ |
||
303 | 5 | private function serializeOrExpression(OrExpressionInterface $orExpression): int |
|
304 | { |
||
305 | 5 | $index = $this->allocateIndex(); |
|
306 | |||
307 | $serialized = [ |
||
308 | 5 | static::TYPE => static::TYPE__OR_EXPRESSION, |
|
309 | 5 | static::OR_EXPRESSION_PRIMARY => $this->addBlock($orExpression->getPrimary()), |
|
310 | 5 | static::OR_EXPRESSION_SECONDARY => $this->addBlock($orExpression->getSecondary()), |
|
311 | ]; |
||
312 | 5 | if (empty($orExpression->getProperties()) === false) { |
|
313 | 5 | $serialized[static::PROPERTIES] = $orExpression->getProperties(); |
|
314 | } |
||
315 | |||
316 | 5 | $this->serializedBlocks[$index] = $serialized; |
|
317 | |||
318 | 5 | return $index; |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * @return int |
||
323 | */ |
||
324 | 25 | private function allocateIndex(): int |
|
330 | } |
||
331 |
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.