From bd3a77e9464e84f6301e5cc4b2b4f718c536eb3f Mon Sep 17 00:00:00 2001 From: Klein Florian Date: Tue, 5 Jun 2018 23:43:11 +0200 Subject: [PATCH] simplify prettying Signed-off-by: Klein Florian --- README.md | 2 +- spec/EncodeSpec.php | 6 +++--- src/Encode.php | 24 ++++++++++++++++-------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index cc3f649..e323c70 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # json-chunks ```php -$chunks = (new DocteurKlein\JsonChunks\Encode)([ +$chunks = DocteurKlein\JsonChunks\Encode::from([ '_links' => [ 'product' => function() { yield from [1, 2, 3]; diff --git a/spec/EncodeSpec.php b/spec/EncodeSpec.php index 3529e57..001175a 100644 --- a/spec/EncodeSpec.php +++ b/spec/EncodeSpec.php @@ -10,7 +10,7 @@ class EncodeSpec extends ObjectBehavior { function it_is_valid_json() { - $actual = $this([ + $actual = $this::from([ '_links' => [ 'product' => function() { yield from [1, 2, 3]; @@ -55,7 +55,7 @@ function it_is_valid_json() function it_allows_empty_generators() { $i = new class implements \IteratorAggregate {function getIterator(){return (function(){yield from [];})();}}; - $actual = $this([ + $actual = $this::from([ 'sub' => function() use($i) { yield from $i; }, @@ -74,7 +74,7 @@ function jsonSerialize() { return [(function(){yield from ['from-obj'];})()]; } }; - $actual = $this([ + $actual = $this::from([ 'sub' => function() use($obj) { yield from [$obj]; }, diff --git a/src/Encode.php b/src/Encode.php index c23566a..d4c196e 100644 --- a/src/Encode.php +++ b/src/Encode.php @@ -9,8 +9,13 @@ final class Encode private const ARRAY_START = '['; private const ARRAY_END = ']'; - public function __invoke(iterable $schema, int $level = 0): \Generator + public static function from(iterable $schema, bool $pretty = false): \Generator { + if ($pretty) { + yield from self::pretty($schema); + return; + } + $key = $schema instanceof \Iterator ? $schema->key() : key($schema); if (null === $key) { yield '[]'; @@ -18,13 +23,11 @@ public function __invoke(iterable $schema, int $level = 0): \Generator } $isHash = is_string($key); - yield ($isHash ? self::HASH_START : self::ARRAY_START).PHP_EOL; + yield ($isHash ? self::HASH_START : self::ARRAY_START); - $indentation = str_repeat(' ', $level); $isFirst = true; foreach ($schema as $key => $child) { - $comma = $isFirst ? '' : ', '.PHP_EOL; - yield $comma.$indentation; + yield $isFirst ? '' : ', '; $isFirst = false; if ($isHash) { @@ -35,18 +38,23 @@ public function __invoke(iterable $schema, int $level = 0): \Generator $child = $child(); } if (is_iterable($child)) { - yield from ($this)($child, $level + 1); + yield from self::from($child); continue; } if (is_object($child)) { if ($child instanceof \JsonSerializable) { - yield from ($this)($child->jsonSerialize(), $level + 1); + yield from self::from($child->jsonSerialize()); continue; } } yield json_encode($child).PHP_EOL; } - yield PHP_EOL.$indentation.($isHash ? self::HASH_END : self::ARRAY_END); + yield ($isHash ? self::HASH_END : self::ARRAY_END); + } + + public static function pretty(iterable $schema): \Generator + { + yield from [json_encode(json_decode(implode(iterator_to_array(self::from($schema), false))), JSON_PRETTY_PRINT)]; } }