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.
feat: add generic type to normalizer and use external formatter
- Loading branch information
Showing
17 changed files
with
275 additions
and
33 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
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Normalizer; | ||
|
||
use CuyZ\Valinor\Normalizer\Formatter\Formatter; | ||
use CuyZ\Valinor\Normalizer\Formatter\FormatterFactory; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @template T | ||
* @implements Normalizer<T> | ||
*/ | ||
final class FormatNormalizer implements Normalizer | ||
{ | ||
/** | ||
* @param FormatterFactory<Formatter<T>> $formatterFactory | ||
*/ | ||
public function __construct( | ||
private FormatterFactory $formatterFactory, | ||
private RecursiveNormalizer $recursiveNormalizer, | ||
) {} | ||
|
||
public function normalize(mixed $value): mixed | ||
{ | ||
return $this->recursiveNormalizer->normalize($value, $this->formatterFactory->new()); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Normalizer\Formatter; | ||
|
||
use function is_array; | ||
use function is_iterable; | ||
use function iterator_to_array; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @implements Formatter<array<mixed>|scalar|null> | ||
*/ | ||
final class ArrayFormatter implements Formatter | ||
{ | ||
/** @var iterable<mixed>|scalar|null */ | ||
private mixed $value; | ||
|
||
public function push(mixed $value): void | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function value(): mixed | ||
{ | ||
if (is_iterable($this->value) && ! is_array($this->value)) { | ||
$this->value = iterator_to_array($this->value); | ||
} | ||
|
||
return $this->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Normalizer\Formatter; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @implements FormatterFactory<ArrayFormatter> | ||
*/ | ||
final class ArrayFormatterFactory implements FormatterFactory | ||
{ | ||
public function new(): ArrayFormatter | ||
{ | ||
return new ArrayFormatter(); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Normalizer\Formatter; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @template-covariant T | ||
*/ | ||
interface Formatter | ||
{ | ||
/** | ||
* @param iterable<mixed>|scalar|null $value | ||
*/ | ||
public function push(mixed $value): void; | ||
|
||
/** | ||
* @return T | ||
*/ | ||
public function value(): mixed; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Normalizer\Formatter; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @template-covariant T of Formatter | ||
*/ | ||
interface FormatterFactory | ||
{ | ||
/** | ||
* @return T | ||
*/ | ||
public function new(): Formatter; | ||
} |
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
Oops, something went wrong.