Completed
Push — feature/EVO-5751-text-index-mo... ( 0ab3ac...8fecb1 )
by
unknown
62:16 queued 57:01
created

DateHandler::deserializeDateTimeFromJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 10
rs 9.4285
ccs 0
cts 5
cp 0
cc 2
eloc 5
nc 2
nop 3
crap 6
1
<?php
2
/**
3
 * DateHandler class file
4
 */
5
6
namespace Graviton\DocumentBundle\Serializer\Handler;
7
8
use JMS\Serializer\JsonDeserializationVisitor;
9
use JMS\Serializer\Handler\DateHandler as BaseDateHandler;
10
use JsonSchema\Rfc3339;
11
12
/**
13
 * Date handler for JMS serializer
14
 * This is needed as we allowed the client to pass microseconds in datetime values.
15
 * This cannot be parsed by the Serializer as it's not always the same format. By using the Rfc3339 class
16
 * from JsonSchema, we use the same logic as we use in the validation and can accept that date.
17
 * Note that the main logic from JMS DateHandler is still used..
18
 *
19
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
20
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
21
 * @link     http://swisscom.ch
22
 */
23
class DateHandler extends BaseDateHandler
24
{
25
26
    /**
27
     * @var string
28
     */
29
    private $defaultFormat;
30
31
    /**
32
     * @var string
33
     */
34
    private $defaultTimezone;
35
36
    /**
37
     * DateHandler constructor.
38
     *
39
     * @param string $defaultFormat   configured default format
40
     * @param string $defaultTimezone default timezone
41
     * @param bool   $xmlCData        xml data
42
     */
43
    public function __construct($defaultFormat = \DateTime::ISO8601, $defaultTimezone = 'UTC', $xmlCData = true)
44
    {
45
        $this->defaultFormat = $defaultFormat;
46
        $this->defaultTimezone = $defaultTimezone;
47
        parent::__construct($defaultFormat, $defaultTimezone, $xmlCData);
48
    }
49
50
    /**
51
     * serialize datetime from json
52
     *
53
     * @param JsonDeserializationVisitor $visitor visitor
54
     * @param string                     $data    data
55
     * @param array                      $type    type
56
     *
57
     * @return \DateTime|null DateTime instance
58
     */
59
    public function deserializeDateTimeFromJson(JsonDeserializationVisitor $visitor, $data, array $type)
60
    {
61
        if (null === $data) {
62
            return null;
63
        }
64
65
        $dt = Rfc3339::createFromString($data);
66
67
        return parent::deserializeDateTimeFromJson($visitor, $dt->format($this->defaultFormat), $type);
68
    }
69
}
70