1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Doctrine\Mapping; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizer; |
8
|
|
|
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; |
9
|
|
|
use Chubbyphp\Deserialization\Doctrine\Accessor\PropertyAccessor; |
10
|
|
|
use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMapping; |
11
|
|
|
use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingBuilderInterface; |
12
|
|
|
use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingInterface; |
13
|
|
|
|
14
|
|
View Code Duplication |
final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $groups; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FieldDenormalizerInterface |
28
|
|
|
*/ |
29
|
|
|
private $fieldDenormalizer; |
30
|
|
|
|
31
|
|
|
private function __construct() |
32
|
|
|
{ |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $name |
37
|
|
|
* |
38
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
39
|
|
|
*/ |
40
|
|
|
public static function create(string $name): DenormalizationFieldMappingBuilderInterface |
41
|
|
|
{ |
42
|
|
|
$self = new self(); |
43
|
|
|
$self->name = $name; |
44
|
|
|
$self->groups = []; |
45
|
|
|
$self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name)); |
46
|
|
|
|
47
|
|
|
return $self; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $groups |
52
|
|
|
* |
53
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
54
|
|
|
*/ |
55
|
|
|
public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface |
56
|
|
|
{ |
57
|
|
|
$this->groups = $groups; |
58
|
|
|
|
59
|
|
|
return $this; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param FieldDenormalizerInterface $fieldDenormalizer |
64
|
|
|
* |
65
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
66
|
|
|
*/ |
67
|
|
|
public function setFieldDenormalizer( |
68
|
|
|
FieldDenormalizerInterface $fieldDenormalizer |
69
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
70
|
|
|
$this->fieldDenormalizer = $fieldDenormalizer; |
71
|
|
|
|
72
|
|
|
return $this; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return DenormalizationFieldMappingInterface |
77
|
|
|
*/ |
78
|
|
|
public function getMapping(): DenormalizationFieldMappingInterface |
79
|
|
|
{ |
80
|
|
|
return new DenormalizationFieldMapping($this->name, $this->groups, $this->fieldDenormalizer); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.