Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.78% covered (warning)
83.78%
31 / 37
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemListNormalizer
83.78% covered (warning)
83.78%
31 / 37
25.00% covered (danger)
25.00%
1 / 4
21.71
0.00% covered (danger)
0.00%
0 / 1
 supportsNormalization
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
5.20
 normalize
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 supportsDenormalization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
4
 denormalize
83.33% covered (warning)
83.33%
20 / 24
0.00% covered (danger)
0.00%
0 / 1
7.23
1<?php
2namespace Apie\Serializer\Normalizers;
3
4use Apie\Core\Exceptions\InvalidTypeException;
5use Apie\Core\Lists\ItemHashmap;
6use Apie\Core\Lists\ItemList;
7use Apie\Core\Lists\ItemSet;
8use Apie\Core\Lists\PermissionList;
9use Apie\Core\Utils\HashmapUtils;
10use Apie\Serializer\Context\ApieSerializerContext;
11use Apie\Serializer\Exceptions\ValidationException;
12use Apie\Serializer\Interfaces\DenormalizerInterface;
13use Apie\Serializer\Interfaces\NormalizerInterface;
14use Apie\Serializer\Lists\SerializedHashmap;
15use Apie\Serializer\Lists\SerializedList;
16use Exception;
17use Psr\Http\Message\UploadedFileInterface;
18
19class ItemListNormalizer implements NormalizerInterface, DenormalizerInterface
20{
21    public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool
22    {
23        if ($object instanceof ItemList && !($object instanceof SerializedList)) {
24            return true;
25        }
26        if ($object instanceof ItemSet) {
27            return true;
28        }
29
30        return ($object instanceof ItemHashmap && !($object instanceof SerializedHashmap));
31    }
32
33    /**
34     * @param ItemList|ItemHashmap|ItemSet $object
35     */
36    public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): SerializedList|SerializedHashmap
37    {
38        if ($object instanceof PermissionList) {
39            $object = $object->toStringList();
40        }
41        $list = [];
42        foreach ($object as $key => $value) {
43            $childElement = $apieSerializerContext->normalizeChildElement((string) $key, $value);
44            $list[$key] = $childElement;
45        }
46
47        return $object instanceof ItemHashmap ? new SerializedHashmap($list) : new SerializedList($list);
48    }
49
50    public function supportsDenormalization(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool
51    {
52        return $desiredType === 'array' || HashmapUtils::isHashmap($desiredType) || HashmapUtils::isList($desiredType) || HashmapUtils::isSet($desiredType);
53    }
54
55    public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): mixed
56    {
57        if (is_a($object, $desiredType, true)) {
58            return $object;
59        }
60        if (!is_iterable($object)) {
61            throw new InvalidTypeException($object, 'iterable');
62        }
63        $list = [];
64        $validationErrors = [];
65        $iterator = $object->getIterator();
66        $arrayType = HashmapUtils::getArrayType($desiredType);
67        $key = '';
68        while ($iterator->valid()) {
69            try {
70                do {
71                    $key = $iterator->key();
72                    $value = $iterator->current();
73                    $iterator->next();
74                    $list[$key] = $apieSerializerContext->denormalizeChildElement(
75                        (string) $key,
76                        $value,
77                        $arrayType
78                    );
79                } while ($iterator->valid());
80            } catch (Exception $throwable) {
81                $validationErrors[(string) $key] = $throwable;
82            }
83        }
84        if (!empty($validationErrors)) {
85            throw ValidationException::createFromArray($validationErrors);
86        }
87        return $desiredType === 'array' ? $list : new $desiredType($list);
88    }
89}