1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\DataObjects\Behaviors\PropertyOverloading; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait PropertyOverloadingTrait |
7
|
|
|
* @package ByTIC\DataObjects\Behaviors\PropertyOverloading |
8
|
|
|
*/ |
9
|
|
|
trait PropertyOverloadingTrait |
10
|
|
|
{ |
11
|
1 |
|
use \ByTIC\DataObjects\Legacy\Behaviors\OldAccessingPatternsTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param array|null $data |
15
|
|
|
* @return $this |
16
|
|
|
* @noinspection PhpMissingReturnTypeInspection |
17
|
|
|
*/ |
18
|
18 |
|
public function fill(array $data = null) |
19
|
|
|
{ |
20
|
18 |
|
if (!is_array($data)) { |
21
|
16 |
|
return $this; |
22
|
|
|
} |
23
|
|
|
|
24
|
5 |
|
foreach ($data as $key => $value) { |
25
|
5 |
|
$this->set($key, $value); |
26
|
|
|
} |
27
|
|
|
|
28
|
5 |
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $name |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
12 |
|
public function __get($name) |
36
|
|
|
{ |
37
|
12 |
|
return $this->get($name); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $name |
42
|
|
|
* @param null $default |
|
|
|
|
43
|
|
|
* @return mixed|void |
44
|
|
|
*/ |
45
|
|
|
public function get($name, $default = null) |
46
|
|
|
{ |
47
|
|
|
return $this->getPropertyValue($name, $default); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $name |
52
|
|
|
* @param null $default |
|
|
|
|
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
6 |
|
protected function getPropertyValue($name, $default = null) |
56
|
|
|
{ |
57
|
6 |
|
return $this->transformValue($name, $this->getPropertyRaw($name, $default)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $key |
62
|
|
|
* @param mixed $value |
63
|
|
|
* @return mixed |
64
|
|
|
* @noinspection PhpUnusedParameterInspection |
65
|
|
|
*/ |
66
|
|
|
protected function transformValue($key, $value) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $name |
73
|
|
|
* @param null $default |
|
|
|
|
74
|
|
|
* @return mixed|null |
75
|
|
|
*/ |
76
|
|
|
protected function getPropertyRaw(string $name, $default = null) |
77
|
|
|
{ |
78
|
|
|
return isset($this->{$name}) ? $this->{$name} : $default; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $name |
83
|
|
|
* @param $value |
84
|
|
|
* @return mixed |
85
|
13 |
|
*/ |
86
|
|
|
public function __set($name, $value) |
87
|
13 |
|
{ |
88
|
|
|
return $this->set($name, $value); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $name |
93
|
|
|
* @param $value |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function set($name, $value) |
97
|
|
|
{ |
98
|
|
|
return $this->setPropertyValue($name, $value); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $name |
103
|
|
|
* @param $value |
104
|
|
|
* @param $condition |
105
|
|
|
* @return mixed|void |
106
|
|
|
*/ |
107
|
|
|
public function setIf($name, $value, $condition) |
108
|
|
|
{ |
109
|
|
|
$condition = is_callable($condition) ? $condition() : $condition; |
110
|
|
|
if ($condition !== true) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
return $this->setPropertyValue($name, $value); |
114
|
4 |
|
} |
115
|
|
|
|
116
|
4 |
|
/** |
117
|
|
|
* @param $name |
118
|
|
|
* @param $value |
119
|
|
|
* @return mixed|void |
120
|
|
|
*/ |
121
|
|
|
public function setIfNull($name, $value) |
122
|
|
|
{ |
123
|
4 |
|
return $this->setIf( |
124
|
|
|
$name, |
125
|
4 |
|
$value, |
126
|
4 |
|
function () use ($name) { |
127
|
3 |
|
return !$this->has($name) || $this->get($name) == null; |
128
|
|
|
} |
129
|
|
|
); |
130
|
|
|
} |
131
|
4 |
|
|
132
|
|
|
/** |
133
|
|
|
* @param $name |
134
|
|
|
* @param $value |
135
|
|
|
* @return mixed|void |
136
|
|
|
*/ |
137
|
|
|
public function setIfEmpty($name, $value) |
138
|
1 |
|
{ |
139
|
|
|
return $this->setIf( |
140
|
1 |
|
$name, |
141
|
|
|
$value, |
142
|
|
|
function () use ($name) { |
143
|
|
|
return !$this->has($name) || empty($this->get($name)); |
144
|
|
|
} |
145
|
|
|
); |
146
|
|
|
} |
147
|
1 |
|
|
148
|
|
|
/** |
149
|
1 |
|
* @param $name |
150
|
1 |
|
* @param $value |
151
|
1 |
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
protected function setPropertyValue($name, $value) |
154
|
1 |
|
{ |
155
|
|
|
$value = $this->transformInboundValue($name, $value); |
156
|
|
|
return $this->{$name} = $value; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $key |
161
|
|
|
* @param mixed $value |
162
|
|
|
* @return mixed |
163
|
|
|
* @noinspection PhpUnusedParameterInspection |
164
|
|
|
*/ |
165
|
|
|
protected function transformInboundValue($key, $value) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
return $value; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param $key |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function __isset($key): bool |
175
|
|
|
{ |
176
|
|
|
return $this->has($key); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string|array $key |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public function has($key): bool |
184
|
|
|
{ |
185
|
|
|
foreach ((array)$key as $prop) { |
186
|
|
|
if ($this->get($prop) === null) { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param $key |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function __unset($key) |
199
|
|
|
{ |
200
|
|
|
return $this->unset($key); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $field |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
|
|
public function unset($field) |
208
|
|
|
{ |
209
|
|
|
$field = (array)$field; |
210
|
|
|
foreach ($field as $prop) { |
211
|
|
|
$this->unsetProperty($prop); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param $prop |
219
|
|
|
*/ |
220
|
|
|
protected function unsetProperty($prop) |
221
|
|
|
{ |
222
|
|
|
unset($this->{$prop}); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Checks that a field is empty |
227
|
|
|
* |
228
|
|
|
* This is not working like the PHP `empty()` function. The method will |
229
|
|
|
* return true for: |
230
|
|
|
* |
231
|
|
|
* - `''` (empty string) |
232
|
|
|
* - `null` |
233
|
|
|
* - `[]` |
234
|
|
|
* |
235
|
|
|
* and false in all other cases. |
236
|
|
|
* |
237
|
|
|
* @param string $field The field to check. |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
|
|
public function isEmpty(string $field): bool |
241
|
|
|
{ |
242
|
|
|
$value = $this->get($field); |
243
|
|
|
|
244
|
|
|
if ($value === null) { |
245
|
|
|
return true; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if (is_array($value) && empty($value)) { |
249
|
|
|
return true; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
if (is_string($value) && empty($value)) { |
253
|
|
|
return true; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Checks tha a field has a value. |
261
|
|
|
* |
262
|
|
|
* This method will return true for |
263
|
|
|
* |
264
|
|
|
* - Non-empty strings |
265
|
|
|
* - Non-empty arrays |
266
|
|
|
* - Any object |
267
|
|
|
* - Integer, even `0` |
268
|
|
|
* - Float, even 0.0 |
269
|
|
|
* |
270
|
|
|
* and false in all other cases. |
271
|
|
|
* |
272
|
|
|
* @param string $field The field to check. |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
public function hasValue(string $field): bool |
276
|
|
|
{ |
277
|
|
|
return !$this->isEmpty($field); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|