Skip to content

Commit

Permalink
simplify prettying
Browse files Browse the repository at this point in the history
Signed-off-by: Klein Florian <florian.klein@free.fr>
  • Loading branch information
docteurklein committed Jun 5, 2018
1 parent 284bfec commit bd3a77e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
6 changes: 3 additions & 3 deletions spec/EncodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
},
Expand All @@ -74,7 +74,7 @@ function jsonSerialize() {
return [(function(){yield from ['from-obj'];})()];
}
};
$actual = $this([
$actual = $this::from([
'sub' => function() use($obj) {
yield from [$obj];
},
Expand Down
24 changes: 16 additions & 8 deletions src/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ 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 '[]';
return;
}
$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) {
Expand All @@ -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)];
}
}

0 comments on commit bd3a77e

Please sign in to comment.