Passed
Push — master ( a9c512...146f63 )
by Michael
02:32
created

Definition::setType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition;
5
6
use Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour\LinksAwareInterface;
7
use Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour\LinksContainer;
8
9
/**
10
 * Mapping Definition
11
 *
12
 * @package Mikemirten\Component\JsonApi\Mapper\Definition
13
 */
14
class Definition implements LinksAwareInterface
15
{
16
    use LinksContainer;
17
18
    /**
19
     * Class covered by definition
20
     *
21
     * @var string
22
     */
23
    protected $class;
24
25
    /**
26
     * Resource type
27
     *
28
     * @var string
29
     */
30
    protected $type;
31
32
    /**
33
     * Attributes
34
     *
35
     * @var Attribute[]
36
     */
37
    protected $attributes = [];
38
39
    /**
40
     * Relationships
41
     *
42
     * @var Relationship[]
43
     */
44
    protected $relationships = [];
45
46
    /**
47
     * Definition constructor.
48
     *
49
     * @param string $class
50
     */
51 19
    public function __construct(string $class)
52
    {
53 19
        $this->class = $class;
54 19
    }
55
56
    /**
57
     * Get class covered by definition
58
     *
59
     * @return string
60
     */
61 2
    public function getClass(): string
62
    {
63 2
        return $this->class;
64
    }
65
66
    /**
67
     * Set type of resource
68
     *
69
     * @param string $type
70
     */
71 7
    public function setType(string $type)
72
    {
73 7
        $this->type = $type;
74 7
    }
75
76
    /**
77
     * Has type of resource defined ?
78
     *
79
     * @return bool
80
     */
81 3
    public function hasType(): bool
82
    {
83 3
        return $this->type !== null;
84
    }
85
86
    /**
87
     * Get type of resource
88
     *
89
     * @return string
90
     */
91 3
    public function getType(): string
92
    {
93 3
        return $this->type;
94
    }
95
96
    /**
97
     * Add attribute
98
     *
99
     * @param  Attribute $attribute
100
     * @throws \LogicException
101
     */
102
    public function addAttribute(Attribute $attribute)
103
    {
104
        $name = $attribute->getName();
105
106
        if (isset($this->attributes[$name])) {
107
            throw new \LogicException(sprintf('Attribute "%s" already defined.', $name));
108
        }
109
110
        $this->attributes[$name] = $attribute;
111
    }
112
113
    /**
114
     * Get attributes
115
     *
116
     * @return Attribute[]
117
     */
118 1
    public function getAttributes(): array
119
    {
120 1
        return $this->attributes;
121
    }
122
123
    /**
124
     * Add relationship
125
     *
126
     * @param  Relationship $relationship
127
     * @throws \LogicException
128
     */
129 10
    public function addRelationship(Relationship $relationship)
130
    {
131 10
        $name = $relationship->getName();
132
133 10
        if (isset($this->relationships[$name])) {
134 1
            throw new \LogicException(sprintf('Relationship "%s" already defined.', $name));
135
        }
136
137 10
        $this->relationships[$name] = $relationship;
138 10
    }
139
140
    /**
141
     * Get relationships
142
     *
143
     * @return Relationship[]
144
     */
145 8
    public function getRelationships(): array
146
    {
147 8
        return $this->relationships;
148
    }
149
}