|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Definition of attribute |
|
8
|
|
|
* |
|
9
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition |
|
10
|
|
|
*/ |
|
11
|
|
|
class Attribute |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Name of property contains related object. |
|
15
|
|
|
* Value is optional. Can be set only for real properties. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $propertyName; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Unique name of serialized attribute |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $name; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Data-type |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $type; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Attribute is an iterable container of values |
|
37
|
|
|
* |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $many; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Parameters for data-type handler |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $typeParameters = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Getter-method to access value |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $getter; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Setter-method to access value |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $setter; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Process null-values: |
|
65
|
|
|
* - add to resource attribute with null-value |
|
66
|
|
|
* - set to object null value from resources |
|
67
|
|
|
* |
|
68
|
|
|
* @var bool |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $processNull; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Attribute constructor. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* @param string $getter |
|
77
|
|
|
*/ |
|
78
|
12 |
|
public function __construct(string $name, string $getter) |
|
79
|
|
|
{ |
|
80
|
12 |
|
$this->name = $name; |
|
81
|
12 |
|
$this->getter = $getter; |
|
82
|
12 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get name |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
5 |
|
public function getName(): string |
|
90
|
|
|
{ |
|
91
|
5 |
|
return $this->name; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get name of getter-method to access value of attribute |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
4 |
|
public function getGetter(): string |
|
100
|
|
|
{ |
|
101
|
4 |
|
return $this->getter; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set name of setter-method to access value of attribute |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function setSetter(string $name) |
|
110
|
|
|
{ |
|
111
|
3 |
|
$this->setter = $name; |
|
112
|
3 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Has setter-method defined ? |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public function hasSetter(): bool |
|
120
|
|
|
{ |
|
121
|
3 |
|
return $this->setter !== null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get name of setter-method to access value of attribute |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function getSetter(): string |
|
130
|
|
|
{ |
|
131
|
3 |
|
return $this->setter; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set flag "processNull" |
|
136
|
|
|
* |
|
137
|
|
|
* @param bool $process |
|
138
|
|
|
*/ |
|
139
|
3 |
|
public function setProcessNull($process = true) |
|
140
|
|
|
{ |
|
141
|
3 |
|
$this->processNull = $process; |
|
142
|
3 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get value of "processNull" flag |
|
146
|
|
|
* |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
3 |
|
public function getProcessNull(): bool |
|
150
|
|
|
{ |
|
151
|
3 |
|
return $this->processNull !== null && $this->processNull; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Has flag "processNull" defined |
|
156
|
|
|
* |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function hasProcessNull(): bool |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->processNull !== null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Set name of data-type |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $type |
|
168
|
|
|
*/ |
|
169
|
5 |
|
public function setType(string $type) |
|
170
|
|
|
{ |
|
171
|
5 |
|
$this->type = $type; |
|
172
|
5 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Has data-type defined ? |
|
176
|
|
|
* |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function hasType(): bool |
|
180
|
|
|
{ |
|
181
|
1 |
|
return $this->type !== null; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get name of data-type |
|
186
|
|
|
* |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
6 |
|
public function getType(): string |
|
190
|
|
|
{ |
|
191
|
6 |
|
return $this->type; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Set "many" flag |
|
196
|
|
|
* |
|
197
|
|
|
* @param bool $many |
|
198
|
|
|
*/ |
|
199
|
3 |
|
public function setMany($many = true) |
|
200
|
|
|
{ |
|
201
|
3 |
|
$this->many = $many; |
|
202
|
3 |
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Attribute is an iterable container of values ? |
|
206
|
|
|
* |
|
207
|
|
|
* @return bool |
|
208
|
|
|
*/ |
|
209
|
4 |
|
public function isMany(): bool |
|
210
|
|
|
{ |
|
211
|
4 |
|
return $this->many !== null && $this->many; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Flag "many" has been defined |
|
216
|
|
|
* |
|
217
|
|
|
* @return bool |
|
218
|
|
|
*/ |
|
219
|
|
|
public function hasManyDefined(): bool |
|
220
|
|
|
{ |
|
221
|
|
|
return $this->many !== null; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Set parameters of data-type handling |
|
226
|
|
|
* |
|
227
|
|
|
* @param array $parameters |
|
228
|
|
|
*/ |
|
229
|
5 |
|
public function setTypeParameters(array $parameters) |
|
230
|
|
|
{ |
|
231
|
5 |
|
$this->typeParameters = $parameters; |
|
232
|
5 |
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Get parameters of data-type handling |
|
236
|
|
|
* |
|
237
|
|
|
* @return array |
|
238
|
|
|
*/ |
|
239
|
6 |
|
public function getTypeParameters(): array |
|
240
|
|
|
{ |
|
241
|
6 |
|
return $this->typeParameters; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Set name of property contains related object |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $name |
|
248
|
|
|
*/ |
|
249
|
3 |
|
public function setPropertyName(string $name) |
|
250
|
|
|
{ |
|
251
|
3 |
|
$this->propertyName = $name; |
|
252
|
3 |
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Has name of property ? |
|
256
|
|
|
* |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
3 |
|
public function hasPropertyName(): bool |
|
260
|
|
|
{ |
|
261
|
3 |
|
return $this->propertyName !== null; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Get name of property contains related object |
|
266
|
|
|
* |
|
267
|
|
|
* @return string |
|
268
|
|
|
*/ |
|
269
|
4 |
|
public function getPropertyName(): string |
|
270
|
|
|
{ |
|
271
|
4 |
|
return $this->propertyName; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Merge a attribute into this one |
|
276
|
|
|
* |
|
277
|
|
|
* @param self $attribute |
|
278
|
|
|
*/ |
|
279
|
1 |
|
public function merge(self $attribute) |
|
280
|
|
|
{ |
|
281
|
1 |
|
if ($this->propertyName === null && $attribute->hasPropertyName()) { |
|
282
|
1 |
|
$this->propertyName = $attribute->getPropertyName(); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
1 |
|
if ($this->type === null && $attribute->hasType()) { |
|
286
|
1 |
|
$this->type = $attribute->getType(); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
1 |
|
if ($this->many === null && $attribute->hasManyDefined()) { |
|
290
|
1 |
|
$this->many = $attribute->isMany(); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
1 |
|
if (empty($this->typeParameters)) { |
|
294
|
1 |
|
$this->typeParameters = $attribute->getTypeParameters(); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
1 |
|
if ($this->processNull === null && $attribute->hasProcessNull()) { |
|
298
|
|
|
$this->processNull = $attribute->getProcessNull(); |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
} |