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