1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lamoda\AtolClient\Serializer\Handler; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
6
|
|
|
use JMS\Serializer\Handler\DateHandler; |
7
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
8
|
|
|
use JMS\Serializer\SerializationContext; |
9
|
|
|
use JMS\Serializer\Visitor\DeserializationVisitorInterface; |
10
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Add support of different formats for DateTime and DateInterval JMS type. |
14
|
|
|
*/ |
15
|
|
|
class ExtendedDateHandler implements SubscribingHandlerInterface |
16
|
|
|
{ |
17
|
|
|
private static $additionalFormats = [ |
18
|
|
|
'atol_client', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
private static $types = [ |
22
|
|
|
'DateTime', |
23
|
|
|
'DateInterval', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** @var DateHandler there is no DI */ |
27
|
|
|
private $dateHandler; |
28
|
|
|
|
29
|
21 |
|
public function __construct() |
30
|
|
|
{ |
31
|
21 |
|
$this->dateHandler = new DateHandler(); |
32
|
21 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
21 |
|
public static function getSubscribingMethods() |
38
|
|
|
{ |
39
|
21 |
|
$methods = DateHandler::getSubscribingMethods(); |
40
|
|
|
|
41
|
21 |
|
foreach (self::$additionalFormats as $format) { |
42
|
21 |
|
$methods[] = [ |
43
|
21 |
|
'type' => 'DateTime', |
44
|
21 |
|
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, |
45
|
21 |
|
'format' => $format, |
46
|
21 |
|
'method' => 'deserializeDateTimeFromJson', |
47
|
|
|
]; |
48
|
|
|
|
49
|
21 |
|
foreach (self::$types as $type) { |
50
|
21 |
|
$methods[] = [ |
51
|
21 |
|
'type' => $type, |
52
|
21 |
|
'format' => $format, |
53
|
21 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
54
|
21 |
|
'method' => 'serialize' . $type, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
21 |
|
return $methods; |
60
|
|
|
} |
61
|
|
|
|
62
|
14 |
|
public function serializeDateTime(SerializationVisitorInterface $visitor, \DateTime $date, array $type, SerializationContext $context) |
63
|
|
|
{ |
64
|
14 |
|
return $this->dateHandler->serializeDateTime($visitor, $date, $type, $context); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function serializeDateTimeImmutable( |
68
|
|
|
SerializationVisitorInterface $visitor, |
69
|
|
|
\DateTimeImmutable $date, |
70
|
|
|
array $type, |
71
|
|
|
SerializationContext $context |
72
|
|
|
) { |
73
|
|
|
return $this->dateHandler->serializeDateTimeImmutable($visitor, $date, $type, $context); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function serializeDateInterval(SerializationVisitorInterface $visitor, \DateInterval $date, array $type, SerializationContext $context) |
77
|
|
|
{ |
78
|
|
|
return $this->dateHandler->serializeDateInterval($visitor, $date, $type, $context); |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
public function deserializeDateTimeFromJson(DeserializationVisitorInterface $visitor, $data, array $type): ?\DateTimeInterface |
82
|
|
|
{ |
83
|
11 |
|
return $this->dateHandler->deserializeDateTimeFromJson($visitor, $data, $type); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|