-
-
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.
Merge pull request #8 from localheinz/feature/callable-normalizer
Enhancement: Implement CallableNormalizer
- Loading branch information
Showing
3 changed files
with
163 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,41 @@ | ||
<?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 CallableNormalizer implements NormalizerInterface | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $callable; | ||
|
||
public function __construct(callable $callable) | ||
{ | ||
$this->callable = $callable; | ||
} | ||
|
||
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 | ||
)); | ||
} | ||
|
||
$callable = $this->callable; | ||
|
||
return $callable($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,86 @@ | ||
<?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\CallableNormalizer; | ||
|
||
final class CallableNormalizerTest extends AbstractNormalizerTestCase | ||
{ | ||
/** | ||
* @dataProvider providerCallable | ||
* | ||
* @param callable $callable | ||
*/ | ||
public function testNormalizePassesJsonThroughCallable(callable $callable) | ||
{ | ||
$json = <<<'JSON' | ||
{ | ||
"name": "Andreas Möller", | ||
"url": "https://localheinz.com", | ||
"level": 1 | ||
} | ||
JSON; | ||
|
||
$normalized = $callable($json); | ||
|
||
$normalizer = new CallableNormalizer($callable); | ||
|
||
$this->assertSame($normalized, $normalizer->normalize($json)); | ||
} | ||
|
||
public function providerCallable(): \Generator | ||
{ | ||
$values = [ | ||
'closure' => function (string $json): string { | ||
$decoded = \json_decode($json); | ||
|
||
foreach (\get_object_vars($decoded) as $name => $value) { | ||
if (!\is_int($value)) { | ||
continue; | ||
} | ||
|
||
$decoded->{$name} = $value + 1; | ||
} | ||
|
||
return \json_encode($decoded); | ||
}, | ||
'function-name' => 'trim', | ||
'method' => [ | ||
self::class, | ||
'callable', | ||
], | ||
]; | ||
|
||
foreach ($values as $key => $value) { | ||
yield $key => [ | ||
$value, | ||
]; | ||
} | ||
} | ||
|
||
public static function callable(string $json): string | ||
{ | ||
$decoded = \json_decode($json); | ||
|
||
foreach (\get_object_vars($decoded) as $name => $value) { | ||
if (!\is_string($value)) { | ||
continue; | ||
} | ||
|
||
$decoded->{$name} = $value . ' (ok)'; | ||
} | ||
|
||
return \json_encode($decoded); | ||
} | ||
} |