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
|
|
|
* Parameters for data-type handler |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $typeParameters = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Getter-method to access value |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $getter; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Setter-method to access value |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $setter; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Attribute constructor. |
58
|
|
|
* |
59
|
|
|
* @param string $name |
60
|
|
|
* @param string $getter |
61
|
|
|
*/ |
62
|
10 |
|
public function __construct(string $name, string $getter) |
63
|
|
|
{ |
64
|
10 |
|
$this->name = $name; |
65
|
10 |
|
$this->getter = $getter; |
66
|
10 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get name |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
5 |
|
public function getName(): string |
74
|
|
|
{ |
75
|
5 |
|
return $this->name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get name of getter-method to access value of attribute |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
4 |
|
public function getGetter(): string |
84
|
|
|
{ |
85
|
4 |
|
return $this->getter; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set name of setter-method to access value of attribute |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
*/ |
93
|
3 |
|
public function setSetter(string $name) |
94
|
|
|
{ |
95
|
3 |
|
$this->setter = $name; |
96
|
3 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Has setter-method defined ? |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
3 |
|
public function hasSetter(): bool |
104
|
|
|
{ |
105
|
3 |
|
return $this->setter !== null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get name of setter-method to access value of attribute |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
3 |
|
public function getSetter(): string |
114
|
|
|
{ |
115
|
3 |
|
return $this->setter; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set name of data-type |
120
|
|
|
* |
121
|
|
|
* @param string $type |
122
|
|
|
*/ |
123
|
5 |
|
public function setType(string $type) |
124
|
|
|
{ |
125
|
5 |
|
$this->type = $type; |
126
|
5 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Has data-type defined ? |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
1 |
|
public function hasType(): bool |
134
|
|
|
{ |
135
|
1 |
|
return $this->type !== null; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get name of data-type |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
6 |
|
public function getType(): string |
144
|
|
|
{ |
145
|
6 |
|
return $this->type; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set parameters of data-type handling |
150
|
|
|
* |
151
|
|
|
* @param array $parameters |
152
|
|
|
*/ |
153
|
5 |
|
public function setTypeParameters(array $parameters) |
154
|
|
|
{ |
155
|
5 |
|
$this->typeParameters = $parameters; |
156
|
5 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get parameters of data-type handling |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
6 |
|
public function getTypeParameters(): array |
164
|
|
|
{ |
165
|
6 |
|
return $this->typeParameters; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set name of property contains related object |
170
|
|
|
* |
171
|
|
|
* @param string $name |
172
|
|
|
*/ |
173
|
3 |
|
public function setPropertyName(string $name) |
174
|
|
|
{ |
175
|
3 |
|
$this->propertyName = $name; |
176
|
3 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Has name of property ? |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
3 |
|
public function hasPropertyName(): bool |
184
|
|
|
{ |
185
|
3 |
|
return $this->propertyName !== null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get name of property contains related object |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
4 |
|
public function getPropertyName(): string |
194
|
|
|
{ |
195
|
4 |
|
return $this->propertyName; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Merge a attribute into this one |
200
|
|
|
* |
201
|
|
|
* @param self $attribute |
202
|
|
|
*/ |
203
|
1 |
|
public function merge(self $attribute) |
204
|
|
|
{ |
205
|
1 |
|
if ($this->propertyName === null && $attribute->hasPropertyName()) { |
206
|
1 |
|
$this->propertyName = $attribute->getPropertyName(); |
207
|
|
|
} |
208
|
|
|
|
209
|
1 |
|
if ($this->type === null && $attribute->hasType()) { |
210
|
1 |
|
$this->type = $attribute->getType(); |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
if (empty($this->typeParameters)) { |
214
|
1 |
|
$this->typeParameters = $attribute->getTypeParameters(); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |