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\FieldDenormalizer; |
9
|
|
|
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; |
10
|
|
|
|
11
|
|
|
final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $name; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $groups = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var FieldDenormalizerInterface |
25
|
|
|
*/ |
26
|
|
|
private $fieldDenormalizer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $forceType; |
32
|
|
|
|
33
|
2 |
|
private function __construct() |
34
|
|
|
{ |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $name |
39
|
|
|
* |
40
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
41
|
|
|
*/ |
42
|
2 |
|
public static function create(string $name): DenormalizationFieldMappingBuilderInterface |
43
|
|
|
{ |
44
|
2 |
|
$self = new self(); |
45
|
2 |
|
$self->name = $name; |
46
|
2 |
|
$self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name)); |
47
|
|
|
|
48
|
2 |
|
return $self; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $groups |
53
|
|
|
* |
54
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
55
|
|
|
*/ |
56
|
1 |
|
public function setGroups(array $groups): DenormalizationFieldMappingBuilderInterface |
57
|
|
|
{ |
58
|
1 |
|
$this->groups = $groups; |
59
|
|
|
|
60
|
1 |
|
return $this; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param FieldDenormalizerInterface $fieldDenormalizer |
65
|
|
|
* |
66
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
67
|
|
|
*/ |
68
|
1 |
|
public function setFieldDenormalizer( |
69
|
|
|
FieldDenormalizerInterface $fieldDenormalizer |
70
|
|
|
): DenormalizationFieldMappingBuilderInterface { |
71
|
1 |
|
$this->fieldDenormalizer = $fieldDenormalizer; |
72
|
|
|
|
73
|
1 |
|
return $this; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string|null $forceType |
78
|
|
|
* |
79
|
|
|
* @return DenormalizationFieldMappingBuilderInterface |
80
|
|
|
*/ |
81
|
|
|
public function setForceType(string $forceType = null): DenormalizationFieldMappingBuilderInterface |
82
|
|
|
{ |
83
|
|
|
$this->forceType = $forceType; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return DenormalizationFieldMappingInterface |
90
|
|
|
*/ |
91
|
2 |
|
public function getMapping(): DenormalizationFieldMappingInterface |
92
|
|
|
{ |
93
|
2 |
|
return new DenormalizationFieldMapping($this->name, $this->groups, $this->fieldDenormalizer, $this->forceType); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.