diff --git a/src/FacadeTrait.php b/src/FacadeTrait.php new file mode 100644 index 0000000..dbd8057 --- /dev/null +++ b/src/FacadeTrait.php @@ -0,0 +1,168 @@ +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; + } +} diff --git a/src/Items.php b/src/Items.php index 37f2d10..5749f8a 100644 --- a/src/Items.php +++ b/src/Items.php @@ -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; } } diff --git a/src/RecursiveItems.php b/src/RecursiveItems.php new file mode 100644 index 0000000..3d470ce --- /dev/null +++ b/src/RecursiveItems.php @@ -0,0 +1,18 @@ +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); - } - } - } }