Passed
Push — master ( 7c7fa2...d7cd6a )
by Michael
04:35
created

Relationship::isCollection()   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
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
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
     * Include data with resource-identifier(s)
71
     *
72
     * @var bool
73
     */
74
    protected $includeData = false;
75
76
    /**
77
     * Limit amount of resource identifiers of collection
78
     * Works only with "x-to-many" type of relation.
79
     *
80
     * @var int
81
     */
82
    protected $dataLimit = 0;
83
84
    /**
85
     * Relationship constructor.
86
     *
87
     * @param string $name
88
     * @param int    $type
89
     */
90 15
    public function __construct(string $name, int $type)
91
    {
92 15
        $this->name = $name;
93 15
        $this->type = $type;
94 15
    }
95
96
    /**
97
     * Get name
98
     *
99
     * @return string
100
     */
101 10
    public function getName(): string
102
    {
103 10
        return $this->name;
104
    }
105
106
    /**
107
     * Get metadata to include into document's relationship
108
     * [name => value]
109
     *
110
     * @return array
111
     */
112
    public function getMetadata(): array
113
    {
114
        return $this->metadata;
115
    }
116
117
    /**
118
     * Is x-to-many type of relationship ?
119
     *
120
     * @return bool
121
     */
122 3
    public function isCollection(): bool
123
    {
124 3
        return $this->type === self::TYPE_X_TO_MANY;
125
    }
126
127
    /**
128
     * Get type of relationship
129
     * @see TYPE_X_TO_* constants
130
     *
131
     * @return int
132
     */
133
    public function getType(): int
134
    {
135
        return $this->type;
136
    }
137
138
    /**
139
     * Set name of property contains related object
140
     *
141
     * @param string $name
142
     */
143 10
    public function setPropertyName(string $name)
144
    {
145 10
        $this->propertyName = $name;
146 10
    }
147
148
    /**
149
     * Has name of property ?
150
     *
151
     * @return bool
152
     */
153 1
    public function hasPropertyName(): bool
154
    {
155 1
        return $this->propertyName !== null;
156
    }
157
158
    /**
159
     * Get name of property contains related object
160
     *
161
     * @return string
162
     */
163 9
    public function getPropertyName(): string
164
    {
165 9
        return $this->propertyName;
166
    }
167
168
    /**
169
     * Set name of a getter-method to access related data
170
     *
171
     * @param string $method
172
     */
173 10
    public function setGetter(string $method)
174
    {
175 10
        $this->getter = $method;
176 10
    }
177
178
    /**
179
     * Contains name of a getter-method to access related data ?
180
     *
181
     * @return bool
182
     */
183 1
    public function hasGetter(): bool
184
    {
185 1
        return $this->getter !== null;
186
    }
187
188
    /**
189
     * Get name of a getter-method to access related data
190
     *
191
     * @return string
192
     */
193 3
    public function getGetter(): string
194
    {
195 3
        return $this->getter;
196
    }
197
198
    /**
199
     * Set include-data option
200
     *
201
     * @param bool $include
202
     */
203 10
    public function setIncludeData(bool $include = true)
204
    {
205 10
        $this->includeData = $include;
206 10
    }
207
208
    /**
209
     * Is data-section with resource-identifier(s) have to be included ?
210
     *
211
     * @return bool
212
     */
213 3
    public function isDataIncluded(): bool
214
    {
215 3
        return $this->includeData;
216
    }
217
218
    /**
219
     * Set limit of amount of resource-objects in data-section
220
     * Works only for "x-to-many" type of relationship with included data allowed
221
     *
222
     * @param int $limit
223
     */
224 10
    public function setDataLimit(int $limit)
225
    {
226 10
        $this->dataLimit = $limit;
227 10
    }
228
229
    /**
230
     * Get limit of amount of resource-objects in data-section
231
     * Has a sense only for "x-to-many" type of relationship with included data allowed
232
     *
233
     * @return int
234
     */
235 3
    public function getDataLimit(): int
236
    {
237 3
        return $this->dataLimit;
238
    }
239
240
    /**
241
     * Merge a relationship into this one
242
     *
243
     * @param self $relationship
244
     */
245 1
    public function merge(self $relationship)
246
    {
247 1
        $this->mergeLinks($relationship);
248
    }
249
}