Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.50% covered (warning)
62.50%
5 / 8
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BooleanNormalizer
62.50% covered (warning)
62.50%
5 / 8
50.00% covered (danger)
50.00%
1 / 2
11.38
0.00% covered (danger)
0.00%
0 / 1
 supportsDenormalization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 denormalize
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
8.83
1<?php
2namespace Apie\Serializer\Normalizers;
3
4use Apie\Core\Exceptions\InvalidTypeException;
5use Apie\Core\Lists\ItemHashmap;
6use Apie\Core\Lists\ItemList;
7use Apie\Serializer\Context\ApieSerializerContext;
8use Apie\Serializer\Interfaces\DenormalizerInterface;
9use Psr\Http\Message\UploadedFileInterface;
10
11class 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}