1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Dto; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The wrapper for DTO property types. |
7
|
|
|
* |
8
|
|
|
* @property DtoPropertyType[] $all |
9
|
|
|
* @property bool $includeNull |
10
|
|
|
* @property bool $includeArray |
11
|
|
|
* @property bool $includeBool |
12
|
|
|
* @property bool $expectCollection |
13
|
|
|
* @property string $expectedDto |
14
|
|
|
* @property array $declaredNames |
15
|
|
|
*/ |
16
|
|
|
class DtoPropertyTypes |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The DTO property types. |
20
|
|
|
* |
21
|
|
|
* @var DtoPropertyType[] |
22
|
|
|
*/ |
23
|
|
|
protected $all = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Whether one of the types is 'null'. |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $includeNull = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Whether one of the types is 'array'. |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $includeArray = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Whether one of the types is 'bool'. |
41
|
|
|
* |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $includeBool = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Whether the types expect a collection. |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $expectCollection = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The expected DTO. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $expectedDto; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The types name with the [] suffix if collections. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $declaredNames = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add the given DTO property type |
69
|
|
|
* |
70
|
|
|
* @param DtoPropertyType $type |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
234 |
|
public function addType(DtoPropertyType $type): self |
74
|
|
|
{ |
75
|
234 |
|
$this->all[] = $type; |
76
|
234 |
|
$this->includeNull = $this->includeNull || $type->name() == 'null'; |
77
|
234 |
|
$this->includeArray = $this->includeArray || $type->name() == 'array'; |
78
|
234 |
|
$this->includeBool = $this->includeBool || $type->name() == 'bool'; |
79
|
234 |
|
$this->expectCollection = $this->expectCollection || $type->isCollection(); |
80
|
234 |
|
$this->expectedDto = $this->getExpectedDto($type); |
81
|
234 |
|
$this->declaredNames[] = $type->declaredName(); |
82
|
|
|
|
83
|
234 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Retrieve the expected DTO if any |
88
|
|
|
* |
89
|
|
|
* @param DtoPropertyType $type |
90
|
|
|
* @return string|null |
91
|
|
|
*/ |
92
|
234 |
|
protected function getExpectedDto(DtoPropertyType $type): ?string |
93
|
|
|
{ |
94
|
234 |
|
if ($this->expectedDto) { |
95
|
3 |
|
return $this->expectedDto; |
96
|
|
|
} |
97
|
|
|
|
98
|
234 |
|
return $type->isDto() ? $type->name() : null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determine whether the given value matches at least one of the property types |
103
|
|
|
* |
104
|
|
|
* @param mixed $value |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
198 |
|
public function match($value): bool |
108
|
|
|
{ |
109
|
198 |
|
foreach ($this->all as $type) { |
110
|
198 |
|
if ($type->matches($value)) { |
111
|
194 |
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
12 |
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Determine whether the types have a default value depending on the given flags |
120
|
|
|
* |
121
|
|
|
* @param int $flags |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
168 |
|
public function haveDefaultValue(int $flags) |
125
|
|
|
{ |
126
|
168 |
|
return $this->arraysHaveDefault($flags) || |
127
|
168 |
|
($this->includeBool && ($flags & BOOL_DEFAULT_TO_FALSE)) || |
128
|
168 |
|
($this->includeNull && ($flags & NULLABLE_DEFAULT_TO_NULL)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Determine whether array types have a default value |
133
|
|
|
* |
134
|
|
|
* @param int $flags |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
168 |
|
protected function arraysHaveDefault(int $flags): bool |
138
|
|
|
{ |
139
|
168 |
|
$includeArray = $this->includeArray || $this->expectCollection; |
140
|
|
|
|
141
|
168 |
|
return $includeArray && ($flags & ARRAY_DEFAULT_TO_EMPTY_ARRAY); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Retrieve the types default value depending on the given flags |
146
|
|
|
* |
147
|
|
|
* @param int $flags |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
12 |
|
public function getDefaultValue(int $flags) |
151
|
|
|
{ |
152
|
|
|
switch (true) { |
153
|
12 |
|
case ($this->includeArray || $this->expectCollection) && ($flags & ARRAY_DEFAULT_TO_EMPTY_ARRAY): |
154
|
9 |
|
return []; |
155
|
9 |
|
case $this->includeBool && ($flags & BOOL_DEFAULT_TO_FALSE): |
156
|
6 |
|
return false; |
157
|
|
|
} |
158
|
9 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Retrieve the given property |
162
|
|
|
* |
163
|
|
|
* @param string $name |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
219 |
|
public function __get(string $name) |
167
|
|
|
{ |
168
|
219 |
|
return $this->{$name}; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|