-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Printer.php
174 lines (146 loc) · 5.2 KB
/
Printer.php
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
/**
* Copyright (c) 2018-2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/json-printer
*/
namespace Ergebnis\Json\Printer;
final class Printer implements PrinterInterface
{
/**
* This code is adopted from composer/composer (originally licensed under MIT by Nils Adermann <[email protected]>
* and Jordi Boggiano <[email protected]>), who adopted it from a blog post by Dave Perrett (originally licensed
* under MIT by Dave Perrett <[email protected]>).
*
* The primary objective of the adoption is
*
* - turn static method into an instance method
* - allow to specify indent
* - allow to specify new-line character sequence
*
* If you observe closely, the options for un-escaping unicode characters and slashes have been removed. Since this
* package requires PHP 7, there is no need to implement this in user-land code.
*
* @see https://github.com/composer/composer/blob/1.6.0/src/Composer/Json/JsonFormatter.php#L25-L126
* @see https://www.daveperrett.com/articles/2008/03/11/format-json-with-php/
* @see http://php.net/manual/en/function.json-encode.php
* @see http://php.net/manual/en/json.constants.php
*
* @throws \InvalidArgumentException
*/
public function print(
string $json,
string $indent = ' ',
string $newLine = \PHP_EOL
): string {
try {
\json_decode(
$json,
false,
512,
\JSON_THROW_ON_ERROR,
);
} catch (\JsonException $exception) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not valid JSON.',
$json,
));
}
if (1 !== \preg_match('/^( +|\t+)$/', $indent)) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not a valid indent.',
$indent,
));
}
if (1 !== \preg_match('/^(?>\r\n|\n|\r)$/', $newLine)) {
throw new \InvalidArgumentException(\sprintf(
'"%s" is not a valid new-line character sequence.',
$newLine,
));
}
$printed = '';
$indentLevel = 0;
$length = \mb_strlen($json);
$withinStringLiteral = false;
$stringLiteral = '';
$noEscape = true;
for ($i = 0; $i < $length; ++$i) {
/**
* Grab the next character in the string.
*/
$character = \mb_substr($json, $i, 1);
/**
* Are we inside a quoted string literal?
*/
if ('"' === $character && $noEscape) {
$withinStringLiteral = !$withinStringLiteral;
}
/**
* Collect characters if we are inside a quoted string literal.
*/
if ($withinStringLiteral) {
$stringLiteral .= $character;
$noEscape = '\\' === $character ? !$noEscape : true;
continue;
}
/**
* Process string literal if we are about to leave it.
*/
if ('' !== $stringLiteral) {
$printed .= $stringLiteral . $character;
$stringLiteral = '';
continue;
}
/**
* Ignore whitespace outside of string literal.
*/
if ('' === \trim($character)) {
continue;
}
/**
* Ensure space after ":" character.
*/
if (':' === $character) {
$printed .= ': ';
continue;
}
/**
* Output a new line after "," character and indent the next line.
*/
if (',' === $character) {
$printed .= $character . $newLine . \str_repeat($indent, $indentLevel);
continue;
}
/**
* Output a new line after "{" and "[" and indent the next line.
*/
if ('{' === $character || '[' === $character) {
++$indentLevel;
$printed .= $character . $newLine . \str_repeat($indent, $indentLevel);
continue;
}
/**
* Output a new line after "}" and "]" and indent the next line.
*/
if ('}' === $character || ']' === $character) {
--$indentLevel;
$trimmed = \rtrim($printed);
$previousNonWhitespaceCharacter = \mb_substr($trimmed, -1);
/**
* Collapse empty {} and [].
*/
if ('{' === $previousNonWhitespaceCharacter || '[' === $previousNonWhitespaceCharacter) {
$printed = $trimmed . $character;
continue;
}
$printed .= $newLine . \str_repeat($indent, $indentLevel);
}
$printed .= $character;
}
return $printed;
}
}