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