diff --git a/src/JsonParser.php b/src/JsonParser.php index ccacee1..6c57a9a 100644 --- a/src/JsonParser.php +++ b/src/JsonParser.php @@ -64,6 +64,7 @@ final class JsonParser implements IteratorAggregate try { yield from $this->parser; } catch (SyntaxException $e) { + $e->setPosition($this->parser->position()); call_user_func($this->config->onSyntaxError, $e); } } @@ -92,7 +93,36 @@ final class JsonParser implements IteratorAggregate */ public function pointer(string $pointer, Closure $callback = null): self { - $this->config->pointers->add(new Pointer($pointer, $callback)); + $this->config->pointers->add(new Pointer($pointer, false, $callback)); + + return $this; + } + + /** + * Set the lazy JSON pointers + * + * @param string[]|array $pointers + * @return self + */ + public function lazyPointers(array $pointers): self + { + foreach ($pointers as $pointer => $callback) { + $callback instanceof Closure ? $this->lazyPointer($pointer, $callback) : $this->lazyPointer($callback); + } + + return $this; + } + + /** + * Set a lazy JSON pointer + * + * @param string $pointer + * @param Closure|null $callback + * @return self + */ + public function lazyPointer(string $pointer, Closure $callback = null): self + { + $this->config->pointers->add(new Pointer($pointer, true, $callback)); return $this; } diff --git a/src/Pointers/Pointer.php b/src/Pointers/Pointer.php index b03d937..f0681ed 100644 --- a/src/Pointers/Pointer.php +++ b/src/Pointers/Pointer.php @@ -50,9 +50,10 @@ final class Pointer implements Stringable * Instantiate the class. * * @param string $pointer + * @param bool $isLazy * @param Closure|null $callback */ - public function __construct(private string $pointer, Closure $callback = null) + public function __construct(private string $pointer, private bool $isLazy = false, Closure $callback = null) { $this->referenceTokens = $this->toReferenceTokens(); $this->depth = count($this->referenceTokens); @@ -76,6 +77,16 @@ final class Pointer implements Stringable return array_slice($referenceTokens, 1); } + /** + * Determine whether the pointer is lazy + * + * @return bool + */ + public function isLazy(): bool + { + return $this->isLazy; + } + /** * Retrieve the reference tokens * @@ -148,6 +159,7 @@ final class Pointer implements Stringable */ public function includesTree(Tree $tree): bool { + // if ($this->pointer == '' && !$this->isLazy) { if ($this->pointer == '') { return true; }