|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Remorhaz\JSON\Data\Export; |
|
6
|
|
|
|
|
7
|
|
|
use Iterator; |
|
8
|
|
|
use Remorhaz\JSON\Data\Event\AfterArrayEventInterface; |
|
9
|
|
|
use Remorhaz\JSON\Data\Event\AfterElementEventInterface; |
|
10
|
|
|
use Remorhaz\JSON\Data\Event\AfterObjectEventInterface; |
|
11
|
|
|
use Remorhaz\JSON\Data\Event\AfterPropertyEventInterface; |
|
12
|
|
|
use Remorhaz\JSON\Data\Event\BeforeArrayEventInterface; |
|
13
|
|
|
use Remorhaz\JSON\Data\Event\BeforeElementEventInterface; |
|
14
|
|
|
use Remorhaz\JSON\Data\Event\BeforeObjectEventInterface; |
|
15
|
|
|
use Remorhaz\JSON\Data\Event\BeforePropertyEventInterface; |
|
16
|
|
|
use Remorhaz\JSON\Data\Event\EventInterface; |
|
17
|
|
|
use Remorhaz\JSON\Data\Event\ScalarEventInterface; |
|
18
|
|
|
use Remorhaz\JSON\Data\Value\DecodedJson\NodeValueFactory; |
|
19
|
|
|
use Remorhaz\JSON\Data\Value\NodeValueInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class EventDecoder implements EventDecoderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Iterator|EventInterface[] $events |
|
26
|
|
|
* @return NodeValueInterface|null |
|
27
|
|
|
*/ |
|
28
|
|
|
public function exportEvents(Iterator $events): ?NodeValueInterface |
|
29
|
|
|
{ |
|
30
|
|
|
$buffer = []; |
|
31
|
|
|
$structures = []; |
|
32
|
|
|
$structure = null; |
|
33
|
|
|
foreach ($events as $event) { |
|
34
|
|
|
switch (true) { |
|
35
|
|
|
case $event instanceof ScalarEventInterface: |
|
36
|
|
|
$buffer[] = $event->getData(); |
|
37
|
|
|
break; |
|
38
|
|
|
|
|
39
|
|
|
case $event instanceof BeforeArrayEventInterface: |
|
40
|
|
|
case $event instanceof BeforeObjectEventInterface: |
|
41
|
|
|
$structures[] = $structure; |
|
42
|
|
|
$structure = []; |
|
43
|
|
|
break; |
|
44
|
|
|
|
|
45
|
|
|
case $event instanceof BeforeElementEventInterface: |
|
46
|
|
|
case $event instanceof BeforePropertyEventInterface: |
|
47
|
|
|
break; |
|
48
|
|
|
|
|
49
|
|
|
case $event instanceof AfterElementEventInterface: |
|
50
|
|
|
$structure[$event->getIndex()] = array_pop($buffer); |
|
51
|
|
|
break; |
|
52
|
|
|
|
|
53
|
|
|
case $event instanceof AfterPropertyEventInterface: |
|
54
|
|
|
$structure[$event->getName()] = array_pop($buffer); |
|
55
|
|
|
break; |
|
56
|
|
|
|
|
57
|
|
|
case $event instanceof AfterArrayEventInterface: |
|
58
|
|
|
$buffer[] = $structure; |
|
59
|
|
|
$structure = array_pop($structures); |
|
60
|
|
|
break; |
|
61
|
|
|
|
|
62
|
|
|
case $event instanceof AfterObjectEventInterface: |
|
63
|
|
|
$buffer[] = (object) $structure; |
|
64
|
|
|
$structure = array_pop($structures); |
|
65
|
|
|
break; |
|
66
|
|
|
|
|
67
|
|
|
default: |
|
68
|
|
|
throw new Exception\UnknownEventException($event); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
if (empty($buffer)) { |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
$data = array_pop($buffer); |
|
75
|
|
|
|
|
76
|
|
|
return (new NodeValueFactory())->createValue($data); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function exportExistingEvents(Iterator $events): NodeValueInterface |
|
80
|
|
|
{ |
|
81
|
|
|
$value = $this->exportEvents($events); |
|
82
|
|
|
if (isset($value)) { |
|
83
|
|
|
return $value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
throw new Exception\NoValueToExportException(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|