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\Event\AfterObjectEvent; |
9
|
|
|
use Remorhaz\JSON\Data\Event\BeforeObjectEvent; |
10
|
|
|
use Remorhaz\JSON\Data\Event\PropertyEvent; |
11
|
|
|
use Remorhaz\JSON\Data\Iterator\ValueIteratorFactoryInterface; |
12
|
|
|
use Remorhaz\JSON\Data\Value\ObjectValueInterface; |
13
|
|
|
use Remorhaz\JSON\Data\Path\PathInterface; |
14
|
|
|
use Remorhaz\JSON\Data\Value\NodeValueInterface; |
15
|
|
|
use stdClass; |
16
|
|
|
|
17
|
|
|
final class NodeObjectValue implements NodeValueInterface, ObjectValueInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
private $data; |
21
|
|
|
|
22
|
|
|
private $path; |
23
|
|
|
|
24
|
|
|
private $valueFactory; |
25
|
|
|
|
26
|
|
|
private $valueIteratorFactory; |
27
|
|
|
|
28
|
3 |
|
public function __construct( |
29
|
|
|
stdClass $data, |
30
|
|
|
PathInterface $path, |
31
|
|
|
NodeValueFactoryInterface $valueFactory, |
32
|
|
|
ValueIteratorFactoryInterface $valueIteratorFactory |
33
|
|
|
) { |
34
|
3 |
|
$this->data = $data; |
35
|
3 |
|
$this->path = $path; |
36
|
3 |
|
$this->valueFactory = $valueFactory; |
37
|
3 |
|
$this->valueIteratorFactory = $valueIteratorFactory; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
3 |
|
public function createChildIterator(): Iterator |
41
|
|
|
{ |
42
|
|
|
return $this |
43
|
3 |
|
->valueIteratorFactory |
44
|
3 |
|
->createObjectIterator($this->createEventIterator()); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
public function createEventIterator(): Iterator |
48
|
|
|
{ |
49
|
3 |
|
return $this->createEventGenerator($this->data, $this->path); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
private function createEventGenerator(stdClass $data, PathInterface $path): Generator |
53
|
|
|
{ |
54
|
3 |
|
yield new BeforeObjectEvent($this); |
55
|
|
|
|
56
|
3 |
|
foreach (get_object_vars($data) as $name => $property) { |
57
|
2 |
|
yield new PropertyEvent($name, $path); |
58
|
|
|
yield from $this |
59
|
2 |
|
->valueFactory |
60
|
2 |
|
->createValue($property, $path->copyWithProperty($name)) |
61
|
2 |
|
->createEventIterator(); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
yield new AfterObjectEvent($this); |
65
|
3 |
|
} |
66
|
|
|
|
67
|
3 |
|
public function getPath(): PathInterface |
68
|
|
|
{ |
69
|
3 |
|
return $this->path; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|