Completed
Pull Request — master (#473)
by
unknown
02:29
created

EventType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 48
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromJSONLDEvent() 0 10 3
A fromUdb3ModelCategory() 0 13 2
A deserialize() 0 4 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