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 | * @return mixed |
||
105 | */ |
||
106 | protected function setPropertyValue($name, $value) |
||
107 | { |
||
108 | $value = $this->transformInboundValue($name, $value); |
||
109 | return $this->{$name} = $value; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param string $key |
||
114 | 4 | * @param mixed $value |
|
115 | * @return mixed |
||
116 | 4 | * @noinspection PhpUnusedParameterInspection |
|
117 | */ |
||
118 | protected function transformInboundValue($key, $value) |
||
0 ignored issues
–
show
|
|||
119 | { |
||
120 | return $value; |
||
121 | } |
||
122 | |||
123 | 4 | /** |
|
124 | * @param $key |
||
125 | 4 | * @return bool |
|
126 | 4 | */ |
|
127 | 3 | public function __isset($key): bool |
|
128 | { |
||
129 | return $this->has($key); |
||
130 | } |
||
131 | 4 | ||
132 | /** |
||
133 | * @param string|array $key |
||
134 | * @return bool |
||
135 | */ |
||
136 | public function has($key): bool |
||
137 | { |
||
138 | 1 | foreach ((array)$key as $prop) { |
|
139 | if ($this->get($prop) === null) { |
||
140 | 1 | return false; |
|
141 | } |
||
142 | } |
||
143 | |||
144 | return true; |
||
145 | } |
||
146 | |||
147 | 1 | /** |
|
148 | * @param $key |
||
149 | 1 | * @return $this |
|
150 | 1 | */ |
|
151 | 1 | public function __unset($key) |
|
152 | { |
||
153 | return $this->unset($key); |
||
154 | 1 | } |
|
155 | |||
156 | /** |
||
157 | * @param $field |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function unset($field) |
||
161 | { |
||
162 | $field = (array)$field; |
||
163 | foreach ($field as $prop) { |
||
164 | $this->unsetProperty($prop); |
||
165 | } |
||
166 | |||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param $prop |
||
172 | */ |
||
173 | protected function unsetProperty($prop) |
||
174 | { |
||
175 | unset($this->{$prop}); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Checks that a field is empty |
||
180 | * |
||
181 | * This is not working like the PHP `empty()` function. The method will |
||
182 | * return true for: |
||
183 | * |
||
184 | * - `''` (empty string) |
||
185 | * - `null` |
||
186 | * - `[]` |
||
187 | * |
||
188 | * and false in all other cases. |
||
189 | * |
||
190 | * @param string $field The field to check. |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function isEmpty(string $field): bool |
||
194 | { |
||
195 | $value = $this->get($field); |
||
196 | |||
197 | if ($value === null) { |
||
198 | return true; |
||
199 | } |
||
200 | |||
201 | if (is_array($value) && empty($value)) { |
||
202 | return true; |
||
203 | } |
||
204 | |||
205 | if (is_string($value) && empty($value)) { |
||
206 | return true; |
||
207 | } |
||
208 | |||
209 | return false; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Checks tha a field has a value. |
||
214 | * |
||
215 | * This method will return true for |
||
216 | * |
||
217 | * - Non-empty strings |
||
218 | * - Non-empty arrays |
||
219 | * - Any object |
||
220 | * - Integer, even `0` |
||
221 | * - Float, even 0.0 |
||
222 | * |
||
223 | * and false in all other cases. |
||
224 | * |
||
225 | * @param string $field The field to check. |
||
226 | * @return bool |
||
227 | */ |
||
228 | public function hasValue(string $field): bool |
||
229 | { |
||
230 | return !$this->isEmpty($field); |
||
231 | } |
||
232 | } |
||
233 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.