cerbero90 /
json-parser
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Cerbero\JsonParser\ValueObjects; |
||
| 4 | |||
| 5 | use Cerbero\JsonParser\Pointers\Pointers; |
||
| 6 | use Cerbero\JsonParser\Tokens\CompoundBegin; |
||
| 7 | use Cerbero\JsonParser\Tokens\Parser; |
||
| 8 | use Cerbero\JsonParser\Tokens\Token; |
||
| 9 | use Cerbero\JsonParser\Tokens\Tokens; |
||
| 10 | use Closure; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The JSON parsing state. |
||
| 14 | * |
||
| 15 | */ |
||
| 16 | final class State |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The JSON tree. |
||
| 20 | * |
||
| 21 | * @var Tree |
||
| 22 | */ |
||
| 23 | public readonly Tree $tree; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The JSON buffer. |
||
| 27 | * |
||
| 28 | * @var Parser|string |
||
| 29 | */ |
||
| 30 | private Parser|string $buffer = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Whether an object key is expected. |
||
| 34 | * |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | public bool $expectsKey = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The expected token. |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | public int $expectedToken = Tokens::COMPOUND_BEGIN; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Instantiate the class. |
||
| 48 | * |
||
| 49 | * @param Pointers $pointers |
||
| 50 | * @param Closure $lazyLoad |
||
| 51 | */ |
||
| 52 | 361 | public function __construct(private readonly Pointers $pointers, private readonly Closure $lazyLoad) |
|
| 53 | { |
||
| 54 | 361 | $this->tree = new Tree($pointers); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Determine whether the parser can stop parsing |
||
| 59 | * |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | 354 | public function canStopParsing(): bool |
|
| 63 | { |
||
| 64 | 354 | return $this->pointers->wereFoundInTree($this->tree); |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Call the current pointer callback |
||
| 69 | * |
||
| 70 | * @param mixed $value |
||
| 71 | * @param mixed $key |
||
| 72 | * @return mixed |
||
| 73 | */ |
||
| 74 | 264 | public function callPointer(mixed $value, mixed &$key): mixed |
|
| 75 | { |
||
| 76 | 264 | return $this->pointers->matching()->call($value, $key); |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Mutate state depending on the given token |
||
| 81 | * |
||
| 82 | * @param Token $token |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | 357 | public function mutateByToken(Token $token): void |
|
| 86 | { |
||
| 87 | 357 | $this->tree->traverseToken($token, $this->expectsKey); |
|
| 88 | |||
| 89 | 357 | if ($this->tree->isMatched() && ((!$this->expectsKey && $token->isValue()) || $this->tree->isDeep())) { |
|
| 90 | 265 | $pointer = $this->pointers->markAsFound(); |
|
| 91 | |||
| 92 | 265 | if ($token instanceof CompoundBegin && $pointer->isLazy) { |
|
| 93 | 34 | $this->buffer = ($this->lazyLoad)(); |
|
| 94 | 34 | $token->shouldLazyLoad = true; |
|
| 95 | } else { |
||
| 96 | /** @phpstan-ignore-next-line */ |
||
| 97 | 256 | $this->buffer .= $token; |
|
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | 357 | $token->mutateState($this); |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Determine whether the buffer contains tokens |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | 355 | public function hasBuffer(): bool |
|
| 110 | { |
||
| 111 | 355 | return $this->buffer != ''; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Retrieve the value from the buffer and reset it |
||
| 116 | * |
||
| 117 | * @return Parser|string |
||
| 118 | */ |
||
| 119 | 265 | public function value(): Parser|string |
|
| 120 | { |
||
| 121 | 265 | $buffer = $this->buffer; |
|
| 122 | |||
| 123 | 265 | $this->buffer = ''; |
|
| 124 | |||
| 125 | 265 | return $buffer; |
|
| 126 | } |
||
| 127 | } |
||
| 128 |