1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Handler; |
6
|
|
|
|
7
|
|
|
use JMS\Serializer\Context; |
8
|
|
|
use JMS\Serializer\DeserializationContext; |
9
|
|
|
use JMS\Serializer\Exception\NonVisitableTypeException; |
10
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
11
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
12
|
|
|
use JMS\Serializer\SerializationContext; |
13
|
|
|
use JMS\Serializer\Visitor\DeserializationVisitorInterface; |
14
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
15
|
|
|
|
16
|
|
|
final class UnionHandler implements SubscribingHandlerInterface |
17
|
|
|
{ |
18
|
|
|
private static $aliases = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float']; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public static function getSubscribingMethods() |
24
|
|
|
{ |
25
|
|
|
$methods = []; |
26
|
|
|
$formats = ['json', 'xml']; |
27
|
|
|
|
28
|
|
|
foreach ($formats as $format) { |
29
|
|
|
$methods[] = [ |
30
|
|
|
'type' => 'union', |
31
|
|
|
'format' => $format, |
32
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, |
33
|
|
|
'method' => 'deserializeUnion', |
34
|
|
|
]; |
35
|
|
|
$methods[] = [ |
36
|
|
|
'type' => 'union', |
37
|
|
|
'format' => $format, |
38
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
39
|
|
|
'method' => 'serializeUnion', |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $methods; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function serializeUnion( |
47
|
|
|
SerializationVisitorInterface $visitor, |
|
|
|
|
48
|
|
|
mixed $data, |
49
|
|
|
array $type, |
50
|
|
|
SerializationContext $context |
51
|
|
|
): mixed { |
52
|
|
|
if ($this->isPrimitiveType(gettype($data))) { |
53
|
|
|
return $this->matchSimpleType($data, $type, $context); |
54
|
|
|
} else { |
55
|
|
|
$resolvedType = [ |
56
|
|
|
'name' => get_class($data), |
57
|
|
|
'params' => [], |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
return $context->getNavigator()->accept($data, $resolvedType); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function deserializeUnion(DeserializationVisitorInterface $visitor, mixed $data, array $type, DeserializationContext $context): mixed |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if ($data instanceof \SimpleXMLElement) { |
67
|
|
|
throw new RuntimeException('XML deserialisation into union types is not supported yet.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (3 === count($type['params'])) { |
71
|
|
|
$lookupField = $type['params'][1]; |
72
|
|
|
if (empty($data[$lookupField])) { |
73
|
|
|
throw new NonVisitableTypeException(sprintf('Union Discriminator Field "%s" not found in data', $lookupField)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$unionMap = $type['params'][2]; |
77
|
|
|
$lookupValue = $data[$lookupField]; |
78
|
|
|
if (empty($unionMap[$lookupValue])) { |
79
|
|
|
throw new NonVisitableTypeException(sprintf('Union Discriminator Map does not contain key "%s"', $lookupValue)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$finalType = [ |
83
|
|
|
'name' => $unionMap[$lookupValue], |
84
|
|
|
'params' => [], |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
return $context->getNavigator()->accept($data, $finalType); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
foreach ($type['params'][0] as $possibleType) { |
91
|
|
|
if ($this->isPrimitiveType($possibleType['name']) && $this->testPrimitive($data, $possibleType['name'], $context->getFormat())) { |
92
|
|
|
return $context->getNavigator()->accept($data, $possibleType); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function matchSimpleType(mixed $data, array $type, Context $context): mixed |
100
|
|
|
{ |
101
|
|
|
foreach ($type['params'][0] as $possibleType) { |
102
|
|
|
if ($this->isPrimitiveType($possibleType['name']) && !$this->testPrimitive($data, $possibleType['name'], $context->getFormat())) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
return $context->getNavigator()->accept($data, $possibleType); |
108
|
|
|
} catch (NonVisitableTypeException $e) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function isPrimitiveType(string $type): bool |
117
|
|
|
{ |
118
|
|
|
return in_array($type, ['int', 'integer', 'float', 'double', 'bool', 'boolean', 'string'], true); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function testPrimitive(mixed $data, string $type, string $format): bool |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
switch ($type) { |
124
|
|
|
case 'integer': |
125
|
|
|
case 'int': |
126
|
|
|
return (string) (int) $data === (string) $data; |
127
|
|
|
|
128
|
|
|
case 'double': |
129
|
|
|
case 'float': |
130
|
|
|
return (string) (float) $data === (string) $data; |
131
|
|
|
|
132
|
|
|
case 'bool': |
133
|
|
|
case 'boolean': |
134
|
|
|
return (string) (bool) $data === (string) $data; |
135
|
|
|
|
136
|
|
|
case 'string': |
137
|
|
|
return (string) $data === (string) $data; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|