|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Remorhaz\JSON\Data\Value\DecodedJson; |
|
5
|
|
|
|
|
6
|
|
|
use Generator; |
|
7
|
|
|
use Iterator; |
|
8
|
|
|
use Remorhaz\JSON\Data\Iterator\ValueIteratorFactoryInterface; |
|
9
|
|
|
use Remorhaz\JSON\Data\Value\ArrayValueInterface; |
|
10
|
|
|
use Remorhaz\JSON\Data\Event\AfterArrayEvent; |
|
11
|
|
|
use Remorhaz\JSON\Data\Event\BeforeArrayEvent; |
|
12
|
|
|
use Remorhaz\JSON\Data\Event\ElementEvent; |
|
13
|
|
|
use Remorhaz\JSON\Data\Path\PathInterface; |
|
14
|
|
|
use Remorhaz\JSON\Data\Value\NodeValueInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class NodeArrayValue implements NodeValueInterface, ArrayValueInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
private $data; |
|
20
|
|
|
|
|
21
|
|
|
private $path; |
|
22
|
|
|
|
|
23
|
|
|
private $valueFactory; |
|
24
|
|
|
|
|
25
|
|
|
private $valueIteratorFactory; |
|
26
|
|
|
|
|
27
|
5 |
|
public function __construct( |
|
28
|
|
|
array $data, |
|
29
|
|
|
PathInterface $path, |
|
30
|
|
|
NodeValueFactoryInterface $valueFactory, |
|
31
|
|
|
ValueIteratorFactoryInterface $valueIteratorFactory |
|
32
|
|
|
) { |
|
33
|
5 |
|
$this->data = $data; |
|
34
|
5 |
|
$this->path = $path; |
|
35
|
5 |
|
$this->valueFactory = $valueFactory; |
|
36
|
5 |
|
$this->valueIteratorFactory = $valueIteratorFactory; |
|
37
|
5 |
|
} |
|
38
|
|
|
|
|
39
|
3 |
|
public function createChildIterator(): Iterator |
|
40
|
|
|
{ |
|
41
|
|
|
return $this |
|
42
|
3 |
|
->valueIteratorFactory |
|
43
|
3 |
|
->createArrayIterator($this->createEventIterator()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
public function createEventIterator(): Iterator |
|
47
|
|
|
{ |
|
48
|
5 |
|
return $this->createEventGenerator($this->data, $this->path); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
5 |
|
private function createEventGenerator(array $data, PathInterface $path): Generator |
|
52
|
|
|
{ |
|
53
|
5 |
|
yield new BeforeArrayEvent($this); |
|
54
|
|
|
|
|
55
|
5 |
|
$validIndex = 0; |
|
56
|
5 |
|
foreach ($data as $index => $element) { |
|
57
|
4 |
|
if ($index !== $validIndex++) { |
|
58
|
2 |
|
throw new Exception\InvalidElementKeyException($index, $path); |
|
59
|
|
|
} |
|
60
|
2 |
|
yield new ElementEvent($index, $path); |
|
61
|
|
|
yield from $this |
|
62
|
2 |
|
->valueFactory |
|
63
|
2 |
|
->createValue($element, $path->copyWithElement($index)) |
|
64
|
2 |
|
->createEventIterator(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
yield new AfterArrayEvent($this); |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
public function getPath(): PathInterface |
|
71
|
|
|
{ |
|
72
|
3 |
|
return $this->path; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|