1 | <?php |
||
20 | abstract class JsonReader implements JsonReadable |
||
21 | { |
||
22 | /** |
||
23 | * A stack representing the next element to be consumed |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $stack = []; |
||
28 | |||
29 | /** |
||
30 | * An array of types that map to the position in the stack |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $stackTypes = []; |
||
35 | |||
36 | /** |
||
37 | * The current size of the stack |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $stackSize = 0; |
||
42 | |||
43 | /** |
||
44 | * An array of path names that correspond to the current stack |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $pathNames = []; |
||
49 | |||
50 | /** |
||
51 | * An array of path indicies that correspond to the current stack. This array could contain invalid |
||
52 | * values at indexes outside the current stack. It could also contain incorrect values at indexes |
||
53 | * where a path name is used. Data should only be fetched by referencing the current position in the stack. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $pathIndices = []; |
||
58 | |||
59 | /** |
||
60 | * A cache of the current [@see JsonToken]. This should get nulled out |
||
61 | * whenever a new token should be returned with the subsequent call |
||
62 | * to [@see JsonDecodeReader::peek] |
||
63 | * |
||
64 | * @var int|null |
||
65 | */ |
||
66 | protected $currentToken; |
||
67 | |||
68 | /** |
||
69 | * The original payload |
||
70 | * |
||
71 | * @var mixed |
||
72 | */ |
||
73 | protected $payload; |
||
74 | |||
75 | /** |
||
76 | * Returns an enum representing the type of the next token without consuming it |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | abstract public function peek(): string; |
||
81 | |||
82 | /** |
||
83 | * Get the current read path in json xpath format |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | abstract public function getPath(): string; |
||
88 | |||
89 | /** |
||
90 | * Push an element onto the stack |
||
91 | * |
||
92 | * @param JsonElement|Iterator $element |
||
93 | * @param string|null $type |
||
94 | * @return void |
||
95 | */ |
||
96 | abstract protected function push($element, $type = null): void; |
||
97 | |||
98 | /** |
||
99 | * Consumes the next token and asserts it's the end of an array |
||
100 | * |
||
101 | * @return void |
||
102 | * @throws \Tebru\Gson\Exception\JsonSyntaxException If the next token is not END_ARRAY |
||
103 | */ |
||
104 | 13 | public function endArray(): void |
|
111 | |||
112 | /** |
||
113 | * Consumes the next token and asserts it's the end of an object |
||
114 | * |
||
115 | * @return void |
||
116 | * @throws \Tebru\Gson\Exception\JsonSyntaxException If the next token is not END_OBJECT |
||
117 | */ |
||
118 | 12 | public function endObject(): void |
|
125 | |||
126 | /** |
||
127 | * Returns true if the array or object has another element |
||
128 | * |
||
129 | * If the current scope is not an array or object, this returns false |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | 11 | public function hasNext(): bool |
|
139 | |||
140 | /** |
||
141 | * Consumes the next name and returns it |
||
142 | * |
||
143 | * @return string |
||
144 | * @throws \Tebru\Gson\Exception\JsonSyntaxException If the next token is not NAME |
||
145 | */ |
||
146 | 26 | public function nextName(): string |
|
162 | |||
163 | /** |
||
164 | * Consumes the value of the next token and asserts it's null |
||
165 | * |
||
166 | * @return null |
||
167 | * @throws \Tebru\Gson\Exception\JsonSyntaxException If the next token is not NAME or NULL |
||
168 | */ |
||
169 | 4 | public function nextNull() |
|
179 | |||
180 | /** |
||
181 | * Skip the next value. If the next value is an object or array, all children will |
||
182 | * also be skipped. |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | 2 | public function skipValue(): void |
|
190 | |||
191 | /** |
||
192 | * Returns the original json after json_decode |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | 2 | public function getPayload() |
|
200 | |||
201 | /** |
||
202 | * Pop the last element off the stack and return it |
||
203 | * |
||
204 | * @return JsonElement|Iterator|mixed |
||
205 | */ |
||
206 | 106 | protected function pop() |
|
214 | |||
215 | /** |
||
216 | * Check that the next token equals the expectation |
||
217 | * |
||
218 | * @param string $expectedToken |
||
219 | * @throws \Tebru\Gson\Exception\JsonSyntaxException If the next token is not the expectation |
||
220 | */ |
||
221 | 117 | protected function expect(string $expectedToken) |
|
235 | |||
236 | /** |
||
237 | * Increment the path index. This should be called any time a new value is parsed. |
||
238 | */ |
||
239 | 82 | protected function incrementPathIndex(): void |
|
249 | } |
||
250 |