1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3; |
4
|
|
|
|
5
|
|
|
use Broadway\Serializer\SerializableInterface; |
6
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Calendar\DateRange; |
7
|
|
|
use DateTime; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Provices a class for a timestamp. |
13
|
|
|
* @todo Replace by CultuurNet\UDB3\Model\ValueObject\Calendar\DateRange. |
14
|
|
|
*/ |
15
|
|
|
class Timestamp implements SerializableInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var DateTimeInterface |
20
|
|
|
*/ |
21
|
|
|
protected $startDate; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DateTimeInterface |
25
|
|
|
*/ |
26
|
|
|
protected $endDate; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor |
30
|
|
|
* |
31
|
|
|
* @param DateTimeInterface $startDate |
32
|
|
|
* @param DateTimeInterface $endDate |
33
|
|
|
* |
34
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
DateTimeInterface $startDate, |
38
|
|
|
DateTimeInterface $endDate |
39
|
|
|
) { |
40
|
|
|
if ($endDate < $startDate) { |
41
|
|
|
throw new InvalidArgumentException('End date can not be earlier than start date.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->startDate = $startDate; |
45
|
|
|
$this->endDate = $endDate; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return DateTimeInterface |
50
|
|
|
*/ |
51
|
|
|
public function getStartDate() |
52
|
|
|
{ |
53
|
|
|
return $this->startDate; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return DateTimeInterface |
58
|
|
|
*/ |
59
|
|
|
public function getEndDate() |
60
|
|
|
{ |
61
|
|
|
return $this->endDate; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public static function deserialize(array $data) |
68
|
|
|
{ |
69
|
|
|
return new static( |
70
|
|
|
DateTime::createFromFormat(DateTime::ATOM, $data['startDate']), |
|
|
|
|
71
|
|
|
DateTime::createFromFormat(DateTime::ATOM, $data['endDate']) |
|
|
|
|
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function serialize() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
'startDate' => $this->startDate->format(DateTime::ATOM), |
82
|
|
|
'endDate' => $this->endDate->format(DateTime::ATOM), |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param DateRange $dateRange |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
|
|
public static function fromUdb3ModelDateRange(DateRange $dateRange) |
91
|
|
|
{ |
92
|
|
|
return new Timestamp( |
93
|
|
|
$dateRange->getFrom(), |
|
|
|
|
94
|
|
|
$dateRange->getTo() |
|
|
|
|
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|