|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DMT\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use JMS\Serializer\Context; |
|
6
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
|
7
|
|
|
use JMS\Serializer\GraphNavigator; |
|
8
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
|
9
|
|
|
use JMS\Serializer\JsonDeserializationVisitor; |
|
10
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
|
11
|
|
|
use JMS\Serializer\VisitorInterface; |
|
12
|
|
|
use phpDocumentor\Reflection\Types\Object_; |
|
13
|
|
|
|
|
14
|
|
|
class ParameterBagHandler implements SubscribingHandlerInterface |
|
15
|
|
|
{ |
|
16
|
2 |
|
public static function getSubscribingMethods(): array |
|
17
|
|
|
{ |
|
18
|
|
|
return [ |
|
19
|
|
|
[ |
|
20
|
2 |
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|
21
|
2 |
|
'format' => 'json', |
|
22
|
2 |
|
'type' => 'ParameterBag', |
|
23
|
2 |
|
'method' => 'serializeParameterBag', |
|
24
|
|
|
], |
|
25
|
|
|
[ |
|
26
|
2 |
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
|
27
|
2 |
|
'format' => 'json', |
|
28
|
2 |
|
'type' => 'ParameterBag', |
|
29
|
2 |
|
'method' => 'deserializeParameterBag', |
|
30
|
|
|
], |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param JsonSerializationVisitor|VisitorInterface $visitor |
|
36
|
|
|
* @param ParameterBag|null $data |
|
37
|
|
|
* @param array $type |
|
38
|
|
|
* @param Context $context |
|
39
|
|
|
* |
|
40
|
|
|
* @return array|null |
|
41
|
|
|
* @throws RuntimeException |
|
42
|
|
|
*/ |
|
43
|
25 |
|
public function serializeParameterBag( |
|
44
|
|
|
VisitorInterface $visitor, ? ParameterBag $data, array $type, Context $context |
|
|
|
|
|
|
45
|
|
|
): ? array |
|
46
|
|
|
{ |
|
47
|
25 |
|
$result = []; |
|
48
|
25 |
|
if ($data === null) { |
|
49
|
6 |
|
return $context->shouldSerializeNull() ? $result : null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
19 |
|
foreach ($data as $parameter) { |
|
53
|
13 |
|
if (!$context->shouldSerializeNull() && $parameter->getValue() === null) { |
|
|
|
|
|
|
54
|
6 |
|
continue; |
|
55
|
|
|
} |
|
56
|
13 |
|
$result[$parameter->getName()] = $parameter->getValue(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
19 |
|
return count($result) || $context->shouldSerializeNull() ? $result : null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Deserialize elements into a bag of parameters. |
|
64
|
|
|
* |
|
65
|
|
|
* @param JsonDeserializationVisitor|VisitorInterface $visitor |
|
66
|
|
|
* @param array $data |
|
67
|
|
|
* @param array $type |
|
68
|
|
|
* @param Context $context |
|
69
|
|
|
* |
|
70
|
|
|
* @return ParameterBag |
|
71
|
|
|
* @throws RuntimeException |
|
72
|
|
|
*/ |
|
73
|
25 |
|
public function deserializeParameterBag( |
|
74
|
|
|
VisitorInterface $visitor, $data = null, array $type, Context $context |
|
|
|
|
|
|
75
|
|
|
): ParameterBag |
|
76
|
|
|
{ |
|
77
|
25 |
|
$result = new ParameterBag(); |
|
78
|
25 |
|
if (null === $data) { |
|
79
|
6 |
|
return $result; |
|
80
|
|
|
} |
|
81
|
19 |
|
if (!is_iterable($data)) { |
|
82
|
4 |
|
throw new RuntimeException('ParameterBag only excepts an array of parameters'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** @var ParameterInterface $parameter */ |
|
86
|
15 |
|
$parameterType = $this->getParameterType($type); |
|
87
|
13 |
|
$parameter = unserialize(sprintf('O:%d:"%s":0:{}', strlen($parameterType), $parameterType)); |
|
88
|
|
|
|
|
89
|
13 |
|
foreach ($data as $key => $value) { |
|
90
|
13 |
|
if (!$context->shouldSerializeNull() && $value === null) { |
|
|
|
|
|
|
91
|
6 |
|
continue; |
|
92
|
|
|
} |
|
93
|
13 |
|
$instance = clone $parameter; |
|
94
|
13 |
|
$instance->setName($key); |
|
95
|
13 |
|
$instance->setValue($value); |
|
96
|
|
|
|
|
97
|
13 |
|
$result[] = $instance; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
13 |
|
return $result; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the class name of the object to store in parameter bag. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $type |
|
107
|
|
|
* |
|
108
|
|
|
* @return string The class name for the parameters. |
|
109
|
|
|
* @throws RuntimeException |
|
110
|
|
|
*/ |
|
111
|
15 |
|
protected function getParameterType(array $type): string |
|
112
|
|
|
{ |
|
113
|
15 |
|
if (empty($type['params'])) { |
|
114
|
5 |
|
return Parameter::class; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
10 |
|
$parameterType = $type['params'][0]['name'] ?? $type['params'][0]; |
|
118
|
10 |
|
if (!class_exists($parameterType) || !is_subclass_of($parameterType, ParameterInterface::class)) { |
|
119
|
2 |
|
throw new RuntimeException('Parameter(s) must implement ' . ParameterInterface::class); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
8 |
|
return $parameterType; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.