Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.49% |
32 / 37 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ItemListNormalizer | |
86.49% |
32 / 37 |
|
50.00% |
2 / 4 |
20.99 | |
0.00% |
0 / 1 |
| supportsNormalization | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
| normalize | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| supportsDenormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
4 | |||
| denormalize | |
83.33% |
20 / 24 |
|
0.00% |
0 / 1 |
7.23 | |||
| 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\Core\Lists\ItemSet; |
| 8 | use Apie\Core\Lists\PermissionList; |
| 9 | use Apie\Core\Utils\HashmapUtils; |
| 10 | use Apie\Serializer\Context\ApieSerializerContext; |
| 11 | use Apie\Serializer\Exceptions\ValidationException; |
| 12 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 13 | use Apie\Serializer\Interfaces\NormalizerInterface; |
| 14 | use Apie\Serializer\Lists\SerializedHashmap; |
| 15 | use Apie\Serializer\Lists\SerializedList; |
| 16 | use Exception; |
| 17 | use Psr\Http\Message\UploadedFileInterface; |
| 18 | |
| 19 | class 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 | } |