Skip to content

Commit bd3a77e

Browse files
committed
simplify prettying
Signed-off-by: Klein Florian <florian.klein@free.fr>
1 parent 284bfec commit bd3a77e

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# json-chunks
22

33
```php
4-
$chunks = (new DocteurKlein\JsonChunks\Encode)([
4+
$chunks = DocteurKlein\JsonChunks\Encode::from([
55
'_links' => [
66
'product' => function() {
77
yield from [1, 2, 3];

spec/EncodeSpec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class EncodeSpec extends ObjectBehavior
1010
{
1111
function it_is_valid_json()
1212
{
13-
$actual = $this([
13+
$actual = $this::from([
1414
'_links' => [
1515
'product' => function() {
1616
yield from [1, 2, 3];
@@ -55,7 +55,7 @@ function it_is_valid_json()
5555
function it_allows_empty_generators()
5656
{
5757
$i = new class implements \IteratorAggregate {function getIterator(){return (function(){yield from [];})();}};
58-
$actual = $this([
58+
$actual = $this::from([
5959
'sub' => function() use($i) {
6060
yield from $i;
6161
},
@@ -74,7 +74,7 @@ function jsonSerialize() {
7474
return [(function(){yield from ['from-obj'];})()];
7575
}
7676
};
77-
$actual = $this([
77+
$actual = $this::from([
7878
'sub' => function() use($obj) {
7979
yield from [$obj];
8080
},

src/Encode.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ final class Encode
99
private const ARRAY_START = '[';
1010
private const ARRAY_END = ']';
1111

12-
public function __invoke(iterable $schema, int $level = 0): \Generator
12+
public static function from(iterable $schema, bool $pretty = false): \Generator
1313
{
14+
if ($pretty) {
15+
yield from self::pretty($schema);
16+
return;
17+
}
18+
1419
$key = $schema instanceof \Iterator ? $schema->key() : key($schema);
1520
if (null === $key) {
1621
yield '[]';
1722
return;
1823
}
1924
$isHash = is_string($key);
2025

21-
yield ($isHash ? self::HASH_START : self::ARRAY_START).PHP_EOL;
26+
yield ($isHash ? self::HASH_START : self::ARRAY_START);
2227

23-
$indentation = str_repeat(' ', $level);
2428
$isFirst = true;
2529
foreach ($schema as $key => $child) {
26-
$comma = $isFirst ? '' : ', '.PHP_EOL;
27-
yield $comma.$indentation;
30+
yield $isFirst ? '' : ', ';
2831
$isFirst = false;
2932

3033
if ($isHash) {
@@ -35,18 +38,23 @@ public function __invoke(iterable $schema, int $level = 0): \Generator
3538
$child = $child();
3639
}
3740
if (is_iterable($child)) {
38-
yield from ($this)($child, $level + 1);
41+
yield from self::from($child);
3942
continue;
4043
}
4144
if (is_object($child)) {
4245
if ($child instanceof \JsonSerializable) {
43-
yield from ($this)($child->jsonSerialize(), $level + 1);
46+
yield from self::from($child->jsonSerialize());
4447
continue;
4548
}
4649
}
4750
yield json_encode($child).PHP_EOL;
4851
}
4952

50-
yield PHP_EOL.$indentation.($isHash ? self::HASH_END : self::ARRAY_END);
53+
yield ($isHash ? self::HASH_END : self::ARRAY_END);
54+
}
55+
56+
public static function pretty(iterable $schema): \Generator
57+
{
58+
yield from [json_encode(json_decode(implode(iterator_to_array(self::from($schema), false))), JSON_PRETTY_PRINT)];
5159
}
5260
}

0 commit comments

Comments
 (0)