1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Denormalizer; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\DeserializerLogicException; |
8
|
|
|
use Chubbyphp\Deserialization\Doctrine\Denormalizer\DenormalizerObjectMappingRegistry as DoctrineDenormalizerObjectMappingRegistry; |
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
|
3 |
|
public function __construct(array $objectMappings) |
23
|
|
|
{ |
24
|
3 |
|
$this->objectMappings = []; |
25
|
3 |
|
foreach ($objectMappings as $objectMapping) { |
26
|
2 |
|
$this->addObjectMapping($objectMapping); |
27
|
|
|
} |
28
|
3 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param DenormalizationObjectMappingInterface $objectMapping |
32
|
|
|
*/ |
33
|
2 |
|
private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping) |
34
|
|
|
{ |
35
|
2 |
|
$this->objectMappings[$objectMapping->getClass()] = $objectMapping; |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $class |
40
|
|
|
* |
41
|
|
|
* @return DenormalizationObjectMappingInterface |
42
|
|
|
* |
43
|
|
|
* @throws DeserializerLogicException |
44
|
|
|
*/ |
45
|
3 |
|
public function getObjectMapping(string $class): DenormalizationObjectMappingInterface |
46
|
|
|
{ |
47
|
3 |
|
$reflectionClass = new \ReflectionClass($class); |
48
|
|
|
|
49
|
3 |
|
if (interface_exists('Doctrine\Common\Persistence\Proxy') |
50
|
3 |
|
&& in_array(Proxy::class, $reflectionClass->getInterfaceNames(), true) |
51
|
|
|
) { |
52
|
1 |
|
$parentClass = $reflectionClass->getParentClass()->name; |
53
|
1 |
|
@trigger_error( |
|
|
|
|
54
|
1 |
|
sprintf( |
55
|
1 |
|
'Use "%s" instead of "%s" for "%s"', |
56
|
1 |
|
DoctrineDenormalizerObjectMappingRegistry::class, |
57
|
1 |
|
self::class, |
58
|
1 |
|
$parentClass |
59
|
|
|
), |
60
|
1 |
|
E_USER_DEPRECATED |
61
|
|
|
); |
62
|
|
|
|
63
|
1 |
|
$class = $parentClass; |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
if (isset($this->objectMappings[$class])) { |
67
|
2 |
|
return $this->objectMappings[$class]; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
throw DeserializerLogicException::createMissingMapping($class); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: