|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Diezz\YamlToObjectMapper; |
|
4
|
|
|
|
|
5
|
|
|
use Diezz\YamlToObjectMapper\Attributes\Constructor; |
|
6
|
|
|
use ReflectionProperty; |
|
7
|
|
|
|
|
8
|
|
|
class ClassField |
|
9
|
|
|
{ |
|
10
|
|
|
private string $name; |
|
11
|
|
|
private bool $required; |
|
12
|
|
|
private ?string $type = null; |
|
13
|
|
|
/** |
|
14
|
|
|
* List means that field has an array type |
|
15
|
|
|
* |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
private bool $isList = false; |
|
19
|
|
|
/** |
|
20
|
|
|
* Whether the list typed explicitly with #[Collection] attribute |
|
21
|
|
|
* |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private bool $isTypedCollection = false; |
|
25
|
|
|
private ?ClassInfo $classInfo = null; |
|
26
|
|
|
private string $setter; |
|
27
|
|
|
private bool $isPublic = true; |
|
28
|
|
|
private string $constructor = Constructor::DEFAULT_EMPTY; |
|
29
|
|
|
private bool $hasDefaultValue = false; |
|
30
|
|
|
private ?string $defaultValueResolver = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function isTypedCollection(): bool |
|
36
|
|
|
{ |
|
37
|
2 |
|
return $this->isTypedCollection; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param bool $isTypedCollection |
|
42
|
|
|
*/ |
|
43
|
7 |
|
public function setIsTypedCollection(bool $isTypedCollection): void |
|
44
|
|
|
{ |
|
45
|
7 |
|
$this->isTypedCollection = $isTypedCollection; |
|
46
|
7 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getConstructor(): string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->constructor; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $constructor |
|
58
|
|
|
* |
|
59
|
|
|
* @return ClassField |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setConstructor(string $constructor): ClassField |
|
62
|
|
|
{ |
|
63
|
|
|
$this->constructor = $constructor; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function getSetter(): string |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $this->setter; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $setter |
|
78
|
|
|
* |
|
79
|
|
|
* @return ClassField |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function setSetter(string $setter): ClassField |
|
82
|
|
|
{ |
|
83
|
2 |
|
$this->setter = $setter; |
|
84
|
|
|
|
|
85
|
2 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function isPublic(): bool |
|
92
|
|
|
{ |
|
93
|
2 |
|
return $this->isPublic; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param bool $isPublic |
|
98
|
|
|
* |
|
99
|
|
|
* @return ClassField |
|
100
|
|
|
*/ |
|
101
|
7 |
|
public function setIsPublic(bool $isPublic): ClassField |
|
102
|
|
|
{ |
|
103
|
7 |
|
$this->isPublic = $isPublic; |
|
104
|
|
|
|
|
105
|
7 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return ClassInfo|null |
|
110
|
|
|
*/ |
|
111
|
7 |
|
public function getClassInfo(): ?ClassInfo |
|
112
|
|
|
{ |
|
113
|
7 |
|
return $this->classInfo; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string|null |
|
118
|
|
|
*/ |
|
119
|
7 |
|
public function getType(): ?string |
|
120
|
|
|
{ |
|
121
|
7 |
|
return $this->type; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function isPrimitive(): bool |
|
125
|
|
|
{ |
|
126
|
|
|
return null === $this->classInfo && !$this->isList(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function isList(): bool |
|
133
|
|
|
{ |
|
134
|
2 |
|
return $this->isList; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param bool $isList |
|
139
|
|
|
* |
|
140
|
|
|
* @return ClassField |
|
141
|
|
|
*/ |
|
142
|
7 |
|
public function setIsList(bool $isList): ClassField |
|
143
|
|
|
{ |
|
144
|
7 |
|
$this->isList = $isList; |
|
145
|
|
|
|
|
146
|
7 |
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param ClassInfo|null $classInfo |
|
151
|
|
|
* |
|
152
|
|
|
* @return ClassField |
|
153
|
|
|
*/ |
|
154
|
1 |
|
public function setClassInfo(?ClassInfo $classInfo): ClassField |
|
155
|
|
|
{ |
|
156
|
1 |
|
$this->classInfo = $classInfo; |
|
157
|
|
|
|
|
158
|
1 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $name |
|
163
|
|
|
* |
|
164
|
|
|
* @return ClassField |
|
165
|
|
|
*/ |
|
166
|
7 |
|
public function setName(string $name): ClassField |
|
167
|
|
|
{ |
|
168
|
7 |
|
$this->name = $name; |
|
169
|
|
|
|
|
170
|
7 |
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param bool $required |
|
175
|
|
|
* |
|
176
|
|
|
* @return ClassField |
|
177
|
|
|
*/ |
|
178
|
7 |
|
public function setRequired(bool $required): ClassField |
|
179
|
|
|
{ |
|
180
|
7 |
|
$this->required = $required; |
|
181
|
|
|
|
|
182
|
7 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string $type |
|
187
|
|
|
* |
|
188
|
|
|
* @return ClassField |
|
189
|
|
|
*/ |
|
190
|
7 |
|
public function setType(string $type): ClassField |
|
191
|
|
|
{ |
|
192
|
7 |
|
$this->type = $type; |
|
193
|
|
|
|
|
194
|
7 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
1 |
|
public function getName(): string |
|
201
|
|
|
{ |
|
202
|
1 |
|
return $this->name; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return bool |
|
207
|
|
|
*/ |
|
208
|
3 |
|
public function isRequired(): bool |
|
209
|
|
|
{ |
|
210
|
3 |
|
return $this->required; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return bool |
|
215
|
|
|
*/ |
|
216
|
3 |
|
public function hasDefaultValue(): bool |
|
217
|
|
|
{ |
|
218
|
3 |
|
return $this->hasDefaultValue; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param bool $hasDefaultValue |
|
223
|
|
|
* |
|
224
|
|
|
* @return ClassField |
|
225
|
|
|
*/ |
|
226
|
7 |
|
public function setHasDefaultValue(bool $hasDefaultValue): ClassField |
|
227
|
|
|
{ |
|
228
|
7 |
|
$this->hasDefaultValue = $hasDefaultValue; |
|
229
|
|
|
|
|
230
|
7 |
|
return $this; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return bool |
|
235
|
|
|
*/ |
|
236
|
1 |
|
public function hasDefaultValueResolver(): bool |
|
237
|
|
|
{ |
|
238
|
1 |
|
return $this->defaultValueResolver !== null; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param string|null $defaultValueResolver |
|
243
|
|
|
* |
|
244
|
|
|
* @return ClassField |
|
245
|
|
|
*/ |
|
246
|
7 |
|
public function setDefaultValueResolver(?string $defaultValueResolver): ClassField |
|
247
|
|
|
{ |
|
248
|
7 |
|
$this->defaultValueResolver = $defaultValueResolver; |
|
249
|
|
|
|
|
250
|
7 |
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return string|null |
|
255
|
|
|
*/ |
|
256
|
3 |
|
public function getDefaultValueResolver(): ?string |
|
257
|
|
|
{ |
|
258
|
3 |
|
return $this->defaultValueResolver; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
public function newInstance($value = null): object |
|
262
|
|
|
{ |
|
263
|
|
|
if ($this->constructor === Constructor::STATIC_MAKE) { |
|
264
|
|
|
$callable = [$this->type, 'make']; |
|
265
|
|
|
if ($value !== null) { |
|
266
|
|
|
return $callable($value); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return $callable(); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return new $this->type; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|