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
|
|
|
* Relationship constructor. |
85
|
|
|
* |
86
|
|
|
* @param string $name |
87
|
|
|
* @param int $type |
88
|
|
|
*/ |
89
|
6 |
|
public function __construct(string $name, int $type) |
90
|
|
|
{ |
91
|
6 |
|
$this->name = $name; |
92
|
6 |
|
$this->type = $type; |
93
|
6 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get name |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
6 |
|
public function getName(): string |
101
|
|
|
{ |
102
|
6 |
|
return $this->name; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get metadata to include into document's relationship |
107
|
|
|
* [name => value] |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getMetadata(): array |
112
|
|
|
{ |
113
|
|
|
return $this->metadata; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Is x-to-many type of relationship ? |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
3 |
|
public function isCollection(): bool |
122
|
|
|
{ |
123
|
3 |
|
return $this->type === self::TYPE_X_TO_MANY; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set name of property contains related object |
128
|
|
|
* |
129
|
|
|
* @param string $name |
130
|
|
|
*/ |
131
|
5 |
|
public function setPropertyName(string $name) |
132
|
|
|
{ |
133
|
5 |
|
$this->propertyName = $name; |
134
|
5 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Has name of property ? |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function hasPropertyName(): bool |
142
|
|
|
{ |
143
|
|
|
return $this->propertyName !== null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get name of property contains related object |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
4 |
|
public function getPropertyName(): string |
152
|
|
|
{ |
153
|
4 |
|
return $this->propertyName; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set name of a getter-method to access related data |
158
|
|
|
* |
159
|
|
|
* @param string $method |
160
|
|
|
*/ |
161
|
5 |
|
public function setGetter(string $method) |
162
|
|
|
{ |
163
|
5 |
|
$this->getter = $method; |
164
|
5 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Contains name of a getter-method to access related data ? |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function hasGetter(): bool |
172
|
|
|
{ |
173
|
|
|
return $this->getter !== null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get name of a getter-method to access related data |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
2 |
|
public function getGetter(): string |
182
|
|
|
{ |
183
|
2 |
|
return $this->getter; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set getter-method to access an identifier of related object |
188
|
|
|
* |
189
|
|
|
* @param string $method |
190
|
|
|
*/ |
191
|
4 |
|
public function setIdentifierGetter(string $method) |
192
|
|
|
{ |
193
|
4 |
|
$this->identifierGetter = $method; |
194
|
4 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Has identifier type defined ? |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
2 |
|
public function hasIdentifierGetter(): bool |
202
|
|
|
{ |
203
|
2 |
|
return $this->identifierGetter !== null; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get getter-method to access an identifier of related object |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
2 |
|
public function getIdentifierGetter(): string |
212
|
|
|
{ |
213
|
2 |
|
return $this->identifierGetter; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Set type of resource |
218
|
|
|
* |
219
|
|
|
* @param string $type |
220
|
|
|
*/ |
221
|
4 |
|
public function setResourceType(string $type) |
222
|
|
|
{ |
223
|
4 |
|
$this->resourceType = $type; |
224
|
4 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Has resource type defined ? |
228
|
|
|
* |
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
2 |
|
public function hasResourceType(): bool |
232
|
|
|
{ |
233
|
2 |
|
return $this->resourceType !== null; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get type of resource |
238
|
|
|
* |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
2 |
|
public function getResourceType(): string |
242
|
|
|
{ |
243
|
2 |
|
return $this->resourceType; |
244
|
|
|
} |
245
|
|
|
} |