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

Relationship::hasResourceType()   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 13
    public function __construct(string $name, int $type)
91
    {
92 13
        $this->name = $name;
93 13
        $this->type = $type;
94 13
    }
95
96
    /**
97
     * Get name
98
     *
99
     * @return string
100
     */
101 9
    public function getName(): string
102
    {
103 9
        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
     * Set name of property contains related object
129
     *
130
     * @param string $name
131
     */
132 9
    public function setPropertyName(string $name)
133
    {
134 9
        $this->propertyName = $name;
135 9
    }
136
137
    /**
138
     * Has name of property ?
139
     *
140
     * @return bool
141
     */
142 1
    public function hasPropertyName(): bool
143
    {
144 1
        return $this->propertyName !== null;
145
    }
146
147
    /**
148
     * Get name of property contains related object
149
     *
150
     * @return string
151
     */
152 8
    public function getPropertyName(): string
153
    {
154 8
        return $this->propertyName;
155
    }
156
157
    /**
158
     * Set name of a getter-method to access related data
159
     *
160
     * @param string $method
161
     */
162 9
    public function setGetter(string $method)
163
    {
164 9
        $this->getter = $method;
165 9
    }
166
167
    /**
168
     * Contains name of a getter-method to access related data ?
169
     *
170
     * @return bool
171
     */
172 1
    public function hasGetter(): bool
173
    {
174 1
        return $this->getter !== null;
175
    }
176
177
    /**
178
     * Get name of a getter-method to access related data
179
     *
180
     * @return string
181
     */
182 3
    public function getGetter(): string
183
    {
184 3
        return $this->getter;
185
    }
186
187
    /**
188
     * Set include-data option
189
     *
190
     * @param bool $include
191
     */
192 9
    public function setIncludeData(bool $include = true)
193
    {
194 9
        $this->includeData = $include;
195 9
    }
196
197
    /**
198
     * Is data-section with resource-identifier(s) have to be included ?
199
     *
200
     * @return bool
201
     */
202 3
    public function isDataIncluded(): bool
203
    {
204 3
        return $this->includeData;
205
    }
206
207
    /**
208
     * Set limit of amount of resource-objects in data-section
209
     * Works only for "x-to-many" type of relationship with included data allowed
210
     *
211
     * @param int $limit
212
     */
213 9
    public function setDataLimit(int $limit)
214
    {
215 9
        $this->dataLimit = $limit;
216 9
    }
217
218
    /**
219
     * Get limit of amount of resource-objects in data-section
220
     * Has a sense only for "x-to-many" type of relationship with included data allowed
221
     *
222
     * @return int
223
     */
224 3
    public function getDataLimit(): int
225
    {
226 3
        return $this->dataLimit;
227
    }
228
}