1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2016-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package MissingBits/TypeChecks |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://ganbarodigital.github.io/php-the-missing-bits |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\MissingBits\TypeChecks; |
45
|
|
|
|
46
|
|
|
use GanbaroDigital\MissingBits\Checks\Check; |
47
|
|
|
use GanbaroDigital\MissingBits\Checks\ListCheck; |
48
|
|
|
use GanbaroDigital\MissingBits\Checks\ListCheckHelper; |
49
|
|
|
use GanbaroDigital\MissingBits\TypeInterfaces\CanBeEmpty; |
50
|
|
|
use stdClass; |
51
|
|
|
use Traversable; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* do we have something that is empty? |
55
|
|
|
*/ |
56
|
|
|
class IsEmpty implements Check, ListCheck |
57
|
|
|
{ |
58
|
|
|
// saves us having to implement inspectList() ourselves |
59
|
|
|
use ListCheckHelper; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* check if an item is empty |
63
|
|
|
* |
64
|
|
|
* empty means one of: |
65
|
|
|
* - item itself is empty |
66
|
|
|
* - item is a data container, and only contains empty data items |
67
|
|
|
* |
68
|
|
|
* BE AWARE that this check WILL descend down into the contents of $fieldOrVar |
69
|
|
|
* until it finds the first piece of non-empty data. This has the potential |
70
|
|
|
* to be computationally expensive. |
71
|
|
|
* |
72
|
|
|
* @param mixed $fieldOrVar |
73
|
|
|
* the item to check |
74
|
|
|
* @return bool |
75
|
|
|
* TRUE if the item is empty |
76
|
|
|
* FALSE otherwise |
77
|
|
|
*/ |
78
|
|
|
public static function check($fieldOrVar) |
79
|
|
|
{ |
80
|
|
|
// general case |
81
|
|
|
if (is_null($fieldOrVar)) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// type-specific checks |
86
|
|
|
if (is_array($fieldOrVar)) { |
87
|
|
|
return self::checkArray($fieldOrVar); |
88
|
|
|
} |
89
|
|
|
if ($fieldOrVar instanceof Traversable || $fieldOrVar instanceof stdClass) { |
90
|
|
|
return self::checkTraversable($fieldOrVar); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
if ($fieldOrVar instanceof CanBeEmpty) { |
93
|
|
|
return $fieldOrVar->isEmpty(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (is_string($fieldOrVar)) { |
97
|
|
|
return self::checkString($fieldOrVar); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// if we get here, we've run out of ways to check |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* check if an item is empty |
106
|
|
|
* |
107
|
|
|
* empty means one of: |
108
|
|
|
* - item itself has no content |
109
|
|
|
* - item is a data container, and only contains empty data items |
110
|
|
|
* |
111
|
|
|
* BE AWARE that this check WILL descend down into the contents of $fieldOrVar |
112
|
|
|
* until it finds the first piece of non-empty data. This has the potential |
113
|
|
|
* to be computationally expensive. |
114
|
|
|
* |
115
|
|
|
* @param array $fieldOrVar |
116
|
|
|
* the item to check |
117
|
|
|
* @return bool |
118
|
|
|
* TRUE if the item is empty |
119
|
|
|
* FALSE otherwise |
120
|
|
|
*/ |
121
|
|
|
private static function checkArray($fieldOrVar) |
122
|
|
|
{ |
123
|
|
|
if (count($fieldOrVar) === 0) { |
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return self::checkTraversable($fieldOrVar); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* check if an item is empty |
132
|
|
|
* |
133
|
|
|
* empty means one of: |
134
|
|
|
* - the string has zero length |
135
|
|
|
* - the string only contains whitespace |
136
|
|
|
* |
137
|
|
|
* @param string $fieldOrVar |
138
|
|
|
* the item to check |
139
|
|
|
* @return bool |
140
|
|
|
* TRUE if the item is empty |
141
|
|
|
* FALSE otherwise |
142
|
|
|
*/ |
143
|
|
|
private static function checkString($fieldOrVar) |
144
|
|
|
{ |
145
|
|
|
if (trim((string)$fieldOrVar) === '') { |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* check if an item is empty |
154
|
|
|
* |
155
|
|
|
* empty means one of: |
156
|
|
|
* - item itself has no content |
157
|
|
|
* - item is a data container, and only contains empty data items |
158
|
|
|
* |
159
|
|
|
* BE AWARE that this check WILL descend down into the contents of $fieldOrVar |
160
|
|
|
* until it finds the first piece of non-empty data. This has the potential |
161
|
|
|
* to be computationally expensive. |
162
|
|
|
* |
163
|
|
|
* @param array $fieldOrVar |
164
|
|
|
* the item to check |
165
|
|
|
* @return bool |
166
|
|
|
* TRUE if the item is empty |
167
|
|
|
* FALSE otherwise |
168
|
|
|
*/ |
169
|
|
|
private static function checkTraversable($fieldOrVar) |
170
|
|
|
{ |
171
|
|
|
foreach ($fieldOrVar as $value) { |
172
|
|
|
if (!self::check($value)) { |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// if we get here, item's contents are entirely empty |
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* check if an item is empty |
183
|
|
|
* |
184
|
|
|
* empty means one of: |
185
|
|
|
* - item itself is empty |
186
|
|
|
* - item is a data container, and only contains empty data items |
187
|
|
|
* |
188
|
|
|
* BE AWARE that this check WILL descend down into the contents of $fieldOrVar |
189
|
|
|
* until it finds the first piece of non-empty data. This has the potential |
190
|
|
|
* to be computationally expensive. |
191
|
|
|
* |
192
|
|
|
* @param mixed $fieldOrVar |
193
|
|
|
* the item to check |
194
|
|
|
* @return bool |
195
|
|
|
* TRUE if the item is empty |
196
|
|
|
* FALSE otherwise |
197
|
|
|
*/ |
198
|
|
|
public function inspect($fieldOrVar) |
199
|
|
|
{ |
200
|
|
|
return static::check($fieldOrVar); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* is every entry in $list empty? |
205
|
|
|
* |
206
|
|
|
* @param mixed $list |
207
|
|
|
* the list of items to be checked |
208
|
|
|
* @return bool |
209
|
|
|
* TRUE if every item in $list is empty |
210
|
|
|
* FALSE otherwise |
211
|
|
|
*/ |
212
|
|
|
public static function checkList($list) |
213
|
|
|
{ |
214
|
|
|
$check = new static; |
215
|
|
|
return $check->inspectList($list); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: