Passed
Push — master ( 10fe16...12671c )
by Michael
02:49
created

Relationship::isDataIncluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
/**
11
 * Definition of relationship
12
 *
13
 * @package Mikemirten\Component\JsonApi\Mapper\Definition
14
 */
15
class Relationship implements LinksAwareInterface
16
{
17
    use LinksContainer;
18
19
    const TYPE_X_TO_ONE  = 1;
20
    const TYPE_X_TO_MANY = 2;
21
22
    /**
23
     * Name unique name of resource-object inside of relationships-object
24
     *
25
     * {
26
     *     "author": { <--- Name of resource-object
27
     *         "data": {
28
     *             "id":   "12345",
29
     *             "type": "Author"
30
     *         }
31
     *     }
32
     * }
33
     *
34
     * @var string
35
     */
36
    protected $name;
37
38
    /**
39
     * Type or relationship: "x to one" or "x to many".
40
     *
41
     * @see for TYPE_* constants.
42
     *
43
     * @var int
44
     */
45
    protected $type;
46
47
    /**
48
     * Extra metadata
49
     *
50
     * @var array
51
     */
52
    protected $metadata = [];
53
54
    /**
55
     * Name of property contains related object.
56
     * Value is optional. Can be set only for real properties.
57
     *
58
     * @var string
59
     */
60
    protected $propertyName;
61
62
    /**
63
     * Getter-method to access related data
64
     *
65
     * @var string
66
     */
67
    protected $getter;
68
69
    /**
70
     * Getter-method to access an identifier of related object
71
     *
72
     * @var string
73
     */
74
    protected $identifierGetter;
75
76
    /**
77
     * Type of related resource
78
     *
79
     * @var string
80
     */
81
    protected $resourceType;
82
83
    /**
84
     * Include data with resource-identifier(s)
85
     *
86
     * @var bool
87
     */
88
    protected $includeData = false;
89
90
    /**
91
     * Limit amount of resource identifiers of collection
92
     * Works only with "x-to-many" type of relation.
93
     *
94
     * @var int
95
     */
96
    protected $dataLimit = 0;
97
98
    /**
99
     * Relationship constructor.
100
     *
101
     * @param string $name
102
     * @param int    $type
103
     */
104 12
    public function __construct(string $name, int $type)
105
    {
106 12
        $this->name = $name;
107 12
        $this->type = $type;
108 12
    }
109
110
    /**
111
     * Get name
112
     *
113
     * @return string
114
     */
115 6
    public function getName(): string
116
    {
117 6
        return $this->name;
118
    }
119
120
    /**
121
     * Get metadata to include into document's relationship
122
     * [name => value]
123
     *
124
     * @return array
125
     */
126
    public function getMetadata(): array
127
    {
128
        return $this->metadata;
129
    }
130
131
    /**
132
     * Is x-to-many type of relationship ?
133
     *
134
     * @return bool
135
     */
136 3
    public function isCollection(): bool
137
    {
138 3
        return $this->type === self::TYPE_X_TO_MANY;
139
    }
140
141
    /**
142
     * Set name of property contains related object
143
     *
144
     * @param string $name
145
     */
146 6
    public function setPropertyName(string $name)
147
    {
148 6
        $this->propertyName = $name;
149 6
    }
150
151
    /**
152
     * Has name of property ?
153
     *
154
     * @return bool
155
     */
156 1
    public function hasPropertyName(): bool
157
    {
158 1
        return $this->propertyName !== null;
159
    }
160
161
    /**
162
     * Get name of property contains related object
163
     *
164
     * @return string
165
     */
166 5
    public function getPropertyName(): string
167
    {
168 5
        return $this->propertyName;
169
    }
170
171
    /**
172
     * Set name of a getter-method to access related data
173
     *
174
     * @param string $method
175
     */
176 6
    public function setGetter(string $method)
177
    {
178 6
        $this->getter = $method;
179 6
    }
180
181
    /**
182
     * Contains name of a getter-method to access related data ?
183
     *
184
     * @return bool
185
     */
186 1
    public function hasGetter(): bool
187
    {
188 1
        return $this->getter !== null;
189
    }
190
191
    /**
192
     * Get name of a getter-method to access related data
193
     *
194
     * @return string
195
     */
196 3
    public function getGetter(): string
197
    {
198 3
        return $this->getter;
199
    }
200
201
    /**
202
     * Set getter-method to access an identifier of related object
203
     *
204
     * @param string $method
205
     */
206 5
    public function setIdentifierGetter(string $method)
207
    {
208 5
        $this->identifierGetter = $method;
209 5
    }
210
211
    /**
212
     * Has identifier type defined ?
213
     *
214
     * @return bool
215
     */
216 3
    public function hasIdentifierGetter(): bool
217
    {
218 3
        return $this->identifierGetter !== null;
219
    }
220
221
    /**
222
     * Get getter-method to access an identifier of related object
223
     *
224
     * @return string
225
     */
226 3
    public function getIdentifierGetter(): string
227
    {
228 3
        return $this->identifierGetter;
229
    }
230
231
    /**
232
     * Set type of resource
233
     *
234
     * @param string $type
235
     */
236 5
    public function setResourceType(string $type)
237
    {
238 5
        $this->resourceType = $type;
239 5
    }
240
241
    /**
242
     * Has resource type defined ?
243
     *
244
     * @return bool
245
     */
246 3
    public function hasResourceType(): bool
247
    {
248 3
        return $this->resourceType !== null;
249
    }
250
251
    /**
252
     * Get type of resource
253
     *
254
     * @return string
255
     */
256 3
    public function getResourceType(): string
257
    {
258 3
        return $this->resourceType;
259
    }
260
261
    /**
262
     * Set include-data option
263
     *
264
     * @param bool $include
265
     */
266 1
    public function setIncludeData(bool $include = true)
267
    {
268 1
        $this->includeData = $include;
269 1
    }
270
271
    /**
272
     * Is data-section with resource-identifier(s) have to be included ?
273
     *
274
     * @return bool
275
     */
276 1
    public function isDataIncluded(): bool
277
    {
278 1
        return $this->includeData;
279
    }
280
281
    /**
282
     * Set limit of amount of resource-objects in data-section
283
     * Works only for "x-to-many" type of relationship with included data allowed
284
     *
285
     * @param int $limit
286
     */
287 1
    public function setDataLimit(int $limit)
288
    {
289 1
        $this->dataLimit = $limit;
290 1
    }
291
292
    /**
293
     * Get limit of amount of resource-objects in data-section
294
     * Has a sense only for "x-to-many" type of relationship with included data allowed
295
     *
296
     * @return int
297
     */
298 1
    public function getDataLimit(): int
299
    {
300 1
        return $this->dataLimit;
301
    }
302
}