|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\Schema\Renderer\PhpFileRenderer\Exporter\Rendering; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Schema\Renderer\PhpFileRenderer\Exporter\ExporterItem; |
|
8
|
|
|
|
|
9
|
|
|
final class ArrayRenderer |
|
10
|
|
|
{ |
|
11
|
|
|
public static function render(array $value, int $indentLevel = 0): string |
|
12
|
|
|
{ |
|
13
|
|
|
$aiKeys = self::isAutoIncrementedKeys($value); |
|
14
|
|
|
$inline = $aiKeys && self::isScalarArrayValues($value); |
|
15
|
|
|
if ($inline) { |
|
16
|
|
|
$result = self::renderArrayInline($value, !$aiKeys); |
|
17
|
|
|
if (strlen($result) <= ExporterItem::MAX_LINE_LENGTH) { |
|
18
|
|
|
return $result; |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
return self::renderArrayBlock($value, !$aiKeys, $indentLevel); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param mixed $value |
|
26
|
|
|
*/ |
|
27
|
|
|
private static function renderValue($value, bool $wrapValue = true, int $indentLevel = 0): string |
|
28
|
|
|
{ |
|
29
|
|
|
return ValueRenderer::render($value, $wrapValue, $indentLevel); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private static function renderArrayInline(array $value, bool $withKeys = true): string |
|
33
|
|
|
{ |
|
34
|
|
|
$elements = []; |
|
35
|
|
|
foreach ($value as $key => $item) { |
|
36
|
|
|
$str = ''; |
|
37
|
|
|
if (!$item instanceof self && $withKeys) { |
|
38
|
|
|
$str .= is_int($key) ? "{$key} => " : "'{$key}' => "; |
|
39
|
|
|
} |
|
40
|
|
|
$elements[] = $str . self::renderValue($item); |
|
41
|
|
|
} |
|
42
|
|
|
return '[' . \implode(', ', $elements) . ']'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private static function renderArrayBlock(array $value, bool $withKeys = true, int $indentLevel = 0): string |
|
46
|
|
|
{ |
|
47
|
|
|
$braceIdent = \str_repeat(Indentable::INDENT, $indentLevel); |
|
48
|
|
|
$itemIndent = \str_repeat(Indentable::INDENT, $indentLevel + 1); |
|
49
|
|
|
$result = '['; |
|
50
|
|
|
foreach ($value as $key => $item) { |
|
51
|
|
|
$result .= "\n" . $itemIndent; |
|
52
|
|
|
if ($item instanceof Indentable) { |
|
53
|
|
|
$item->setIndentLevel($indentLevel + 1); |
|
54
|
|
|
} elseif ($withKeys) { |
|
55
|
|
|
$result .= is_int($key) ? "{$key} => " : "'{$key}' => "; |
|
56
|
|
|
} |
|
57
|
|
|
$result .= self::renderValue($item, true, $indentLevel + 1) . ','; |
|
58
|
|
|
} |
|
59
|
|
|
return $result . "\n$braceIdent]"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private static function isAutoIncrementedKeys(array $array): bool |
|
63
|
|
|
{ |
|
64
|
|
|
return count($array) === 0 || array_keys($array) === range(0, count($array) - 1); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private static function isScalarArrayValues(array $array): bool |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($array as $value) { |
|
70
|
|
|
if (!is_scalar($value)) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|