1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Serialization\Mapping; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Serialization\Accessor\PropertyAccessor; |
8
|
|
|
use Chubbyphp\Serialization\Normalizer\CallbackFieldNormalizer; |
9
|
|
|
use Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer; |
10
|
|
|
use Chubbyphp\Serialization\Normalizer\FieldNormalizer; |
11
|
|
|
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface; |
12
|
|
|
use Chubbyphp\Serialization\Normalizer\Relation\EmbedManyFieldNormalizer; |
13
|
|
|
use Chubbyphp\Serialization\Normalizer\Relation\EmbedOneFieldNormalizer; |
14
|
|
|
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceManyFieldNormalizer; |
15
|
|
|
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceOneFieldNormalizer; |
16
|
|
|
use Chubbyphp\Serialization\Policy\NullPolicy; |
17
|
|
|
use Chubbyphp\Serialization\Policy\PolicyInterface; |
18
|
|
|
|
19
|
|
|
final class NormalizationFieldMappingBuilder implements NormalizationFieldMappingBuilderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $groups = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FieldNormalizerInterface|null |
33
|
|
|
*/ |
34
|
|
|
private $fieldNormalizer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var PolicyInterface|null |
38
|
|
|
*/ |
39
|
|
|
private $policy; |
40
|
|
|
|
41
|
|
|
private function __construct(string $name) |
42
|
|
|
{ |
43
|
|
|
$this->name = $name; |
44
|
10 |
|
} |
45
|
|
|
|
46
|
10 |
|
public static function create( |
47
|
10 |
|
string $name, |
48
|
|
|
FieldNormalizerInterface $fieldNormalizer = null |
49
|
|
|
): NormalizationFieldMappingBuilderInterface { |
50
|
|
|
$self = new self($name); |
51
|
|
|
$self->fieldNormalizer = $fieldNormalizer; |
52
|
|
|
|
53
|
|
|
return $self; |
54
|
|
|
} |
55
|
3 |
|
|
56
|
|
|
public static function createCallback(string $name, callable $callback): NormalizationFieldMappingBuilderInterface |
57
|
|
|
{ |
58
|
|
|
$self = new self($name); |
59
|
3 |
|
$self->fieldNormalizer = new CallbackFieldNormalizer($callback); |
60
|
3 |
|
|
61
|
|
|
return $self; |
62
|
3 |
|
} |
63
|
|
|
|
64
|
|
|
public static function createDateTime(string $name, string $format = 'c'): NormalizationFieldMappingBuilderInterface |
65
|
|
|
{ |
66
|
|
|
$self = new self($name); |
67
|
|
|
$self->fieldNormalizer = new DateTimeFieldNormalizer(new PropertyAccessor($name), $format); |
68
|
|
|
|
69
|
|
|
return $self; |
70
|
|
|
} |
71
|
1 |
|
|
72
|
|
|
public static function createEmbedMany(string $name): NormalizationFieldMappingBuilderInterface |
73
|
1 |
|
{ |
74
|
1 |
|
$self = new self($name); |
75
|
|
|
$self->fieldNormalizer = new EmbedManyFieldNormalizer(new PropertyAccessor($name)); |
76
|
1 |
|
|
77
|
|
|
return $self; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function createEmbedOne(string $name): NormalizationFieldMappingBuilderInterface |
81
|
|
|
{ |
82
|
|
|
$self = new self($name); |
83
|
|
|
$self->fieldNormalizer = new EmbedOneFieldNormalizer(new PropertyAccessor($name)); |
84
|
|
|
|
85
|
2 |
|
return $self; |
86
|
|
|
} |
87
|
2 |
|
|
88
|
2 |
|
public static function createReferenceMany( |
89
|
|
|
string $name, |
90
|
2 |
|
string $idName = 'id' |
91
|
|
|
): NormalizationFieldMappingBuilderInterface { |
92
|
|
|
$self = new self($name); |
93
|
|
|
$self->fieldNormalizer = new ReferenceManyFieldNormalizer( |
94
|
|
|
new PropertyAccessor($idName), |
95
|
|
|
new PropertyAccessor($name) |
96
|
|
|
); |
97
|
|
|
|
98
|
1 |
|
return $self; |
99
|
|
|
} |
100
|
1 |
|
|
101
|
1 |
|
public static function createReferenceOne( |
102
|
|
|
string $name, |
103
|
1 |
|
string $idName = 'id' |
104
|
|
|
): NormalizationFieldMappingBuilderInterface { |
105
|
|
|
$self = new self($name); |
106
|
|
|
$self->fieldNormalizer = new ReferenceOneFieldNormalizer( |
107
|
|
|
new PropertyAccessor($idName), |
108
|
|
|
new PropertyAccessor($name) |
109
|
|
|
); |
110
|
|
|
|
111
|
1 |
|
return $self; |
112
|
|
|
} |
113
|
1 |
|
|
114
|
1 |
|
/** |
115
|
|
|
* @deprecated |
116
|
1 |
|
*/ |
117
|
|
|
public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface |
118
|
|
|
{ |
119
|
|
|
$this->groups = $groups; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
1 |
|
* @deprecated |
126
|
|
|
*/ |
127
|
|
|
public function setFieldNormalizer( |
128
|
|
|
FieldNormalizerInterface $fieldNormalizer |
129
|
1 |
|
): NormalizationFieldMappingBuilderInterface { |
130
|
1 |
|
@trigger_error( |
131
|
1 |
|
'Utilize second parameter of create method instead', |
132
|
1 |
|
E_USER_DEPRECATED |
133
|
|
|
); |
134
|
|
|
|
135
|
1 |
|
$this->fieldNormalizer = $fieldNormalizer; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function setPolicy(PolicyInterface $policy): NormalizationFieldMappingBuilderInterface |
141
|
|
|
{ |
142
|
|
|
$this->policy = $policy; |
143
|
|
|
|
144
|
1 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getMapping(): NormalizationFieldMappingInterface |
148
|
1 |
|
{ |
149
|
1 |
|
return new NormalizationFieldMapping( |
150
|
1 |
|
$this->name, |
151
|
1 |
|
$this->groups, |
152
|
|
|
$this->fieldNormalizer ?? new FieldNormalizer(new PropertyAccessor($this->name)), |
153
|
|
|
$this->policy ?? new NullPolicy() |
154
|
1 |
|
); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|