|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lamoda\AtolClient\Serializer\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use JMS\Serializer\Context; |
|
6
|
|
|
use JMS\Serializer\GraphNavigator; |
|
7
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
|
8
|
|
|
use JMS\Serializer\VisitorInterface; |
|
9
|
|
|
use Paillechat\Enum\Enum; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Adds support of "Enum" type for JMS. |
|
13
|
|
|
*/ |
|
14
|
|
|
class EnumHandler implements SubscribingHandlerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private static $formats = [ |
|
17
|
|
|
'atol_client', |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
21 |
|
public static function getSubscribingMethods() |
|
24
|
|
|
{ |
|
25
|
21 |
|
$methods = []; |
|
26
|
|
|
|
|
27
|
21 |
|
foreach (self::$formats as $format) { |
|
28
|
21 |
|
$methods[] = [ |
|
29
|
21 |
|
'type' => 'Enum', |
|
30
|
21 |
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
|
31
|
21 |
|
'format' => $format, |
|
32
|
21 |
|
'method' => 'deserialize', |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
21 |
|
$methods[] = [ |
|
36
|
21 |
|
'type' => 'Enum', |
|
37
|
21 |
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|
38
|
21 |
|
'format' => $format, |
|
39
|
21 |
|
'method' => 'serialize', |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
21 |
|
return $methods; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param VisitorInterface $visitor |
|
48
|
|
|
* @param mixed $data |
|
49
|
|
|
* @param array $type |
|
50
|
|
|
* |
|
51
|
|
|
* @throws \Paillechat\Enum\Exception\EnumException |
|
52
|
|
|
* |
|
53
|
|
|
* @return Enum |
|
54
|
|
|
*/ |
|
55
|
12 |
|
public function deserialize(VisitorInterface $visitor, $data, array $type) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
12 |
|
if ($data === null) { |
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Return enum if exists: |
|
62
|
12 |
|
$class = $type['params'][0] ?? null; |
|
63
|
12 |
|
if (class_exists($class) && isset(class_parents($class)[Enum::class])) { |
|
64
|
12 |
|
return new $class($data); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
throw new \LogicException('Enum class does not exist.'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param VisitorInterface $visitor |
|
72
|
|
|
* @param Enum $enum |
|
73
|
|
|
* @param array $type |
|
74
|
|
|
* @param Context $context |
|
75
|
|
|
* |
|
76
|
|
|
* @throws \LogicException |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
14 |
|
public function serialize(VisitorInterface $visitor, Enum $enum, array $type, Context $context) |
|
81
|
|
|
{ |
|
82
|
14 |
|
$valueType = $type['params'][1] ?? 'string'; |
|
83
|
14 |
|
switch ($valueType) { |
|
84
|
14 |
|
case 'string': |
|
85
|
14 |
|
return $visitor->visitString($enum->getValue(), $type, $context); |
|
|
|
|
|
|
86
|
14 |
|
case 'integer': |
|
87
|
14 |
|
return $visitor->visitInteger($enum->getValue(), $type, $context); |
|
|
|
|
|
|
88
|
|
|
default: |
|
89
|
|
|
throw new \LogicException('Unknown value type '); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.