|
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
|
|
|
|