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