|
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
|
|
|
private function __construct(string $name) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->name = $name; |
|
47
|
15 |
|
} |
|
48
|
|
|
|
|
49
|
15 |
|
public static function create( |
|
50
|
15 |
|
string $name, |
|
51
|
|
|
bool $emptyToNull = false, |
|
52
|
|
|
FieldDenormalizerInterface $fieldDenormalizer = null |
|
53
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
|
54
|
|
|
if (null === $fieldDenormalizer) { |
|
55
|
|
|
$fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name), $emptyToNull); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$self = new self($name); |
|
59
|
4 |
|
$self->fieldDenormalizer = $fieldDenormalizer; |
|
60
|
|
|
|
|
61
|
|
|
return $self; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
public static function createCallback(string $name, callable $callback): DenormalizationFieldMappingBuilderInterface |
|
65
|
3 |
|
{ |
|
66
|
|
|
$self = new self($name); |
|
67
|
|
|
$self->fieldDenormalizer = new CallbackFieldDenormalizer($callback); |
|
68
|
4 |
|
|
|
69
|
4 |
|
return $self; |
|
70
|
|
|
} |
|
71
|
4 |
|
|
|
72
|
|
|
public static function createConvertType( |
|
73
|
|
|
string $name, |
|
74
|
|
|
string $type, |
|
75
|
|
|
bool $emptyToNull = false |
|
76
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
|
77
|
|
|
$self = new self($name); |
|
78
|
|
|
$self->fieldDenormalizer = new ConvertTypeFieldDenormalizer(new PropertyAccessor($name), $type, $emptyToNull); |
|
79
|
|
|
|
|
80
|
1 |
|
return $self; |
|
81
|
|
|
} |
|
82
|
1 |
|
|
|
83
|
1 |
|
/** |
|
84
|
|
|
* @param \DateTimeZone $dateTimeZone |
|
85
|
1 |
|
*/ |
|
86
|
|
|
public static function createDateTime( |
|
87
|
|
|
string $name, |
|
88
|
|
|
bool $emptyToNull = false, |
|
89
|
|
|
\DateTimeZone $dateTimeZone = null |
|
90
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
|
91
|
|
|
$self = new self($name); |
|
92
|
|
|
$self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name), $emptyToNull, $dateTimeZone); |
|
93
|
|
|
|
|
94
|
|
|
return $self; |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function createEmbedMany(string $name, string $class): DenormalizationFieldMappingBuilderInterface |
|
98
|
|
|
{ |
|
99
|
|
|
$self = new self($name); |
|
100
|
2 |
|
$self->fieldDenormalizer = new EmbedManyFieldDenormalizer($class, new PropertyAccessor($name)); |
|
101
|
2 |
|
|
|
102
|
|
|
return $self; |
|
103
|
2 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function createEmbedOne(string $name, string $class): DenormalizationFieldMappingBuilderInterface |
|
106
|
|
|
{ |
|
107
|
|
|
$self = new self($name); |
|
108
|
|
|
$self->fieldDenormalizer = new EmbedOneFieldDenormalizer($class, new PropertyAccessor($name)); |
|
109
|
|
|
|
|
110
|
|
|
return $self; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
3 |
|
public static function createReferenceMany( |
|
114
|
|
|
string $name, |
|
115
|
|
|
callable $repository |
|
116
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
|
117
|
|
|
$self = new self($name); |
|
118
|
3 |
|
$self->fieldDenormalizer = new ReferenceManyFieldDenormalizer($repository, new PropertyAccessor($name)); |
|
119
|
3 |
|
|
|
120
|
|
|
return $self; |
|
121
|
3 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
public static function createReferenceOne( |
|
124
|
|
|
string $name, |
|
125
|
|
|
callable $repository, |
|
126
|
|
|
bool $emptyToNull = false |
|
127
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
|
128
|
|
|
$self = new self($name); |
|
129
|
|
|
$self->fieldDenormalizer = new ReferenceOneFieldDenormalizer( |
|
130
|
1 |
|
$repository, |
|
131
|
|
|
new PropertyAccessor($name), |
|
132
|
1 |
|
$emptyToNull |
|
133
|
1 |
|
); |
|
134
|
|
|
|
|
135
|
1 |
|
return $self; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @deprecated |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface |
|
142
|
|
|
{ |
|
143
|
|
|
$this->groups = $groups; |
|
|
|
|
|
|
144
|
1 |
|
|
|
145
|
|
|
return $this; |
|
146
|
1 |
|
} |
|
147
|
1 |
|
|
|
148
|
|
|
public function setFieldDenormalizer( |
|
149
|
1 |
|
FieldDenormalizerInterface $fieldDenormalizer |
|
150
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
|
151
|
|
|
@trigger_error( |
|
152
|
|
|
'Utilize third parameter of create method instead', |
|
153
|
|
|
E_USER_DEPRECATED |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
$this->fieldDenormalizer = $fieldDenormalizer; |
|
157
|
|
|
|
|
158
|
1 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function setPolicy(PolicyInterface $policy): DenormalizationFieldMappingBuilderInterface |
|
162
|
1 |
|
{ |
|
163
|
1 |
|
$this->policy = $policy; |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function getMapping(): DenormalizationFieldMappingInterface |
|
169
|
|
|
{ |
|
170
|
|
|
return new DenormalizationFieldMapping( |
|
171
|
|
|
$this->name, |
|
172
|
|
|
$this->groups, |
|
|
|
|
|
|
173
|
|
|
$this->fieldDenormalizer, |
|
174
|
|
|
$this->policy ?? new NullPolicy() |
|
175
|
2 |
|
); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|