Refine callable pointers

This commit is contained in:
Andrea Marco Sartori 2023-01-13 23:57:12 +10:00
parent 6d731cdb51
commit 45ed8e9650
3 changed files with 18 additions and 5 deletions

View File

@ -67,10 +67,11 @@ final class Parser implements IteratorAggregate
continue; continue;
} }
if ($this->state->hasBuffer() && $this->state->inObject()) { if ($this->state->hasBuffer()) {
yield $this->decoder->decode($this->state->key()) => $this->decoder->decode($this->state->value()); $key = $this->decoder->decode($this->state->key());
} elseif ($this->state->hasBuffer() && !$this->state->inObject()) { $value = $this->decoder->decode($this->state->value());
yield $this->decoder->decode($this->state->value());
yield $key => $this->state->callPointer($value, $key);
} }
if ($this->state->canStopParsing()) { if ($this->state->canStopParsing()) {

View File

@ -100,7 +100,7 @@ final class Pointer implements Stringable
*/ */
public function call(mixed $value, mixed $key): mixed public function call(mixed $value, mixed $key): mixed
{ {
return call_user_func($this->callback, $value, $key); return call_user_func($this->callback, $value, $key) ?? $value;
} }
/** /**

View File

@ -124,6 +124,18 @@ final class State
return $this->pointers->wereFound() && !$this->pointer->includesTree($this->tree); return $this->pointers->wereFound() && !$this->pointer->includesTree($this->tree);
} }
/**
* Call the current pointer callback
*
* @param mixed $value
* @param mixed $key
* @return mixed
*/
public function callPointer(mixed $value, mixed $key): mixed
{
return $this->pointer->call($value, $key);
}
/** /**
* Mutate state depending on the given token * Mutate state depending on the given token
* *