Test Failed
Push — master ( 8a2b60...8e4848 )
by Dominik
03:00
created

NormalizerObjectMappingRegistry::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Chubbyphp\Serialization\SerializerLogicException;
8
use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface;
9
10
final class NormalizerObjectMappingRegistry implements NormalizerObjectMappingRegistryInterface
11
{
12
    /**
13
     * @var NormalizationObjectMappingInterface[]
14
     */
15
    private $objectMappings;
16
17
    /**
18
     * @param array $objectMappings
19
     */
20
    public function __construct(array $objectMappings)
21
    {
22
        $this->objectMappings = [];
23
        foreach ($objectMappings as $objectMapping) {
24
            $this->addObjectMapping($objectMapping);
25
        }
26
    }
27
28
    /**
29
     * @param NormalizationObjectMappingInterface $objectMapping
30
     */
31
    private function addObjectMapping(NormalizationObjectMappingInterface $objectMapping)
32
    {
33
        $this->objectMappings[$objectMapping->getClass()] = $objectMapping;
34
    }
35
36
    /**
37
     * @param string $class
38
     *
39
     * @return NormalizationObjectMappingInterface
40
     *
41
     * @throws SerializerLogicException
42
     */
43
    public function getObjectMapping(string $class): NormalizationObjectMappingInterface
44
    {
45
        if (isset($this->objectMappings[$class])) {
46
            return $this->objectMappings[$class];
47
        }
48
49
        throw SerializerLogicException::createMissingMapping($class);
50
    }
51
}
52