Completed
Pull Request — master (#345)
by
unknown
15:02
created

Category   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

8 Methods

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