-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement ChainNormalizer
- Loading branch information
1 parent
7fc0f9d
commit a8808df
Showing
3 changed files
with
146 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,43 @@ | ||
<?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 ChainNormalizer implements NormalizerInterface | ||
{ | ||
/** | ||
* @var NormalizerInterface[] | ||
*/ | ||
private $normalizers; | ||
|
||
public function __construct(NormalizerInterface ...$normalizers) | ||
{ | ||
$this->normalizers = $normalizers; | ||
} | ||
|
||
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 | ||
)); | ||
} | ||
|
||
foreach ($this->normalizers as $normalizer) { | ||
$json = $normalizer->normalize($json); | ||
} | ||
|
||
return $json; | ||
} | ||
} |
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,75 @@ | ||
<?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\ChainNormalizer; | ||
use Localheinz\Json\Normalizer\NormalizerInterface; | ||
use Prophecy\Argument; | ||
|
||
final class ChainNormalizerTest extends AbstractNormalizerTestCase | ||
{ | ||
public function testNormalizePassesJsonThroughNormalizers() | ||
{ | ||
$count = $this->faker()->numberBetween(3, 5); | ||
|
||
$json = <<<'JSON' | ||
{ | ||
"name": "Andreas Möller", | ||
"url": "https://localheinz.com", | ||
"normalized-by": "%s" | ||
} | ||
JSON; | ||
|
||
$normalize = function (string $json, int $index) { | ||
return \sprintf( | ||
$json, | ||
$index | ||
); | ||
}; | ||
|
||
$normalized = $normalize( | ||
$json, | ||
$count - 1 | ||
); | ||
|
||
$normalizers = \array_fill(0, $count, null); | ||
|
||
$normalizers = \array_map(function (int $index) use ($json, $normalize) { | ||
$normalized = $normalize( | ||
$json, | ||
$index | ||
); | ||
|
||
if (0 < $index) { | ||
$json = $normalize( | ||
$json, | ||
$index - 1 | ||
); | ||
} | ||
|
||
$normalizer = $this->prophesize(NormalizerInterface::class); | ||
|
||
$normalizer | ||
->normalize(Argument::exact($json)) | ||
->shouldBeCalled() | ||
->willReturn($normalized); | ||
|
||
return $normalizer->reveal(); | ||
}, \array_keys($normalizers)); | ||
|
||
$normalizer = new ChainNormalizer(...$normalizers); | ||
|
||
$this->assertSame($normalized, $normalizer->normalize($json)); | ||
} | ||
} |