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