1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Handler; |
6
|
|
|
|
7
|
|
|
use JMS\Serializer\Context; |
8
|
|
|
use JMS\Serializer\SerializationContext; |
9
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
10
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
11
|
|
|
use JMS\Serializer\Visitor\DeserializationVisitorInterface; |
12
|
|
|
|
13
|
|
|
class BackedEnumHandler implements SubscribingHandlerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritDoc} |
17
|
|
|
*/ |
18
|
|
|
public static function getSubscribingMethods(): array |
19
|
|
|
{ |
20
|
|
|
$methods = []; |
21
|
|
|
foreach (['json', 'xml'] as $format) { |
22
|
|
|
$methods[] = [ |
23
|
|
|
'type' => \BackedEnum::class, |
|
|
|
|
24
|
|
|
'format' => $format, |
25
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
26
|
|
|
'method' => 'serializeBackedEnum', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
$methods[] = [ |
30
|
|
|
'type' => \BackedEnum::class, |
31
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, |
32
|
|
|
'format' => $format, |
33
|
|
|
'method' => 'deserializeBackedEnum', |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $methods; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param SerializationVisitorInterface $visitor |
42
|
|
|
* @param \BackedEnum $object |
43
|
|
|
* @param array $type |
44
|
|
|
* @param SerializationContext $context |
45
|
|
|
* |
46
|
|
|
* @return null|string|int |
47
|
|
|
*/ |
48
|
|
|
public function serializeBackedEnum(SerializationVisitorInterface $visitor, $object, array $type, Context $context) |
49
|
|
|
{ |
50
|
|
|
$value = $object->value; |
51
|
|
|
if ('int' === $this->getValueType($type, $context)) { |
52
|
|
|
return $visitor->visitInteger($value, $type); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $visitor->visitString($value, $type); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param DeserializationVisitorInterface $visitor |
60
|
|
|
* @param string|int $value |
61
|
|
|
* @param array $type |
62
|
|
|
* @param Context $context |
63
|
|
|
* |
64
|
|
|
* @return null|\BackedEnum |
65
|
|
|
*/ |
66
|
|
|
public function deserializeBackedEnum(DeserializationVisitorInterface $visitor, $value, array $type, Context $context) |
67
|
|
|
{ |
68
|
|
|
if ('int' === $this->getValueType($type, $context)) { |
69
|
|
|
$value = $visitor->visitInteger($value, $type); |
70
|
|
|
} else { |
71
|
|
|
$value = $visitor->visitString($value, $type); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->tryFrom($type, $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $type |
79
|
|
|
* @param null|int|string $value |
80
|
|
|
* |
81
|
|
|
* @return null|\BackedEnum |
82
|
|
|
*/ |
83
|
|
|
protected function tryFrom(array $type, $value) |
84
|
|
|
{ |
85
|
|
|
return $type['enum']::tryFrom($value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getValueType(array $type, Context $context): string |
89
|
|
|
{ |
90
|
|
|
return $context->getMetadataFactory()->getMetadataForClass($type['enum'])->propertyMetadata['value']->type['name']; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths