1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Serialization\Mapping; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer; |
8
|
|
|
use Chubbyphp\Serialization\Normalizer\LinkNormalizer; |
9
|
|
|
use Chubbyphp\Serialization\Normalizer\LinkNormalizerInterface; |
10
|
|
|
use Chubbyphp\Serialization\Policy\PolicyInterface; |
11
|
|
|
use Chubbyphp\Serialization\Policy\NullPolicy; |
12
|
|
|
use Psr\Link\LinkInterface; |
13
|
|
|
|
14
|
|
|
final class NormalizationLinkMappingBuilder implements NormalizationLinkMappingBuilderInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $groups = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var LinkNormalizerInterface |
28
|
|
|
*/ |
29
|
|
|
private $linkNormalizer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var PolicyInterface|null |
33
|
|
|
*/ |
34
|
|
|
private $policy; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $name |
38
|
|
|
*/ |
39
|
2 |
|
private function __construct(string $name) |
40
|
|
|
{ |
41
|
2 |
|
$this->name = $name; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $name |
46
|
|
|
* @param LinkNormalizerInterface $linkNormalizer |
47
|
|
|
* |
48
|
|
|
* @return NormalizationLinkMappingBuilderInterface |
49
|
|
|
*/ |
50
|
2 |
|
public static function create( |
51
|
|
|
string $name, |
52
|
|
|
LinkNormalizerInterface $linkNormalizer |
53
|
|
|
): NormalizationLinkMappingBuilderInterface { |
54
|
2 |
|
$self = new self($name); |
55
|
2 |
|
$self->linkNormalizer = $linkNormalizer; |
56
|
|
|
|
57
|
2 |
|
return $self; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $name |
62
|
|
|
* @param callable $callback |
63
|
|
|
* |
64
|
|
|
* @return NormalizationLinkMappingBuilderInterface |
65
|
|
|
*/ |
66
|
|
|
public static function createCallback( |
67
|
|
|
string $name, |
68
|
|
|
callable $callback |
69
|
|
|
): NormalizationLinkMappingBuilderInterface { |
70
|
|
|
$self = new self($name); |
71
|
|
|
$self->linkNormalizer = new CallbackLinkNormalizer($callback); |
72
|
|
|
|
73
|
|
|
return $self; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $name |
78
|
|
|
* @param LinkInterface $link |
79
|
|
|
* |
80
|
|
|
* @return NormalizationLinkMappingBuilderInterface |
81
|
|
|
*/ |
82
|
|
|
public static function createLink( |
83
|
|
|
string $name, |
84
|
|
|
LinkInterface $link |
85
|
|
|
): NormalizationLinkMappingBuilderInterface { |
86
|
|
|
$self = new self($name); |
87
|
|
|
$self->linkNormalizer = new LinkNormalizer($link); |
88
|
|
|
|
89
|
|
|
return $self; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @deprecated |
94
|
|
|
* |
95
|
|
|
* @param array $groups |
96
|
|
|
* |
97
|
|
|
* @return NormalizationLinkMappingBuilderInterface |
98
|
|
|
*/ |
99
|
1 |
|
public function setGroups(array $groups): NormalizationLinkMappingBuilderInterface |
100
|
|
|
{ |
101
|
1 |
|
$this->groups = $groups; |
102
|
|
|
|
103
|
1 |
|
return $this; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param PolicyInterface $policy |
108
|
|
|
* |
109
|
|
|
* @return NormalizationLinkMappingBuilderInterface |
110
|
|
|
*/ |
111
|
|
|
public function setPolicy(PolicyInterface $policy): NormalizationLinkMappingBuilderInterface |
112
|
|
|
{ |
113
|
|
|
$this->policy = $policy; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return NormalizationLinkMappingInterface |
120
|
|
|
*/ |
121
|
2 |
|
public function getMapping(): NormalizationLinkMappingInterface |
122
|
|
|
{ |
123
|
2 |
|
return new NormalizationLinkMapping( |
124
|
2 |
|
$this->name, |
125
|
2 |
|
$this->groups, |
126
|
2 |
|
$this->linkNormalizer, |
127
|
2 |
|
$this->policy ?? new NullPolicy() |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.