1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3; |
4
|
|
|
|
5
|
|
|
use Broadway\Serializer\SerializableInterface; |
6
|
|
|
use CultuurNet\UDB3\Event\ValueObjects\Status; |
7
|
|
|
use CultuurNet\UDB3\Event\ValueObjects\StatusType; |
8
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Calendar\SubEvent; |
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 Status |
27
|
|
|
*/ |
28
|
|
|
private $status; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
DateTimeInterface $startDate, |
32
|
|
|
DateTimeInterface $endDate, |
33
|
|
|
Status $status = 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->status = $status ?? new Status(StatusType::available(), []); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function withStatus(Status $status): self |
45
|
|
|
{ |
46
|
|
|
$clone = clone $this; |
47
|
|
|
$clone->status = $status; |
48
|
|
|
return $clone; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getStartDate(): DateTimeInterface |
52
|
|
|
{ |
53
|
|
|
return $this->startDate; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getEndDate(): DateTimeInterface |
57
|
|
|
{ |
58
|
|
|
return $this->endDate; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getStatus(): Status |
62
|
|
|
{ |
63
|
|
|
return $this->status; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function deserialize(array $data): Timestamp |
67
|
|
|
{ |
68
|
|
|
$status = null; |
69
|
|
|
if (isset($data['status'])) { |
70
|
|
|
$status = Status::deserialize($data['status']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new static( |
74
|
|
|
DateTime::createFromFormat(DateTime::ATOM, $data['startDate']), |
|
|
|
|
75
|
|
|
DateTime::createFromFormat(DateTime::ATOM, $data['endDate']), |
|
|
|
|
76
|
|
|
$status |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function serialize(): array |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
'startDate' => $this->startDate->format(DateTime::ATOM), |
84
|
|
|
'endDate' => $this->endDate->format(DateTime::ATOM), |
85
|
|
|
'status' => $this->status->serialize(), |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function toJsonLd(): array |
90
|
|
|
{ |
91
|
|
|
$jsonLd = $this->serialize(); |
92
|
|
|
$jsonLd['@type'] = 'Event'; |
93
|
|
|
|
94
|
|
|
return $jsonLd; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function fromUdb3ModelSubEvent(SubEvent $subEvent): Timestamp |
98
|
|
|
{ |
99
|
|
|
return new Timestamp( |
100
|
|
|
$subEvent->getDateRange()->getFrom(), |
101
|
|
|
$subEvent->getDateRange()->getTo(), |
102
|
|
|
Status::fromUdb3ModelStatus($subEvent->getStatus()) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|