forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: refactor normalizer integration
Better architecture for upcoming JSON normalizer
- Loading branch information
Showing
10 changed files
with
113 additions
and
180 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
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Normalizer; | ||
|
||
use CuyZ\Valinor\Normalizer\Transformer\RecursiveTransformer; | ||
|
||
use function array_map; | ||
use function is_array; | ||
use function is_iterable; | ||
use function iterator_to_array; | ||
|
||
/** | ||
* @api | ||
* | ||
* @implements Normalizer<array<mixed>|scalar|null> | ||
*/ | ||
final class ArrayNormalizer implements Normalizer | ||
{ | ||
public function __construct( | ||
private RecursiveTransformer $transformer, | ||
) {} | ||
|
||
public function normalize(mixed $value): mixed | ||
{ | ||
$value = $this->transformer->transform($value); | ||
|
||
return $this->normalizeIterator($value); | ||
} | ||
|
||
/** | ||
* @param iterable<mixed>|scalar|null $value | ||
* @return array<mixed>|scalar|null | ||
*/ | ||
private function normalizeIterator(mixed $value): mixed | ||
{ | ||
if (is_iterable($value)) { | ||
if (! is_array($value)) { | ||
$value = iterator_to_array($value); | ||
} | ||
|
||
// PHP8.1 First-class callable syntax | ||
$value = array_map([$this, 'normalizeIterator'], $value); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.