Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| EnumNormalizer | |
94.74% |
18 / 19 |
|
75.00% |
3 / 4 |
11.02 | |
0.00% |
0 / 1 |
| supportsNormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportsDenormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| denormalize | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
7.01 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\Lists\ItemHashmap; |
| 5 | use Apie\Core\Lists\ItemList; |
| 6 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 7 | use Apie\Core\ValueObjects\Utils; |
| 8 | use Apie\Serializer\Context\ApieSerializerContext; |
| 9 | use Apie\Serializer\Exceptions\ItemCanNotBeNormalizedInCurrentContext; |
| 10 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 11 | use Apie\Serializer\Interfaces\NormalizerInterface; |
| 12 | use Psr\Http\Message\UploadedFileInterface; |
| 13 | use ReflectionClass; |
| 14 | use ReflectionEnum; |
| 15 | use UnitEnum; |
| 16 | |
| 17 | class EnumNormalizer implements NormalizerInterface, DenormalizerInterface |
| 18 | { |
| 19 | public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool |
| 20 | { |
| 21 | return $object instanceof UnitEnum; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param UnitEnum $object |
| 26 | */ |
| 27 | public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): string|int |
| 28 | { |
| 29 | return $object->value ?? $object->name; |
| 30 | } |
| 31 | |
| 32 | public function supportsDenormalization(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool |
| 33 | { |
| 34 | return is_a($desiredType, UnitEnum::class, true) && in_array(get_debug_type($object), ['int', 'string']); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param string|int $object |
| 39 | * @param class-string<UnitEnum> $desiredType |
| 40 | */ |
| 41 | public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): UnitEnum |
| 42 | { |
| 43 | $object = Utils::toString($object); |
| 44 | $refl = new ReflectionEnum($desiredType); |
| 45 | $enum = null; |
| 46 | foreach ($refl->getCases() as $case) { |
| 47 | if ($refl->isBacked()) { |
| 48 | if (((string) $case->getBackingValue()) === $object) { |
| 49 | $enum = $case->getValue(); |
| 50 | } |
| 51 | } |
| 52 | if ($case->name === $object) { |
| 53 | $enum = $case->getValue(); |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | if (!$enum) { |
| 58 | throw new InvalidStringForValueObjectException($object, new ReflectionClass($desiredType)); |
| 59 | } |
| 60 | $refl = $refl->getCase($enum->name); |
| 61 | if (!$apieSerializerContext->getContext()->appliesToContext($refl)) { |
| 62 | throw new ItemCanNotBeNormalizedInCurrentContext($enum); |
| 63 | } |
| 64 | return $enum; |
| 65 | } |
| 66 | } |