TypedMapNormalizer   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
eloc 36
c 1
b 0
f 1
dl 0
loc 76
ccs 35
cts 35
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A denormalize() 0 15 3
A supportsDenormalization() 0 11 3
A supportsNormalization() 0 3 1
A createAndFillMap() 0 19 5
A normalize() 0 15 3
1
<?php
2
/*
3
 * This file is part of Goodwix Doctrine JSON ODM.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Goodwix\DoctrineJsonOdm\Serialization\RamseyCollection;
10
11
use Ramsey\Collection\Exception\InvalidArgumentException as RamseyInvalidArgumentException;
12
use Ramsey\Collection\Map\MapInterface;
13
use Ramsey\Collection\Map\TypedMapInterface;
14
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
16
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
17
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
18
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
23
class TypedMapNormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
24
{
25
    use DenormalizerAwareTrait;
26
    use NormalizerAwareTrait;
27
28 8
    public function supportsDenormalization($data, $type, $format = null): bool
29
    {
30 8
        $supports = false;
31
32 8
        if (class_exists($type)) {
33 7
            $reflectionClass = new \ReflectionClass($type);
34 7
            $inheritsClass   = $reflectionClass->isSubclassOf(MapInterface::class);
35 7
            $supports        = is_array($data) && $inheritsClass;
36
        }
37
38 8
        return $supports;
39
    }
40
41 6
    public function denormalize($data, $class, $format = null, array $context = []): TypedMapInterface
42
    {
43 6
        if (!is_array($data)) {
44 1
            throw new UnexpectedValueException(
45 1
                sprintf('Expected value of type "array", value of type "%s" is given.', gettype($data))
46
            );
47
        }
48
49
        try {
50 5
            $map = $this->createAndFillMap($data, $class, $format, $context);
51 1
        } catch (RamseyInvalidArgumentException $exception) {
52 1
            throw new InvalidArgumentException($exception->getMessage(), $exception->getCode(), $exception);
53
        }
54
55 4
        return $map;
56
    }
57
58 16
    public function supportsNormalization($data, $format = null)
59
    {
60 16
        return $data instanceof TypedMapInterface;
61
    }
62
63 8
    public function normalize($object, $format = null, array $context = [])
64
    {
65 8
        $normalizedMap = null;
66
67 8
        if (count($object) > 0) {
68 6
            $normalizedMap = [];
69
70 6
            foreach ($object as $key => $value) {
71 6
                $normalizedMap[$key] = $this->normalizer->normalize($value, $format, $context);
72
            }
73
        } else {
74 2
            $normalizedMap = new \ArrayObject();
75
        }
76
77 8
        return $normalizedMap;
78
    }
79
80 5
    private function createAndFillMap(array $data, string $class, ?string $format, array $context): TypedMapInterface
81
    {
82
        /** @var TypedMapInterface $map */
83 5
        $map = new $class();
84
85 5
        $itemType = $map->getValueType();
86
87 5
        if (class_exists($itemType) || interface_exists($itemType)) {
88 4
            foreach ($data as $key => $item) {
89 4
                $item = $this->denormalizer->denormalize($item, $itemType, $format, $context);
90 4
                $map->put($key, $item);
91
            }
92
        } else {
93 1
            foreach ($data as $key => $item) {
94 1
                $map->put($key, $item);
95
            }
96
        }
97
98 4
        return $map;
99
    }
100
}
101