-
-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathUnionHandler.php
More file actions
174 lines (146 loc) · 5.09 KB
/
UnionHandler.php
File metadata and controls
174 lines (146 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace JMS\Serializer\Handler;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Exception\NonVisitableTypeException;
use JMS\Serializer\Exception\NotAcceptableException;
use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Type\Type;
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
/**
* @phpstan-import-type TypeArray from Type
*/
final class UnionHandler implements SubscribingHandlerInterface
{
private static $aliases = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float'];
/**
* {@inheritdoc}
*/
public static function getSubscribingMethods()
{
$methods = [];
$formats = ['json', 'xml'];
foreach ($formats as $format) {
$methods[] = [
'type' => 'union',
'format' => $format,
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
'method' => 'deserializeUnion',
];
$methods[] = [
'type' => 'union',
'format' => $format,
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'method' => 'serializeUnion',
];
}
return $methods;
}
/**
* @param TypeArray $type
*/
public function serializeUnion(
SerializationVisitorInterface $visitor,
mixed $data,
array $type,
SerializationContext $context
): mixed {
if ($this->isPrimitiveType(gettype($data))) {
$resolvedType = [
'name' => gettype($data),
'params' => [],
];
} else {
$resolvedType = [
'name' => get_class($data),
'params' => [],
];
}
return $context->getNavigator()->accept($data, $resolvedType);
}
/**
* @param TypeArray $type
*/
public function deserializeUnion(DeserializationVisitorInterface $visitor, mixed $data, array $type, DeserializationContext $context): mixed
{
if ($data instanceof \SimpleXMLElement) {
throw new RuntimeException('XML deserialisation into union types is not supported yet.');
}
if (3 === count($type['params'])) {
$lookupField = $type['params'][1];
if (empty($data[$lookupField])) {
throw new NonVisitableTypeException(sprintf('Union Discriminator Field "%s" not found in data', $lookupField));
}
$unionMap = $type['params'][2];
$lookupValue = $data[$lookupField];
if (empty($unionMap[$lookupValue])) {
throw new NonVisitableTypeException(sprintf('Union Discriminator Map does not contain key "%s"', $lookupValue));
}
$finalType = [
'name' => $unionMap[$lookupValue],
'params' => [],
];
return $context->getNavigator()->accept($data, $finalType);
}
$dataType = gettype($data);
if (
array_filter(
$type['params'][0],
static fn (array $type): bool => $type['name'] === $dataType || (isset(self::$aliases[$dataType]) && $type['name'] === self::$aliases[$dataType]),
)
) {
return $context->getNavigator()->accept($data, [
'name' => $dataType,
'params' => [],
]);
}
foreach ($type['params'][0] as $possibleType) {
if ($this->isPrimitiveType($possibleType['name']) && $this->testPrimitive($data, $possibleType['name'])) {
return $context->getNavigator()->accept($data, $possibleType);
}
}
throw new NotAcceptableException();
}
private static $primitiveTypes = [
'int' => true,
'integer' => true,
'float' => true,
'double' => true,
'bool' => true,
'boolean' => true,
'true' => true,
'false' => true,
'string' => true,
'array' => true,
];
private function isPrimitiveType(string $type): bool
{
return isset(self::$primitiveTypes[$type]);
}
private function testPrimitive(mixed $data, string $type): bool
{
switch ($type) {
case 'array':
return is_array($data);
case 'integer':
case 'int':
return (string) (int) $data === (string) $data;
case 'double':
case 'float':
return (string) (float) $data === (string) $data;
case 'bool':
case 'boolean':
return !is_array($data) && (string) (bool) $data === (string) $data;
case 'true':
return true === $data;
case 'false':
return false === $data;
case 'string':
return !is_array($data) && !is_object($data);
}
return false;
}
}