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\DateTimeFieldDenormalizer; |
9
|
|
|
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizer; |
10
|
|
|
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; |
11
|
|
|
use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedManyFieldDenormalizer; |
12
|
|
|
use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedOneFieldDenormalizer; |
13
|
|
|
use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceManyFieldDenormalizer; |
14
|
|
|
use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceOneFieldDenormalizer; |
15
|
|
|
|
16
|
|
|
final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $groups = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var FieldDenormalizerInterface |
30
|
|
|
*/ |
31
|
|
|
private $fieldDenormalizer; |
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 DenormalizationFieldMappingBuilderInterface |
42
|
|
|
*/ |
43
|
2 |
|
public static function create(string $name): DenormalizationFieldMappingBuilderInterface |
44
|
|
|
{ |
45
|
2 |
|
$self = new self($name); |
46
|
2 |
|
$self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name)); |
47
|
|
|
|
48
|
2 |
|
return $self; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $name |
53
|
|
|
* |
54
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
55
|
|
|
*/ |
56
|
1 |
|
public static function createDateTime(string $name): DenormalizationFieldMappingBuilderInterface |
57
|
|
|
{ |
58
|
1 |
|
$self = new self($name); |
59
|
1 |
|
$self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name)); |
60
|
|
|
|
61
|
1 |
|
return $self; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* @param string $class |
67
|
|
|
* |
68
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
69
|
|
|
*/ |
70
|
1 |
View Code Duplication |
public static function createEmbedMany(string $name, string $class): DenormalizationFieldMappingBuilderInterface |
|
|
|
|
71
|
|
|
{ |
72
|
1 |
|
$self = new self($name); |
73
|
1 |
|
$self->fieldDenormalizer = new EmbedManyFieldDenormalizer($class, new PropertyAccessor($name)); |
74
|
|
|
|
75
|
1 |
|
return $self; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @param string $class |
81
|
|
|
* |
82
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
83
|
|
|
*/ |
84
|
1 |
View Code Duplication |
public static function createEmbedOne(string $name, string $class): DenormalizationFieldMappingBuilderInterface |
|
|
|
|
85
|
|
|
{ |
86
|
1 |
|
$self = new self($name); |
87
|
1 |
|
$self->fieldDenormalizer = new EmbedOneFieldDenormalizer($class, new PropertyAccessor($name)); |
88
|
|
|
|
89
|
1 |
|
return $self; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @param callable $repository |
95
|
|
|
* |
96
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
97
|
|
|
*/ |
98
|
1 |
View Code Duplication |
public static function createReferenceMany( |
|
|
|
|
99
|
|
|
string $name, |
100
|
|
|
callable $repository |
101
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
102
|
1 |
|
$self = new self($name); |
103
|
1 |
|
$self->fieldDenormalizer = new ReferenceManyFieldDenormalizer($repository, new PropertyAccessor($name)); |
104
|
|
|
|
105
|
1 |
|
return $self; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $name |
110
|
|
|
* @param callable $repository |
111
|
|
|
* |
112
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
113
|
|
|
*/ |
114
|
1 |
View Code Duplication |
public static function createReferenceOne( |
|
|
|
|
115
|
|
|
string $name, |
116
|
|
|
callable $repository |
117
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
118
|
1 |
|
$self = new self($name); |
119
|
1 |
|
$self->fieldDenormalizer = new ReferenceOneFieldDenormalizer($repository, new PropertyAccessor($name)); |
120
|
|
|
|
121
|
1 |
|
return $self; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $groups |
126
|
|
|
* |
127
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
128
|
|
|
*/ |
129
|
1 |
|
public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface |
130
|
|
|
{ |
131
|
1 |
|
$this->groups = $groups; |
132
|
|
|
|
133
|
1 |
|
return $this; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param FieldDenormalizerInterface $fieldDenormalizer |
138
|
|
|
* |
139
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
140
|
|
|
*/ |
141
|
1 |
|
public function setFieldDenormalizer( |
142
|
|
|
FieldDenormalizerInterface $fieldDenormalizer |
143
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
144
|
1 |
|
$this->fieldDenormalizer = $fieldDenormalizer; |
145
|
|
|
|
146
|
1 |
|
return $this; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return DenormalizationFieldMappingInterface |
151
|
|
|
*/ |
152
|
7 |
|
public function getMapping(): DenormalizationFieldMappingInterface |
153
|
|
|
{ |
154
|
7 |
|
return new DenormalizationFieldMapping($this->name, $this->groups, $this->fieldDenormalizer); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.