Repository navigation
Expand file tree
/
Copy pathDump.php
More file actions
149 lines (125 loc) · 4.14 KB
/
Copy pathDump.php
File metadata and controls
149 lines (125 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Helper;
use function addslashes;
use function array_is_list;
use function get_debug_type;
use function gettype;
use function is_array;
use function is_scalar;
use function str_repeat;
use function var_export;
/**
* Renders JSON-safe values as display or parsable strings for the debugger panels.
*/
final class Dump
{
/**
* Renders a value as a display string: quoted strings, bare scalars, and 4-space-indented arrays without trailing
* commas.
*
* @param mixed $value JSON-safe value to render.
* @param int $depth Maximum nesting level rendered before collapsing to `[...]`.
*
* @return string Display string.
*/
public static function asString(mixed $value, int $depth = 10): string
{
return self::dumpInternal($value, $depth, 0);
}
/**
* Renders a value as a parsable PHP expression: `var_export()` scalars and short-syntax arrays with trailing
* commas, omitting sequential integer keys.
*
* @param mixed $value JSON-safe value to render.
*
* @return string Parsable PHP expression.
*/
public static function export(mixed $value): string
{
return self::exportInternal($value, 0);
}
/**
* Renders an array as an indented display block, or `[...]` once the depth budget is exhausted.
*
* @param array<array-key, mixed> $value Array to render.
* @param int $depth Maximum nesting level.
* @param int $level Current nesting level.
*
* @return string Display block for the array.
*/
private static function dumpArray(array $value, int $depth, int $level): string
{
if ($depth <= $level) {
return '[...]';
}
if ($value === []) {
return '[]';
}
$spaces = str_repeat(' ', $level * 4);
$output = '[';
foreach ($value as $key => $item) {
$output .= "\n{$spaces} ";
$output .= self::dumpInternal($key, $depth, $level);
$output .= ' => ';
$output .= self::dumpInternal($item, $depth, $level + 1);
}
return "{$output}\n{$spaces}]";
}
/**
* Renders one value of any type at the current nesting level.
*
* @param mixed $value JSON-safe value to render.
* @param int $depth Maximum nesting level.
* @param int $level Current nesting level.
*
* @return string Display string for the value.
*/
private static function dumpInternal(mixed $value, int $depth, int $level): string
{
return match (gettype($value)) {
'boolean' => $value ? 'true' : 'false',
'integer', 'double' => (string) $value,
'string' => "'" . addslashes($value) . "'",
'NULL' => 'null',
'array' => self::dumpArray($value, $depth, $level),
default => '{' . get_debug_type($value) . '}',
};
}
/**
* Renders one value as a parsable PHP expression at the current nesting level.
*
* @param mixed $value JSON-safe value to render.
* @param int $level Current nesting level.
*
* @return string Parsable expression for the value.
*/
private static function exportInternal(mixed $value, int $level): string
{
if ($value === null) {
return 'null';
}
if (is_array($value)) {
if ($value === []) {
return '[]';
}
$outputKeys = array_is_list($value) === false;
$spaces = str_repeat(' ', $level * 4);
$output = '[';
foreach ($value as $key => $item) {
$output .= "\n{$spaces} ";
if ($outputKeys) {
$output .= self::exportInternal($key, $level);
$output .= ' => ';
}
$output .= self::exportInternal($item, $level + 1);
$output .= ',';
}
return "{$output}\n{$spaces}]";
}
if (is_scalar($value)) {
return var_export($value, true);
}
return '{' . get_debug_type($value) . '}';
}
}