@@ 12-72 (lines=61) @@ | ||
9 | use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface; |
|
10 | use Doctrine\Common\Persistence\Proxy; |
|
11 | ||
12 | final class DenormalizerObjectMappingRegistry implements DenormalizerObjectMappingRegistryInterface |
|
13 | { |
|
14 | /** |
|
15 | * @var DenormalizationObjectMappingInterface[] |
|
16 | */ |
|
17 | private $objectMappings; |
|
18 | ||
19 | /** |
|
20 | * @param array $objectMappings |
|
21 | */ |
|
22 | public function __construct(array $objectMappings) |
|
23 | { |
|
24 | $this->objectMappings = []; |
|
25 | foreach ($objectMappings as $objectMapping) { |
|
26 | $this->addObjectMapping($objectMapping); |
|
27 | } |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @param DenormalizationObjectMappingInterface $objectMapping |
|
32 | */ |
|
33 | private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping) |
|
34 | { |
|
35 | $this->objectMappings[$objectMapping->getClass()] = $objectMapping; |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * @param string $class |
|
40 | * |
|
41 | * @return DenormalizationObjectMappingInterface |
|
42 | * |
|
43 | * @throws DeserializerLogicException |
|
44 | */ |
|
45 | public function getObjectMapping(string $class): DenormalizationObjectMappingInterface |
|
46 | { |
|
47 | $reflectionClass = new \ReflectionClass($class); |
|
48 | ||
49 | if (interface_exists('Doctrine\Common\Persistence\Proxy') |
|
50 | && in_array(Proxy::class, $reflectionClass->getInterfaceNames(), true) |
|
51 | ) { |
|
52 | $parentClass = $reflectionClass->getParentClass()->name; |
|
53 | @trigger_error( |
|
54 | sprintf( |
|
55 | 'Use "%s" instead of "%s" for "%s"', |
|
56 | DoctrineDenormalizerObjectMappingRegistry::class, |
|
57 | self::class, |
|
58 | $parentClass |
|
59 | ), |
|
60 | E_USER_DEPRECATED |
|
61 | ); |
|
62 | ||
63 | $class = $parentClass; |
|
64 | } |
|
65 | ||
66 | if (isset($this->objectMappings[$class])) { |
|
67 | return $this->objectMappings[$class]; |
|
68 | } |
|
69 | ||
70 | throw DeserializerLogicException::createMissingMapping($class); |
|
71 | } |
|
72 | } |
|
73 |
@@ 12-70 (lines=59) @@ | ||
9 | use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface; |
|
10 | use Doctrine\Common\Persistence\Proxy; |
|
11 | ||
12 | final class DenormalizerObjectMappingRegistry implements DenormalizerObjectMappingRegistryInterface |
|
13 | { |
|
14 | /** |
|
15 | * @var DenormalizationObjectMappingInterface[] |
|
16 | */ |
|
17 | private $objectMappings; |
|
18 | ||
19 | /** |
|
20 | * @param array $objectMappings |
|
21 | */ |
|
22 | public function __construct(array $objectMappings) |
|
23 | { |
|
24 | $this->objectMappings = []; |
|
25 | foreach ($objectMappings as $objectMapping) { |
|
26 | $this->addObjectMapping($objectMapping); |
|
27 | } |
|
28 | } |
|
29 | ||
30 | /** |
|
31 | * @param DenormalizationObjectMappingInterface $objectMapping |
|
32 | */ |
|
33 | private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping) |
|
34 | { |
|
35 | $this->objectMappings[$objectMapping->getClass()] = $objectMapping; |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * @param string $class |
|
40 | * |
|
41 | * @return DenormalizationObjectMappingInterface |
|
42 | * |
|
43 | * @throws DeserializerLogicException |
|
44 | */ |
|
45 | public function getObjectMapping(string $class): DenormalizationObjectMappingInterface |
|
46 | { |
|
47 | $reflectionClass = new \ReflectionClass($class); |
|
48 | ||
49 | if (in_array(Proxy::class, $reflectionClass->getInterfaceNames(), true)) { |
|
50 | $parentClass = $reflectionClass->getParentClass()->name; |
|
51 | @trigger_error( |
|
52 | sprintf( |
|
53 | 'Use "%s" instead of "%s" for "%s"', |
|
54 | DoctrineDenormalizerObjectMappingRegistry::class, |
|
55 | self::class, |
|
56 | $parentClass |
|
57 | ), |
|
58 | E_USER_DEPRECATED |
|
59 | ); |
|
60 | ||
61 | $class = $parentClass; |
|
62 | } |
|
63 | ||
64 | if (isset($this->objectMappings[$class])) { |
|
65 | return $this->objectMappings[$class]; |
|
66 | } |
|
67 | ||
68 | throw DeserializerLogicException::createMissingMapping($class); |
|
69 | } |
|
70 | } |
|
71 |