1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-16 20:48:17 +01:00

RecursiveItems facade

This commit is contained in:
Filip Halaxa 2023-12-01 13:21:20 +01:00
parent 01fc434702
commit 754d3609c0
4 changed files with 190 additions and 164 deletions

168
src/FacadeTrait.php Normal file
View File

@ -0,0 +1,168 @@
<?php
declare(strict_types=1);
namespace JsonMachine;
use JsonMachine\Exception\InvalidArgumentException;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonDecoder\ItemDecoder;
trait FacadeTrait
{
/**
* @var iterable
*/
private $chunks;
/**
* @var string
*/
private $jsonPointer;
/**
* @var ItemDecoder|null
*/
private $jsonDecoder;
/**
* @var Parser
*/
private $parser;
/**
* @var bool
*/
private $debugEnabled;
/**
* @todo Make private when PHP 7 stops being supported
*/
protected abstract function recursive(): bool;
/**
* @param iterable $bytesIterator
*
* @throws InvalidArgumentException
*/
public function __construct($bytesIterator, array $options = [])
{
$options = new ItemsOptions($options);
$this->chunks = $bytesIterator;
$this->jsonPointer = $options['pointer'];
$this->jsonDecoder = $options['decoder'];
$this->debugEnabled = $options['debug'];
if ($this->debugEnabled) {
$tokensClass = TokensWithDebugging::class;
} else {
$tokensClass = Tokens::class;
}
$this->parser = new Parser(
new $tokensClass(
$this->chunks
),
$this->jsonPointer,
$this->jsonDecoder ?: new ExtJsonDecoder(),
$this->recursive()
);
}
/**
* @param string $string
*
* @return self
*
* @throws InvalidArgumentException
*/
public static function fromString($string, array $options = [])
{
return new self(new StringChunks($string), $options);
}
/**
* @param string $file
*
* @return self
*
* @throws Exception\InvalidArgumentException
*/
public static function fromFile($file, array $options = [])
{
return new self(new FileChunks($file), $options);
}
/**
* @param resource $stream
*
* @return self
*
* @throws Exception\InvalidArgumentException
*/
public static function fromStream($stream, array $options = [])
{
return new self(new StreamChunks($stream), $options);
}
/**
* @param iterable $iterable
*
* @return self
*
* @throws Exception\InvalidArgumentException
*/
public static function fromIterable($iterable, array $options = [])
{
return new self($iterable, $options);
}
/**
* @return \Generator
*
* @throws Exception\PathNotFoundException
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return $this->parser->getIterator();
}
/**
* @throws Exception\JsonMachineException
*/
public function getPosition()
{
return $this->parser->getPosition();
}
public function getJsonPointers(): array
{
return $this->parser->getJsonPointers();
}
/**
* @throws Exception\JsonMachineException
*/
public function getCurrentJsonPointer(): string
{
return $this->parser->getCurrentJsonPointer();
}
/**
* @throws Exception\JsonMachineException
*/
public function getMatchedJsonPointer(): string
{
return $this->parser->getMatchedJsonPointer();
}
/**
* @return bool
*/
public function isDebugEnabled()
{
return $this->debugEnabled;
}
}

View File

@ -4,163 +4,15 @@ declare(strict_types=1);
namespace JsonMachine;
use JsonMachine\Exception\InvalidArgumentException;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonDecoder\ItemDecoder;
/**
* Entry-point facade for JSON Machine.
*/
final class Items implements \IteratorAggregate, PositionAware
{
/**
* @var iterable
*/
private $chunks;
use FacadeTrait;
/**
* @var string
*/
private $jsonPointer;
/**
* @var ItemDecoder|null
*/
private $jsonDecoder;
/**
* @var Parser
*/
private $parser;
/**
* @var bool
*/
private $debugEnabled;
/**
* @param iterable $bytesIterator
*
* @throws InvalidArgumentException
*/
public function __construct($bytesIterator, array $options = [])
protected function recursive(): bool
{
$options = new ItemsOptions($options);
$this->chunks = $bytesIterator;
$this->jsonPointer = $options['pointer'];
$this->jsonDecoder = $options['decoder'];
$this->debugEnabled = $options['debug'];
if ($this->debugEnabled) {
$tokensClass = TokensWithDebugging::class;
} else {
$tokensClass = Tokens::class;
}
$this->parser = new Parser(
new $tokensClass(
$this->chunks
),
$this->jsonPointer,
$this->jsonDecoder ?: new ExtJsonDecoder(),
$options['recursive']
);
}
/**
* @param string $string
*
* @return self
*
* @throws InvalidArgumentException
*/
public static function fromString($string, array $options = [])
{
return new self(new StringChunks($string), $options);
}
/**
* @param string $file
*
* @return self
*
* @throws Exception\InvalidArgumentException
*/
public static function fromFile($file, array $options = [])
{
return new self(new FileChunks($file), $options);
}
/**
* @param resource $stream
*
* @return self
*
* @throws Exception\InvalidArgumentException
*/
public static function fromStream($stream, array $options = [])
{
return new self(new StreamChunks($stream), $options);
}
/**
* @param iterable $iterable
*
* @return self
*
* @throws Exception\InvalidArgumentException
*/
public static function fromIterable($iterable, array $options = [])
{
return new self($iterable, $options);
}
/**
* @return \Generator
*
* @throws Exception\PathNotFoundException
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return $this->parser->getIterator();
}
/**
* @throws Exception\JsonMachineException
*/
public function getPosition()
{
return $this->parser->getPosition();
}
public function getJsonPointers(): array
{
return $this->parser->getJsonPointers();
}
/**
* @throws Exception\JsonMachineException
*/
public function getCurrentJsonPointer(): string
{
return $this->parser->getCurrentJsonPointer();
}
/**
* @throws Exception\JsonMachineException
*/
public function getMatchedJsonPointer(): string
{
return $this->parser->getMatchedJsonPointer();
}
/**
* @return bool
*/
public function isDebugEnabled()
{
return $this->debugEnabled;
return false;
}
}

18
src/RecursiveItems.php Normal file
View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace JsonMachine;
/**
* Entry-point facade for recursive iteration.
*/
final class RecursiveItems implements \IteratorAggregate, PositionAware
{
use FacadeTrait;
protected function recursive(): bool
{
return true;
}
}

View File

@ -6,10 +6,10 @@ namespace JsonMachineTest;
use JsonMachine\Items;
use JsonMachine\JsonDecoder\PassThruDecoder;
use Traversable;
/**
* @covers \JsonMachine\Items
* @covers \JsonMachine\RecursiveItems
*/
class ItemsTest extends \PHPUnit_Framework_TestCase
{
@ -147,16 +147,4 @@ class ItemsTest extends \PHPUnit_Framework_TestCase
$this->assertSame(3, iterator_count($items));
}
public function testRecursiveIteration()
{
$items = Items::fromString('[[":)"]]', ['recursive' => true]);
foreach ($items as $emojis) {
$this->assertInstanceOf(Traversable::class, $emojis);
foreach ($emojis as $emoji) {
$this->assertSame(':)', $emoji);
}
}
}
}