Category::toJsonLd()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace CultuurNet\UDB3;
4
5
use Broadway\Serializer\SerializableInterface;
6
use CultuurNet\UDB3\Model\ValueObject\Taxonomy\Category\Category as Udb3ModelCategory;
7
use InvalidArgumentException;
8
9
class Category implements SerializableInterface, JsonLdSerializableInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $id;
15
16
    /**
17
     * @var string
18
     */
19
    protected $label;
20
21
    /**
22
     * @var string
23
     */
24
    protected $domain;
25
26
    public function __construct(string $id, string $label, string $domain)
27
    {
28
        if (empty($id)) {
29
            throw new InvalidArgumentException('Category ID can not be empty.');
30
        }
31
32
        if (!is_string($domain)) {
33
            throw new InvalidArgumentException('Domain should be a string.');
34
        }
35
36
        $this->id = $id;
37
        $this->label = $label;
38
        $this->domain = $domain;
39
    }
40
41
    public function getId(): string
42
    {
43
        return $this->id;
44
    }
45
46
    public function getLabel(): string
47
    {
48
        return $this->label;
49
    }
50
51
    public function getDomain(): string
52
    {
53
        return $this->domain;
54
    }
55
56
    public function serialize(): array
57
    {
58
        return [
59
          'id' => $this->id,
60
          'label' => $this->label,
61
          'domain' => $this->domain,
62
        ];
63
    }
64
65
    /**
66
     * @param array $data
67
     * @return Category
68
     */
69
    public static function deserialize(array $data)
70
    {
71
        return new self($data['id'], $data['label'], $data['domain']);
72
    }
73
74
    public function toJsonLd(): array
75
    {
76
        // Matches the serialized array.
77
        return $this->serialize();
78
    }
79
80
    /**
81
     * @param Udb3ModelCategory $category
82
     * @return Category
83
     */
84
    public static function fromUdb3ModelCategory(Udb3ModelCategory $category)
85
    {
86
        $label = $category->getLabel();
87
        $domain = $category->getDomain();
88
89
        if (is_null($label)) {
90
            throw new InvalidArgumentException('Category label is required.');
91
        }
92
93
        if (is_null($domain)) {
94
            throw new InvalidArgumentException('Category domain is required.');
95
        }
96
97
        return new self(
98
            $category->getId()->toString(),
99
            $label->toString(),
100
            $domain->toString()
101
        );
102
    }
103
}
104