Skip to content

Commit

Permalink
Merge pull request #2 from ixnode/issue/1
Browse files Browse the repository at this point in the history
Issue/1
  • Loading branch information
bjoern-hempel authored Sep 14, 2021
2 parents 17310f1 + c938a4a commit 319efef
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.1.1
7 changes: 4 additions & 3 deletions src/Utils/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function beautify(): string
$jsonObject = json_decode($this->json);

// Beautify the given JSON.
$json = json_encode($jsonObject, JSON_PRETTY_PRINT);
$json = json_encode($jsonObject, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

if ($json === false) {
throw new Exception('Unable to encode given object.');
Expand All @@ -97,8 +97,9 @@ public function beautify(): string
*/
static protected function reformatIndent(string $json, int $indent = self::OPTION_INDENT): string
{
$formattedJson = preg_replace_callback('/^ +/m', function () use ($indent) {
return str_repeat(' ', $indent);
$formattedJson = preg_replace_callback('/^([ ])+/m', function (array $matches) use ($indent) {
$indentNumber = intval(strlen($matches[0]) / 4);
return str_repeat(' ', $indent * $indentNumber);
}, $json);

if ($formattedJson === null) {
Expand Down
42 changes: 28 additions & 14 deletions tests/Unit/Utils/JsonFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class JsonFormatterTest extends WebTestCase
* Test wrapper.
*
* @test
* @testdox 1) Test JsonFormatter class ($title).
* @testdox $number) Test JsonFormatter class ($title).
* @dataProvider dataProvider
* @param int $number
* @param string $title
Expand Down Expand Up @@ -95,38 +95,52 @@ public function dataProvider(): array
return [

/**
* 1) Empty JSON
* 1) Empty JSON's
*/
[++$number, 'Simple JSON', '{}', '{}', ],

/**
* 2) Empty JSON
*/
[++$number, 'Simple JSON', '{
[++$number, 'Empty JSON', '{}', '{}', ],
[++$number, 'Empty JSON', '{
}', '{}', ],

/**
* 3) Invalid JSON
* 2) Invalid JSON's
*/
[++$number, 'Simple JSON', '{":123}', '{}', JsonFormatter::OPTION_INDENT, InvalidJsonGivenException::class, InvalidJsonGivenException::ERROR_CODE, InvalidJsonGivenException::ERROR_MESSAGE],
[++$number, 'Invalid JSON', '{":123}', '{}', JsonFormatter::OPTION_INDENT, InvalidJsonGivenException::class, InvalidJsonGivenException::ERROR_CODE, InvalidJsonGivenException::ERROR_MESSAGE],

/**
* 4) Simple JSON (indent: 4)
* 3) Simple JSON's (indent: 4)
*/
[++$number, 'Simple JSON', '{"key1": "value1", "key2": "value2", "key3": "value3"}', '{
[++$number, 'Simple JSON (indent: 4)', '{"key1": "value1", "key2": "value2", "key3": "value3"}', '{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}', ],
[++$number, 'Simple JSON (indent: 4)', '{"key1": "value1", "key2": "value2", "key3": [1, 2, 3]}', '{
"key1": "value1",
"key2": "value2",
"key3": [
1,
2,
3
]
}', ],

/**
* 5) Simple JSON (indent: 2)
* 4) Simple JSON's (indent: 2)
*/
[++$number, 'Simple JSON', '{"key1": "value1", "key2": "value2", "key3": "value3"}', '{
[++$number, 'Simple JSON (indent: 2)', '{"key1": "value1", "key2": "value2", "key3": "value3"}', '{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}', 2, ],
[++$number, 'Simple JSON (indent: 2)', '{"key1": "value1", "key2": "value2", "key3": [1, 2, 3]}', '{
"key1": "value1",
"key2": "value2",
"key3": [
1,
2,
3
]
}', 2, ],
];
}
Expand Down

0 comments on commit 319efef

Please sign in to comment.