1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Denormalizer; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
|
9
|
|
|
final class DenormalizerContextBuilder implements DenormalizerContextBuilderInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array|null |
13
|
|
|
*/ |
14
|
|
|
private $allowedAdditionalFields; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @deprecated |
18
|
|
|
* |
19
|
|
|
* @var string[] |
20
|
|
|
*/ |
21
|
|
|
private $groups = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ServerRequestInterface|null |
25
|
|
|
*/ |
26
|
|
|
private $request; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $resetMissingFields = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $attributes = []; |
37
|
|
|
|
38
|
4 |
|
private function __construct() |
39
|
|
|
{ |
40
|
4 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return DenormalizerContextBuilderInterface |
44
|
|
|
*/ |
45
|
4 |
|
public static function create(): DenormalizerContextBuilderInterface |
46
|
|
|
{ |
47
|
4 |
|
return new self(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array|null $allowedAdditionalFields |
52
|
|
|
* |
53
|
|
|
* @return DenormalizerContextBuilderInterface |
54
|
|
|
*/ |
55
|
1 |
|
public function setAllowedAdditionalFields( |
56
|
|
|
array $allowedAdditionalFields = null |
57
|
|
|
): DenormalizerContextBuilderInterface { |
58
|
1 |
|
$this->allowedAdditionalFields = $allowedAdditionalFields; |
59
|
|
|
|
60
|
1 |
|
return $this; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @deprecated |
65
|
|
|
* |
66
|
|
|
* @param string[] $groups |
67
|
|
|
* |
68
|
|
|
* @return DenormalizerContextBuilderInterface |
69
|
|
|
*/ |
70
|
1 |
|
public function setGroups(array $groups): DenormalizerContextBuilderInterface |
71
|
|
|
{ |
72
|
1 |
|
$this->groups = $groups; |
|
|
|
|
73
|
|
|
|
74
|
1 |
|
return $this; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ServerRequestInterface|null $request |
79
|
|
|
* |
80
|
|
|
* @return DenormalizerContextBuilderInterface |
81
|
|
|
*/ |
82
|
2 |
|
public function setRequest(ServerRequestInterface $request = null): DenormalizerContextBuilderInterface |
83
|
|
|
{ |
84
|
2 |
|
$this->request = $request; |
85
|
|
|
|
86
|
2 |
|
return $this; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @deprecated |
91
|
|
|
* |
92
|
|
|
* @param bool $resetMissingFields |
93
|
|
|
* |
94
|
|
|
* @return DenormalizerContextBuilderInterface |
95
|
|
|
*/ |
96
|
1 |
|
public function setResetMissingFields(bool $resetMissingFields): DenormalizerContextBuilderInterface |
97
|
|
|
{ |
98
|
1 |
|
@trigger_error( |
|
|
|
|
99
|
1 |
|
'setResetMissingFields is broken by design, please do this your self by model or repository', |
100
|
1 |
|
E_USER_DEPRECATED |
101
|
|
|
); |
102
|
|
|
|
103
|
1 |
|
$this->resetMissingFields = $resetMissingFields; |
104
|
|
|
|
105
|
1 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $attributes |
110
|
|
|
* |
111
|
|
|
* @return DenormalizerContextBuilderInterface |
112
|
|
|
*/ |
113
|
1 |
|
public function setAttributes(array $attributes): DenormalizerContextBuilderInterface |
114
|
|
|
{ |
115
|
1 |
|
$this->attributes = $attributes; |
116
|
|
|
|
117
|
1 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return DenormalizerContextInterface |
122
|
|
|
*/ |
123
|
3 |
|
public function getContext(): DenormalizerContextInterface |
124
|
|
|
{ |
125
|
3 |
|
return new DenormalizerContext( |
126
|
3 |
|
$this->allowedAdditionalFields, |
127
|
3 |
|
$this->groups, |
|
|
|
|
128
|
3 |
|
$this->request, |
129
|
3 |
|
$this->resetMissingFields, |
130
|
3 |
|
$this->attributes |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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.