|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
namespace JsonDecodeStream; |
|
6
|
|
|
|
|
7
|
|
|
use JsonDecodeStream\Internal\Selector; |
|
8
|
|
|
use JsonDecodeStream\Internal\Stack; |
|
9
|
|
|
|
|
10
|
|
|
class Event |
|
11
|
|
|
{ |
|
12
|
|
|
public const DOCUMENT_START = 'document-start'; |
|
13
|
|
|
public const DOCUMENT_END = 'document-end'; |
|
14
|
|
|
public const OBJECT_START = 'object-start'; |
|
15
|
|
|
public const OBJECT_END = 'object-end'; |
|
16
|
|
|
public const ARRAY_START = 'array-start'; |
|
17
|
|
|
public const ARRAY_END = 'array-end'; |
|
18
|
|
|
public const KEY = 'key'; |
|
19
|
|
|
public const VALUE = 'value'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $id; |
|
23
|
|
|
/** @var mixed */ |
|
24
|
|
|
protected $value; |
|
25
|
|
|
/** @var Stack */ |
|
26
|
|
|
protected $stack; |
|
27
|
|
|
/** @var int */ |
|
28
|
|
|
protected $lineNumber; |
|
29
|
|
|
/** @var int */ |
|
30
|
|
|
protected $charNumber; |
|
31
|
|
|
|
|
32
|
52 |
|
public function __construct(string $id, $value, Stack $stack, int $lineNumber, int $charNumber) |
|
33
|
|
|
{ |
|
34
|
52 |
|
$this->id = $id; |
|
35
|
52 |
|
$this->value = $value; |
|
36
|
52 |
|
$this->stack = $stack; |
|
37
|
52 |
|
$this->lineNumber = $lineNumber; |
|
38
|
52 |
|
$this->charNumber = $charNumber; |
|
39
|
52 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
52 |
|
public function getId(): string |
|
45
|
|
|
{ |
|
46
|
52 |
|
return $this->id; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
50 |
|
public function getValue() |
|
53
|
|
|
{ |
|
54
|
50 |
|
return $this->value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function getDepth(): int |
|
61
|
|
|
{ |
|
62
|
4 |
|
return $this->stack->getDepth(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
18 |
|
public function getPath(): string |
|
69
|
|
|
{ |
|
70
|
18 |
|
$path = ''; |
|
71
|
18 |
|
foreach ($this->stack->frames() as $frame) { |
|
72
|
14 |
|
if ($frame->isArray()) { |
|
73
|
6 |
|
$path .= '[' . $frame->getLastKey() . ']'; |
|
74
|
13 |
|
} elseif ($frame->isObject()) { |
|
75
|
13 |
|
if (!empty($path)) { |
|
76
|
5 |
|
$path .= '.'; |
|
77
|
|
|
} |
|
78
|
13 |
|
$path .= $frame->getLastKey(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
18 |
|
return $path; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $selector |
|
87
|
|
|
* @return bool |
|
88
|
|
|
* @throws Exception\SelectorException |
|
89
|
|
|
*/ |
|
90
|
21 |
|
public function matchPath(string $selector) |
|
91
|
|
|
{ |
|
92
|
21 |
|
return Selector::create($selector)->match($this->stack); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getLineNumber(): int |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->lineNumber; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getCharNumber(): int |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->charNumber; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|