mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-01-17 04:58:15 +01:00
Introduce lazy pointers
This commit is contained in:
parent
f930423627
commit
8a69cabfd8
@ -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<string, Closure> $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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user