Completed
Pull Request — master (#473)
by
unknown
02:29
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
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\Event;
7
8
use CultuurNet\UDB3\Category;
9
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Category\Category as Udb3ModelCategory;
10
11
final class EventType extends Category
12
{
13
    const DOMAIN = 'eventtype';
14
15
16
    public function __construct($id, $label)
17
    {
18
        parent::__construct($id, $label, self::DOMAIN);
19
    }
20
21
    /**
22
     * Creates a new EventType object from a JSON-LD encoded event.
23
     *
24
     * @param string $eventString
25
     *   The cultural event encoded as JSON-LD
26
     *
27
     * @return self|null
28
     */
29
    public static function fromJSONLDEvent($eventString)
30
    {
31
        $event = json_decode($eventString);
32
        foreach ($event->terms as $term) {
33
            if ($term->domain == self::DOMAIN) {
34
                return new self($term->id, $term->label);
35
            }
36
        }
37
        return null;
38
    }
39
40
    public static function fromUdb3ModelCategory(Udb3ModelCategory $category): EventType
41
    {
42
        $label = $category->getLabel();
43
44
        if (is_null($label)) {
45
            throw new InvalidArgumentException('Category label is required.');
46
        }
47
48
        return new self(
49
            $category->getId()->toString(),
50
            $label->toString()
51
        );
52
    }
53
54
    public static function deserialize(array $data): EventType
55
    {
56
        return new self($data['id'], $data['label']);
57
    }
58
}
59