-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement JsonEncodeNormalizer
- Loading branch information
1 parent
8749041
commit ddfc7cc
Showing
3 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/json-normalizer | ||
*/ | ||
|
||
namespace Localheinz\Json\Normalizer; | ||
|
||
final class JsonEncodeNormalizer implements NormalizerInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $jsonEncodeOptions; | ||
|
||
/** | ||
* @param int $jsonEncodeOptions | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct(int $jsonEncodeOptions) | ||
{ | ||
if (0 > $jsonEncodeOptions) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'"%s" is not valid options for json_encode().', | ||
$jsonEncodeOptions | ||
)); | ||
} | ||
$this->jsonEncodeOptions = $jsonEncodeOptions; | ||
} | ||
|
||
public function normalize(string $json): string | ||
{ | ||
if (null === \json_decode($json) && JSON_ERROR_NONE !== \json_last_error()) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'"%s" is not valid JSON.', | ||
$json | ||
)); | ||
} | ||
|
||
return \json_encode( | ||
\json_decode($json), | ||
$this->jsonEncodeOptions | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/json-normalizer | ||
*/ | ||
|
||
namespace Localheinz\Json\Normalizer\Test\Unit; | ||
|
||
use Localheinz\Json\Normalizer\JsonEncodeNormalizer; | ||
|
||
final class JsonEncodeNormalizerTest extends AbstractNormalizerTestCase | ||
{ | ||
public function testConstructorRejectsInvalidJsonEncodeOptions() | ||
{ | ||
$jsonEncodeOptions = -1; | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage(\sprintf( | ||
'"%s" is not valid options for json_encode().', | ||
$jsonEncodeOptions | ||
)); | ||
|
||
new JsonEncodeNormalizer($jsonEncodeOptions); | ||
} | ||
|
||
/** | ||
* @dataProvider providerJsonEncodeOptions | ||
* | ||
* @param int $jsonEncodeOptions | ||
*/ | ||
public function testNormalizeDecodesAndEncodesJsonWithJsonEncodeOptions(int $jsonEncodeOptions) | ||
{ | ||
$json = <<<'JSON' | ||
{ | ||
"name": "Andreas M\u00f6ller", | ||
"url": "https:\/\/github.com\/localheinz\/json-normalizer", | ||
"string-apostroph": "'", | ||
"string-numeric": "9000", | ||
"string-quote": "\"", | ||
"string-tag": "<p>" | ||
} | ||
JSON; | ||
$normalizer = new JsonEncodeNormalizer($jsonEncodeOptions); | ||
|
||
$encoded = \json_encode( | ||
\json_decode($json), | ||
$jsonEncodeOptions | ||
); | ||
|
||
$this->assertSame($encoded, $normalizer->normalize($json)); | ||
} | ||
|
||
public function providerJsonEncodeOptions(): \Generator | ||
{ | ||
/** | ||
* Could add more, but the idea counts. | ||
*/ | ||
$jsonEncodeFlags = [ | ||
JSON_HEX_APOS, | ||
JSON_HEX_QUOT, | ||
JSON_HEX_TAG, | ||
JSON_NUMERIC_CHECK, | ||
JSON_UNESCAPED_SLASHES, | ||
JSON_UNESCAPED_UNICODE, | ||
]; | ||
|
||
$combinations = $this->combinations($jsonEncodeFlags); | ||
|
||
foreach ($combinations as $combination) { | ||
$jsonEncodeOptions = \array_reduce( | ||
$combination, | ||
function (int $jsonEncodeFlag, int $jsonEncodeOptions) { | ||
$jsonEncodeOptions |= $jsonEncodeFlag; | ||
|
||
return $jsonEncodeOptions; | ||
}, | ||
0 | ||
); | ||
|
||
yield [ | ||
$jsonEncodeOptions, | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* @see https://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm | ||
* | ||
* @param array $elements | ||
* | ||
* @return array | ||
*/ | ||
private function combinations(array $elements): array | ||
{ | ||
$combinations = [[]]; | ||
|
||
foreach ($elements as $element) { | ||
foreach ($combinations as $combination) { | ||
$combinations[] = \array_merge( | ||
[ | ||
$element, | ||
], | ||
$combination | ||
); | ||
} | ||
} | ||
|
||
return $combinations; | ||
} | ||
} |