1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Mapping; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\Accessor\PropertyAccessor; |
8
|
|
|
use Chubbyphp\Deserialization\Denormalizer\CallbackFieldDenormalizer; |
9
|
|
|
use Chubbyphp\Deserialization\Denormalizer\ConvertTypeFieldDenormalizer; |
10
|
|
|
use Chubbyphp\Deserialization\Denormalizer\DateTimeFieldDenormalizer; |
11
|
|
|
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizer; |
12
|
|
|
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; |
13
|
|
|
use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedManyFieldDenormalizer; |
14
|
|
|
use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedOneFieldDenormalizer; |
15
|
|
|
use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceManyFieldDenormalizer; |
16
|
|
|
use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceOneFieldDenormalizer; |
17
|
|
|
use Chubbyphp\Deserialization\Policy\NullPolicy; |
18
|
|
|
use Chubbyphp\Deserialization\Policy\PolicyInterface; |
19
|
|
|
|
20
|
|
|
final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $name; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @deprecated |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $groups = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FieldDenormalizerInterface |
36
|
|
|
*/ |
37
|
|
|
private $fieldDenormalizer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var PolicyInterface|null |
41
|
|
|
*/ |
42
|
|
|
private $policy; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $name |
46
|
|
|
*/ |
47
|
15 |
|
private function __construct(string $name) |
48
|
|
|
{ |
49
|
15 |
|
$this->name = $name; |
50
|
15 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $name |
54
|
|
|
* @param bool $emptyToNull |
55
|
|
|
* @param FieldDenormalizerInterface|null |
56
|
|
|
* |
57
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
58
|
|
|
*/ |
59
|
4 |
View Code Duplication |
public static function create( |
|
|
|
|
60
|
|
|
string $name, |
61
|
|
|
bool $emptyToNull = false, |
62
|
|
|
FieldDenormalizerInterface $fieldDenormalizer = null |
63
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
64
|
4 |
|
if (null === $fieldDenormalizer) { |
65
|
3 |
|
$fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name), $emptyToNull); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
$self = new self($name); |
69
|
4 |
|
$self->fieldDenormalizer = $fieldDenormalizer; |
70
|
|
|
|
71
|
4 |
|
return $self; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $name |
76
|
|
|
* @param callable $callback |
77
|
|
|
* |
78
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
79
|
|
|
*/ |
80
|
1 |
|
public static function createCallback(string $name, callable $callback): DenormalizationFieldMappingBuilderInterface |
81
|
|
|
{ |
82
|
1 |
|
$self = new self($name); |
83
|
1 |
|
$self->fieldDenormalizer = new CallbackFieldDenormalizer($callback); |
84
|
|
|
|
85
|
1 |
|
return $self; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $name |
90
|
|
|
* @param string $type |
91
|
|
|
* @param bool $emptyToNull |
92
|
|
|
* |
93
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
94
|
|
|
*/ |
95
|
2 |
|
public static function createConvertType( |
96
|
|
|
string $name, |
97
|
|
|
string $type, |
98
|
|
|
bool $emptyToNull = false |
99
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
100
|
2 |
|
$self = new self($name); |
101
|
2 |
|
$self->fieldDenormalizer = new ConvertTypeFieldDenormalizer(new PropertyAccessor($name), $type, $emptyToNull); |
102
|
|
|
|
103
|
2 |
|
return $self; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $name |
108
|
|
|
* @param bool $emptyToNull |
109
|
|
|
* @param \DateTimeZone $dateTimeZone |
110
|
|
|
* |
111
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
112
|
|
|
*/ |
113
|
3 |
View Code Duplication |
public static function createDateTime( |
|
|
|
|
114
|
|
|
string $name, |
115
|
|
|
bool $emptyToNull = false, |
116
|
|
|
\DateTimeZone $dateTimeZone = null |
117
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
118
|
3 |
|
$self = new self($name); |
119
|
3 |
|
$self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name), $emptyToNull, $dateTimeZone); |
120
|
|
|
|
121
|
3 |
|
return $self; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $name |
126
|
|
|
* @param string $class |
127
|
|
|
* |
128
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
129
|
|
|
*/ |
130
|
1 |
View Code Duplication |
public static function createEmbedMany(string $name, string $class): DenormalizationFieldMappingBuilderInterface |
|
|
|
|
131
|
|
|
{ |
132
|
1 |
|
$self = new self($name); |
133
|
1 |
|
$self->fieldDenormalizer = new EmbedManyFieldDenormalizer($class, new PropertyAccessor($name)); |
134
|
|
|
|
135
|
1 |
|
return $self; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $name |
140
|
|
|
* @param string $class |
141
|
|
|
* |
142
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
143
|
|
|
*/ |
144
|
1 |
View Code Duplication |
public static function createEmbedOne(string $name, string $class): DenormalizationFieldMappingBuilderInterface |
|
|
|
|
145
|
|
|
{ |
146
|
1 |
|
$self = new self($name); |
147
|
1 |
|
$self->fieldDenormalizer = new EmbedOneFieldDenormalizer($class, new PropertyAccessor($name)); |
148
|
|
|
|
149
|
1 |
|
return $self; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $name |
154
|
|
|
* @param callable $repository |
155
|
|
|
* |
156
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
157
|
|
|
*/ |
158
|
1 |
|
public static function createReferenceMany( |
159
|
|
|
string $name, |
160
|
|
|
callable $repository |
161
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
162
|
1 |
|
$self = new self($name); |
163
|
1 |
|
$self->fieldDenormalizer = new ReferenceManyFieldDenormalizer($repository, new PropertyAccessor($name)); |
164
|
|
|
|
165
|
1 |
|
return $self; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $name |
170
|
|
|
* @param callable $repository |
171
|
|
|
* @param bool $emptyToNull |
172
|
|
|
* |
173
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
174
|
|
|
*/ |
175
|
2 |
|
public static function createReferenceOne( |
176
|
|
|
string $name, |
177
|
|
|
callable $repository, |
178
|
|
|
bool $emptyToNull = false |
179
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
180
|
2 |
|
$self = new self($name); |
181
|
2 |
|
$self->fieldDenormalizer = new ReferenceOneFieldDenormalizer( |
182
|
2 |
|
$repository, |
183
|
2 |
|
new PropertyAccessor($name), |
184
|
2 |
|
$emptyToNull |
185
|
|
|
); |
186
|
|
|
|
187
|
2 |
|
return $self; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @deprecated |
192
|
|
|
* |
193
|
|
|
* @param array $groups |
194
|
|
|
* |
195
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
196
|
|
|
*/ |
197
|
1 |
|
public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface |
198
|
|
|
{ |
199
|
1 |
|
$this->groups = $groups; |
|
|
|
|
200
|
|
|
|
201
|
1 |
|
return $this; |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param FieldDenormalizerInterface $fieldDenormalizer |
206
|
|
|
* |
207
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
208
|
|
|
*/ |
209
|
1 |
|
public function setFieldDenormalizer( |
210
|
|
|
FieldDenormalizerInterface $fieldDenormalizer |
211
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
212
|
1 |
|
@trigger_error( |
|
|
|
|
213
|
1 |
|
'Utilize third parameter of create method instead', |
214
|
1 |
|
E_USER_DEPRECATED |
215
|
|
|
); |
216
|
|
|
|
217
|
1 |
|
$this->fieldDenormalizer = $fieldDenormalizer; |
218
|
|
|
|
219
|
1 |
|
return $this; |
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param PolicyInterface $policy |
224
|
|
|
* |
225
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
226
|
|
|
*/ |
227
|
1 |
|
public function setPolicy(PolicyInterface $policy): DenormalizationFieldMappingBuilderInterface |
228
|
|
|
{ |
229
|
1 |
|
$this->policy = $policy; |
230
|
|
|
|
231
|
1 |
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return DenormalizationFieldMappingInterface |
236
|
|
|
*/ |
237
|
15 |
|
public function getMapping(): DenormalizationFieldMappingInterface |
238
|
|
|
{ |
239
|
15 |
|
return new DenormalizationFieldMapping( |
240
|
15 |
|
$this->name, |
241
|
15 |
|
$this->groups, |
|
|
|
|
242
|
15 |
|
$this->fieldDenormalizer, |
243
|
15 |
|
$this->policy ?? new NullPolicy() |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.