Completed
Pull Request — master (#472)
by
unknown
02:37
created

EventType::deserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Event;
4
5
use CultuurNet\UDB3\Category;
6
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Category\Category as Udb3ModelCategory;
7
use InvalidArgumentException;
8
9
final class EventType extends Category
10
{
11
    public const DOMAIN = 'eventtype';
12
13
    public function __construct(string $id, string $label)
14
    {
15
        parent::__construct($id, $label, self::DOMAIN);
16
    }
17
18
    public static function deserialize(array $data): EventType
19
    {
20
        return new self($data['id'], $data['label']);
21
    }
22
23
    public static function fromJSONLDEvent(string $eventString): ?EventType
24
    {
25
        $event = json_decode($eventString, false);
26
        foreach ($event->terms as $term) {
27
            if ($term->domain === self::DOMAIN) {
28
                return new self($term->id, $term->label);
29
            }
30
        }
31
        return null;
32
    }
33
34
    public static function fromUdb3ModelCategory(Udb3ModelCategory $category): EventType
35
    {
36
        $label = $category->getLabel();
37
38
        if (is_null($label)) {
39
            throw new InvalidArgumentException('Category label is required.');
40
        }
41
42
        return new self(
43
            $category->getId()->toString(),
44
            $label->toString()
45
        );
46
    }
47
}
48