Passed
Push — master ( 00c044...5a9be5 )
by Michael
02:49
created

Relationship::setResourceType()   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
dl 0
loc 4
c 0
b 0
f 0
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
/**
7
 * Definition of relationship
8
 *
9
 * @package Mikemirten\Component\JsonApi\Mapper\Definition
10
 */
11
class Relationship
12
{
13
    const TYPE_X_TO_ONE  = 1;
14
    const TYPE_X_TO_MANY = 2;
15
16
    /**
17
     * Unique name
18
     *
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * Type
25
     *
26
     * @var int
27
     */
28
    protected $relationType;
29
30
    /**
31
     * Collection of links
32
     *
33
     * @var array
34
     */
35
    protected $links = [];
36
37
    /**
38
     * Extra metadata
39
     *
40
     * @var array
41
     */
42
    protected $metadata = [];
43
44
    /**
45
     * Getter-method to access related data
46
     *
47
     * @var string
48
     */
49
    protected $getter;
50
51
    /**
52
     * Getter-method to access an identifier of related object
53
     *
54
     * @var string
55
     */
56
    protected $identifierGetter;
57
58
    /**
59
     * Type of related resource
60
     *
61
     * @var string
62
     */
63
    protected $resourceType;
64
65
    /**
66
     * Relationship constructor.
67
     *
68
     * @param string $name
69
     * @param int    $relationType
70
     */
71 2
    public function __construct(string $name, int $relationType)
72
    {
73 2
        $this->name         = $name;
74 2
        $this->relationType = $relationType;
75 2
    }
76
77
    /**
78
     * Get name
79
     *
80
     * @return string
81
     */
82 2
    public function getName(): string
83
    {
84 2
        return $this->name;
85
    }
86
87
    /**
88
     * Set link
89
     *
90
     * @param string        $name
91
     * @param LinkInterface $link
92
     */
93
    public function setLink(string $name, LinkInterface $link)
94
    {
95
        if (isset($this->links[$name])) {
96
            throw new \LogicException(sprintf('A link name by "%s" is already exists for the relationship'));
97
        }
98
99
        $this->links[$name] = $link;
100
    }
101
102
    /**
103
     * Get links
104
     * [name => link-object]
105
     *
106
     * @return LinkInterface[]
107
     */
108
    public function getLinks(): array
109
    {
110
        $this->links;
111
    }
112
113
    /**
114
     * Get metadata to include into document's relationship
115
     * [name => value]
116
     *
117
     * @return array
118
     */
119
    public function getMetadata(): array
120
    {
121
        return $this->metadata;
122
    }
123
124
    /**
125
     * Is x-to-many type of relationship ?
126
     *
127
     * @return bool
128
     */
129 2
    public function isCollection(): bool
130
    {
131 2
        return $this->relationType === self::TYPE_X_TO_MANY;
132
    }
133
134
    /**
135
     * Set name of a getter-method to access related data
136
     *
137
     * @param string $method
138
     */
139 2
    public function setGetter(string $method)
140
    {
141 2
        $this->getter = $method;
142 2
    }
143
144
    /**
145
     * Get name of a getter-method to access related data
146
     *
147
     * @return string
148
     */
149 2
    public function getGetter(): string
150
    {
151 2
        return $this->getter;
152
    }
153
154
    /**
155
     * Set getter-method to access an identifier of related object
156
     *
157
     * @param string $method
158
     */
159 2
    public function setIdentifierGetter(string $method)
160
    {
161 2
        $this->identifierGetter = $method;
162 2
    }
163
164
    /**
165
     * Has identifier type defined ?
166
     *
167
     * @return bool
168
     */
169 2
    public function hasIdentifierGetter(): bool
170
    {
171 2
        return $this->identifierGetter !== null;
172
    }
173
174
    /**
175
     * Get getter-method to access an identifier of related object
176
     *
177
     * @return string
178
     */
179 2
    public function getIdentifierGetter(): string
180
    {
181 2
        return $this->identifierGetter;
182
    }
183
184
    /**
185
     * Set type of resource
186
     *
187
     * @param string $type
188
     */
189 2
    public function setResourceType(string $type)
190
    {
191 2
        $this->resourceType = $type;
192 2
    }
193
194
    /**
195
     * Has resource type defined ?
196
     *
197
     * @return bool
198
     */
199 2
    public function hasResourceType(): bool
200
    {
201 2
        return $this->resourceType !== null;
202
    }
203
204
    /**
205
     * Get type of resource
206
     *
207
     * @return string
208
     */
209 2
    public function getResourceType(): string
210
    {
211 2
        return $this->resourceType;
212
    }
213
}