Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
62.50% |
5 / 8 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| BooleanNormalizer | |
62.50% |
5 / 8 |
|
50.00% |
1 / 2 |
11.38 | |
0.00% |
0 / 1 |
| supportsDenormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| denormalize | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
8.83 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\Exceptions\InvalidTypeException; |
| 5 | use Apie\Core\Lists\ItemHashmap; |
| 6 | use Apie\Core\Lists\ItemList; |
| 7 | use Apie\Serializer\Context\ApieSerializerContext; |
| 8 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 9 | use Psr\Http\Message\UploadedFileInterface; |
| 10 | |
| 11 | class BooleanNormalizer implements DenormalizerInterface |
| 12 | { |
| 13 | public function supportsDenormalization(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool |
| 14 | { |
| 15 | return $desiredType ==='bool' || $desiredType === 'boolean'; |
| 16 | } |
| 17 | public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool |
| 18 | { |
| 19 | return match (gettype($object)) { |
| 20 | 'string' => filter_var($object, FILTER_VALIDATE_BOOLEAN), |
| 21 | 'boolean' => $object, |
| 22 | 'integer' => !!$object, |
| 23 | 'double' => !!$object, |
| 24 | default => throw new InvalidTypeException($object, 'bool'), |
| 25 | }; |
| 26 | } |
| 27 | } |