1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Handler; |
6
|
|
|
|
7
|
|
|
use JMS\Serializer\Exception\InvalidMetadataException; |
8
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
9
|
|
|
use JMS\Serializer\SerializationContext; |
10
|
|
|
use JMS\Serializer\Visitor\DeserializationVisitorInterface; |
11
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
12
|
|
|
|
13
|
|
|
final class EnumHandler implements SubscribingHandlerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public static function getSubscribingMethods() |
19
|
|
|
{ |
20
|
|
|
$methods = []; |
21
|
|
|
|
22
|
|
|
foreach (['json', 'xml'] as $format) { |
23
|
|
|
$methods[] = [ |
24
|
|
|
'type' => 'enum', |
25
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, |
26
|
|
|
'format' => $format, |
27
|
|
|
'method' => 'deserializeEnum', |
28
|
|
|
]; |
29
|
|
|
$methods[] = [ |
30
|
|
|
'type' => 'enum', |
31
|
|
|
'format' => $format, |
32
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
33
|
|
|
'method' => 'serializeEnum', |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $methods; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function serializeEnum( |
41
|
|
|
SerializationVisitorInterface $visitor, |
|
|
|
|
42
|
|
|
\UnitEnum $enum, |
|
|
|
|
43
|
|
|
array $type, |
44
|
|
|
SerializationContext $context |
45
|
|
|
) { |
46
|
|
|
if (isset($type['params'][1]) && 'value' === $type['params'][1]) { |
47
|
|
|
if (!$enum instanceof \BackedEnum) { |
|
|
|
|
48
|
|
|
throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', get_class($enum))); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$valueType = isset($type['params'][2]) ? ['name' => $type['params'][2]] : null; |
52
|
|
|
|
53
|
|
|
return $context->getNavigator()->accept($enum->value, $valueType); |
54
|
|
|
} else { |
55
|
|
|
return $context->getNavigator()->accept($enum->name); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int|string|\SimpleXMLElement $data |
61
|
|
|
* @param array $type |
62
|
|
|
*/ |
63
|
|
|
public function deserializeEnum(DeserializationVisitorInterface $visitor, $data, array $type): ?\UnitEnum |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$ref = new \ReflectionEnum($type['params'][0]); |
66
|
|
|
|
67
|
|
|
if (isset($type['params'][1]) && 'value' === $type['params'][1]) { |
68
|
|
|
$backingType = $ref->getBackingType(); |
|
|
|
|
69
|
|
|
|
70
|
|
|
if (!$backingType instanceof \ReflectionNamedType) { |
71
|
|
|
throw new InvalidMetadataException(sprintf('The type "%s" is not a backed enum, thus you can not use "value" as serialization mode for its value.', $ref->getName())); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$cases = $ref->getCases(); |
|
|
|
|
75
|
|
|
foreach ($cases as $case) { |
76
|
|
|
$value = $case->getValue(); |
77
|
|
|
$backingTypeName = $backingType->getName(); |
78
|
|
|
|
79
|
|
|
if ('int' === $backingTypeName) { |
80
|
|
|
if ($value->value === (int) $data) { |
81
|
|
|
return $value; |
82
|
|
|
} |
83
|
|
|
} elseif ('string' === $backingTypeName) { |
84
|
|
|
if ($value->value === (string) $data) { |
85
|
|
|
return $value; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return null; |
91
|
|
|
} else { |
92
|
|
|
$caseValue = (string) $data; |
93
|
|
|
if (!$ref->hasCase($caseValue)) { |
|
|
|
|
94
|
|
|
throw new InvalidMetadataException(sprintf('The type "%s" does not have the case "%s"', $ref->getName(), $caseValue)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $ref->getCase($caseValue)->getValue(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.