1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Code\Printer; |
5
|
|
|
|
6
|
|
|
use Nette\PhpGenerator\PhpLiteral; |
7
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
8
|
|
|
use SlayerBirden\DFCodeGeneration\Util\ArrayUtils; |
9
|
|
|
|
10
|
|
|
final class NsArrayPrinter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var PhpNamespace |
14
|
|
|
*/ |
15
|
|
|
private $phpNamespace; |
16
|
|
|
|
17
|
15 |
|
public function __construct(PhpNamespace $phpNamespace) |
18
|
|
|
{ |
19
|
15 |
|
$this->phpNamespace = $phpNamespace; |
20
|
15 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Prints array with short tags |
24
|
|
|
* |
25
|
|
|
* @param array $list |
26
|
|
|
* @param int $indentationLevel |
27
|
|
|
* @param string $closingChar |
28
|
|
|
* @param null|mixed $key |
29
|
|
|
* @param bool $shouldShowPrefixBracket |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
15 |
|
public function printArray( |
33
|
|
|
array $list, |
34
|
|
|
int $indentationLevel = 1, |
35
|
|
|
string $closingChar = ",\n", |
36
|
|
|
$key = null, |
37
|
|
|
$shouldShowPrefixBracket = true |
38
|
|
|
): string { |
39
|
15 |
|
$parts = []; |
40
|
15 |
|
$this->iterateArray($list, $parts, $indentationLevel); |
41
|
|
|
|
42
|
15 |
|
$bracketsSpaces = $this->getSpaces(($indentationLevel - 1) * 4); |
43
|
15 |
|
if ($key === null) { |
44
|
15 |
|
$keyPart = ''; |
45
|
|
|
} else { |
46
|
10 |
|
$keyPart = $this->varExport($key) . ' => '; |
47
|
|
|
} |
48
|
|
|
|
49
|
15 |
|
if ($shouldShowPrefixBracket) { |
50
|
15 |
|
$prefixBracket = $bracketsSpaces; |
51
|
|
|
} else { |
52
|
4 |
|
$prefixBracket = ''; |
53
|
|
|
} |
54
|
|
|
|
55
|
15 |
|
return $prefixBracket . $keyPart . "[\n" . implode("\n", $parts) . "\n" . $bracketsSpaces . ']' . $closingChar; |
56
|
|
|
} |
57
|
|
|
|
58
|
15 |
|
private function iterateArray(array $list, array &$parts, int$indentationLevel): void |
59
|
|
|
{ |
60
|
15 |
|
$isList = ArrayUtils::isSequential($list); |
61
|
15 |
|
foreach ($list as $itemKey => $item) { |
62
|
15 |
|
if (is_array($item)) { |
63
|
12 |
|
if ($isList) { |
64
|
12 |
|
$itemKey = null; |
65
|
|
|
} |
66
|
12 |
|
$parts[] = $this->printArray($item, $indentationLevel + 1, ',', $itemKey); |
67
|
|
|
} else { |
68
|
15 |
|
$spacesCount = $indentationLevel * 4; |
69
|
15 |
|
if ($isList) { |
70
|
15 |
|
$keyLiteral = ''; |
71
|
|
|
} else { |
72
|
12 |
|
$keyLiteral = $this->varExport($itemKey) . ' => '; |
73
|
|
|
} |
74
|
15 |
|
$parts[] = $this->getSpaces($spacesCount) . $keyLiteral . $this->varExport($item) . ','; |
75
|
|
|
} |
76
|
|
|
} |
77
|
15 |
|
} |
78
|
|
|
|
79
|
15 |
|
private function getSpaces($count): string |
80
|
|
|
{ |
81
|
15 |
|
$spaces = ''; |
82
|
15 |
|
while ($count > 0) { |
83
|
15 |
|
$spaces .= ' '; |
84
|
15 |
|
$count -= 1; |
85
|
|
|
} |
86
|
|
|
|
87
|
15 |
|
return $spaces; |
88
|
|
|
} |
89
|
|
|
|
90
|
15 |
|
private function varExport($variable): string |
91
|
|
|
{ |
92
|
15 |
|
if ($variable instanceof PhpLiteral) { |
93
|
11 |
|
if (strpos((string)$variable, '::class') !== false) { |
94
|
10 |
|
$className = str_replace('::class', '', (string)$variable); |
95
|
10 |
|
return $this->phpNamespace->unresolveName($className) . '::class'; |
96
|
|
|
} else { |
97
|
11 |
|
return (string)$variable; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
// firs check for ::class strings |
101
|
15 |
|
if (is_string($variable) && (strpos($variable, '::class') !== false)) { |
102
|
11 |
|
$className = str_replace('::class', '', $variable); |
103
|
11 |
|
return $this->phpNamespace->unresolveName($className) . '::class'; |
104
|
|
|
} |
105
|
|
|
// then checks for backslash |
106
|
|
|
// also check that the string does not contain not allowed characters |
107
|
14 |
|
if (is_string($variable) |
108
|
14 |
|
&& (strpos($variable, '\\') !== false) |
109
|
14 |
|
&& preg_match('/^[\w\\\]+$/', $variable)) { |
110
|
10 |
|
return $this->phpNamespace->unresolveName($variable) . '::class'; |
111
|
|
|
} |
112
|
14 |
|
return stripslashes(var_export($variable, true)); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|