1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Event\ValueObjects; |
4
|
|
|
|
5
|
|
|
use Broadway\Serializer\SerializableInterface; |
6
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Translation\Language; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
final class EventStatus implements SerializableInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var EventStatusType |
13
|
|
|
*/ |
14
|
|
|
private $eventStatusType; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var EventStatusReason[] |
18
|
|
|
*/ |
19
|
|
|
private $eventStatusReasons; |
20
|
|
|
|
21
|
|
|
public function __construct(EventStatusType $eventStatusType, array $eventStatusReasons) |
22
|
|
|
{ |
23
|
|
|
$this->ensureTranslationsAreUnique($eventStatusReasons); |
24
|
|
|
$this->eventStatusType = $eventStatusType; |
25
|
|
|
$this->eventStatusReasons = $eventStatusReasons; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getEventStatusType(): EventStatusType |
29
|
|
|
{ |
30
|
|
|
return $this->eventStatusType; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getEventStatusReasons(): array |
34
|
|
|
{ |
35
|
|
|
return $this->eventStatusReasons; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function deserialize(array $data): EventStatus |
39
|
|
|
{ |
40
|
|
|
$eventStatusReasons = []; |
41
|
|
|
foreach ($data['eventStatusReason'] as $language => $eventStatusReason) { |
42
|
|
|
$eventStatusReasons[] = new EventStatusReason( |
43
|
|
|
new Language($language), |
44
|
|
|
$eventStatusReason |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return new EventStatus( |
49
|
|
|
EventStatusType::fromNative($data['eventStatus']), |
50
|
|
|
$eventStatusReasons |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function serialize(): array |
55
|
|
|
{ |
56
|
|
|
$eventStatusReasons = []; |
57
|
|
|
foreach ($this->eventStatusReasons as $statusReason) { |
58
|
|
|
$eventStatusReasons[$statusReason->getLanguage()->getCode()] = $statusReason->getReason(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return [ |
62
|
|
|
'eventStatus' => $this->eventStatusType->toNative(), |
63
|
|
|
'eventStatusReason' => $eventStatusReasons, |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param EventStatusReason[] $eventStatusReasons |
69
|
|
|
*/ |
70
|
|
|
private function ensureTranslationsAreUnique(array $eventStatusReasons): void |
71
|
|
|
{ |
72
|
|
|
$languageCodes = \array_map(static function (EventStatusReason $reason) { |
73
|
|
|
return $reason->getLanguage()->getCode(); |
74
|
|
|
}, $eventStatusReasons); |
75
|
|
|
|
76
|
|
|
if (count($languageCodes) !== count(array_unique($languageCodes))) { |
77
|
|
|
throw new InvalidArgumentException('Duplicate translations are not allowed for EventStatusReason'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|