Completed
Pull Request — master (#3)
by Evgeny
08:55
created

ExtendedDateHandler::serializeDateInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
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 21
26
    /** @var DateHandler there is no DI */
27 21
    private $dateHandler;
28
29 21
    public function __construct()
30 21
    {
31 21
        $this->dateHandler = new DateHandler();
32 21
    }
33 21
34 21
    /**
35
     * {@inheritdoc}
36
     */
37 21
    public static function getSubscribingMethods()
38 21
    {
39 21
        $methods = DateHandler::getSubscribingMethods();
40 21
41 21
        foreach (self::$additionalFormats as $format) {
42 21
            $methods[] = [
43
                'type' => 'DateTime',
44
                'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
45
                'format' => $format,
46
                'method' => 'deserializeDateTimeFromJson',
47 21
            ];
48
49
            foreach (self::$types as $type) {
50
                $methods[] = [
51
                    'type' => $type,
52
                    'format' => $format,
53
                    'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
54
                    'method' => 'serialize' . $type,
55
                ];
56
            }
57
        }
58
59
        return $methods;
60
    }
61
62
    public function serializeDateTime(SerializationVisitorInterface $visitor, \DateTime $date, array $type, SerializationContext $context)
63
    {
64
        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
    public function deserializeDateTimeFromJson(DeserializationVisitorInterface $visitor, $data, array $type): ?\DateTimeInterface
82
    {
83
        return $this->dateHandler->deserializeDateTimeFromJson($visitor, $data, $type);
84
    }
85
}
86