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