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
|
|
|
* Name of resource inside of relationships-object |
18
|
|
|
* |
19
|
|
|
* { |
20
|
|
|
* "author": { <--- Name of relationship |
21
|
|
|
* "data": { |
22
|
|
|
* "id": "12345", |
23
|
|
|
* "type": "Author" |
24
|
|
|
* } |
25
|
|
|
* } |
26
|
|
|
* } |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Type or relationship: "x to one" or "x to many". |
34
|
|
|
* |
35
|
|
|
* @see for TYPE_* constants. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $type; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Collection of links |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $links = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Extra metadata |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $metadata = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Name of property contains related object. |
57
|
|
|
* Value is optional. Can be set only for real properties. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $propertyName; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Getter-method to access related data |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $getter; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Getter-method to access an identifier of related object |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $identifierGetter; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Type of related resource |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $resourceType; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Relationship constructor. |
86
|
|
|
* |
87
|
|
|
* @param string $name |
88
|
|
|
* @param int $type |
89
|
|
|
*/ |
90
|
2 |
|
public function __construct(string $name, int $type) |
91
|
|
|
{ |
92
|
2 |
|
$this->name = $name; |
93
|
2 |
|
$this->type = $type; |
94
|
2 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get name |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
2 |
|
public function getName(): string |
102
|
|
|
{ |
103
|
2 |
|
return $this->name; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set link |
108
|
|
|
* |
109
|
|
|
* @param string $name |
110
|
|
|
* @param LinkInterface $link |
111
|
|
|
*/ |
112
|
|
|
public function setLink(string $name, LinkInterface $link) |
113
|
|
|
{ |
114
|
|
|
if (isset($this->links[$name])) { |
115
|
|
|
throw new \LogicException(sprintf('A link name by "%s" is already exists for the relationship')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->links[$name] = $link; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get links |
123
|
|
|
* [name => link-object] |
124
|
|
|
* |
125
|
|
|
* @return LinkInterface[] |
126
|
|
|
*/ |
127
|
|
|
public function getLinks(): array |
128
|
|
|
{ |
129
|
|
|
$this->links; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get metadata to include into document's relationship |
134
|
|
|
* [name => value] |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getMetadata(): array |
139
|
|
|
{ |
140
|
|
|
return $this->metadata; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Is x-to-many type of relationship ? |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
2 |
|
public function isCollection(): bool |
149
|
|
|
{ |
150
|
2 |
|
return $this->type === self::TYPE_X_TO_MANY; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set name of property contains related object |
155
|
|
|
* |
156
|
|
|
* @param string $name |
157
|
|
|
*/ |
158
|
2 |
|
public function setPropertyName(string $name) |
159
|
|
|
{ |
160
|
2 |
|
$this->propertyName = $name; |
161
|
2 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Has name of property ? |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function hasPropertyName(): bool |
169
|
|
|
{ |
170
|
|
|
return $this->propertyName !== null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get name of property contains related object |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
1 |
|
public function getPropertyName(): string |
179
|
|
|
{ |
180
|
1 |
|
return $this->propertyName; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set name of a getter-method to access related data |
185
|
|
|
* |
186
|
|
|
* @param string $method |
187
|
|
|
*/ |
188
|
2 |
|
public function setGetter(string $method) |
189
|
|
|
{ |
190
|
2 |
|
$this->getter = $method; |
191
|
2 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get name of a getter-method to access related data |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
2 |
|
public function getGetter(): string |
199
|
|
|
{ |
200
|
2 |
|
return $this->getter; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Set getter-method to access an identifier of related object |
205
|
|
|
* |
206
|
|
|
* @param string $method |
207
|
|
|
*/ |
208
|
2 |
|
public function setIdentifierGetter(string $method) |
209
|
|
|
{ |
210
|
2 |
|
$this->identifierGetter = $method; |
211
|
2 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Has identifier type defined ? |
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
2 |
|
public function hasIdentifierGetter(): bool |
219
|
|
|
{ |
220
|
2 |
|
return $this->identifierGetter !== null; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get getter-method to access an identifier of related object |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
2 |
|
public function getIdentifierGetter(): string |
229
|
|
|
{ |
230
|
2 |
|
return $this->identifierGetter; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Set type of resource |
235
|
|
|
* |
236
|
|
|
* @param string $type |
237
|
|
|
*/ |
238
|
2 |
|
public function setResourceType(string $type) |
239
|
|
|
{ |
240
|
2 |
|
$this->resourceType = $type; |
241
|
2 |
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Has resource type defined ? |
245
|
|
|
* |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
2 |
|
public function hasResourceType(): bool |
249
|
|
|
{ |
250
|
2 |
|
return $this->resourceType !== null; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Get type of resource |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
2 |
|
public function getResourceType(): string |
259
|
|
|
{ |
260
|
2 |
|
return $this->resourceType; |
261
|
|
|
} |
262
|
|
|
} |