|
4 | 4 |
|
5 | 5 | {% code title="" %}
|
6 | 6 | ```php
|
7 |
| -Arr::check(array $array, mixed|callable $condition, bool $strict = false): bool |
| 7 | +Arr::check(array $array, mixed|callable $condition, int $flag = 0): bool |
8 | 8 | ```
|
9 | 9 | {% endcode %}
|
10 | 10 |
|
11 | 11 | #### Description
|
12 | 12 |
|
13 |
| -Check if every element of an array meets specified condition. |
| 13 | +Check if some or every array element meets specified condition. |
14 | 14 |
|
15 |
| -#### Examples |
| 15 | +If `CHECK_SOME` flag is NOT present then every array element must meet specified condition in order to pass check. |
| 16 | + |
| 17 | +#### Condition |
| 18 | + |
| 19 | +<table> |
| 20 | + <thead> |
| 21 | + <tr> |
| 22 | + <th style="text-align:left"><code>$condition</code> type</th> |
| 23 | + <th style="text-align:left">Description</th> |
| 24 | + </tr> |
| 25 | + </thead> |
| 26 | + <tbody> |
| 27 | + <tr> |
| 28 | + <td style="text-align:left"><code>callable</code> |
| 29 | + </td> |
| 30 | + <td style="text-align:left"> |
| 31 | + <p><em>Callable </em>should return truthy or falsy value (while using <code>CHECK_STRICT</code> flag, |
| 32 | + return values other than <code>true</code> are treated as <code>false</code>).</p> |
| 33 | + <p></p> |
| 34 | + <p><em>Callable</em> is supplied with either only element value (<code>function($value)</code>) |
| 35 | + or pair of element value and key (<code>function($value, $key)</code>) |
| 36 | + as arguments. |
| 37 | + <br />Arguments amount depend on <em>callable </em>definition and is dynamically |
| 38 | + resolved using <a href="https://www.php.net/manual/en/reflectionfunctionabstract.getnumberofparameters.php">reflection</a> (defaults |
| 39 | + to 2 - value and key)</p> |
| 40 | + </td> |
| 41 | + </tr> |
| 42 | + <tr> |
| 43 | + <td style="text-align:left"><code>mixed</code> |
| 44 | + </td> |
| 45 | + <td style="text-align:left"> |
| 46 | + <p>If <em>condition</em> type is different than <code>callable</code> then every |
| 47 | + array element is compared against it value.</p> |
| 48 | + <p></p> |
| 49 | + <p><code>$value == $condition</code> by default</p> |
| 50 | + <p><code>$value === $condition</code>if <code>CHECK_STRICT</code> flag is enabled</p> |
| 51 | + </td> |
| 52 | + </tr> |
| 53 | + </tbody> |
| 54 | +</table>#### Flags |
| 55 | + |
| 56 | +Can be used as stand alone \(i.e.`Arr::CHECK_STRICT`\) as well as in conjunction \(i.e. `Arr::CHECK_STRICT | Arr::CHECK_SOME`\) |
| 57 | + |
| 58 | +{% hint style="info" %} |
| 59 | +`$flag` argument used to be a boolean parameter called `$strict` |
| 60 | + |
| 61 | +But do not worry, it it is fully backward compatible due to in-flight type conversion from `bool` to `int` |
| 62 | +{% endhint %} |
| 63 | + |
| 64 | +<table> |
| 65 | + <thead> |
| 66 | + <tr> |
| 67 | + <th style="text-align:left">Constant name</th> |
| 68 | + <th style="text-align:left">Description</th> |
| 69 | + </tr> |
| 70 | + </thead> |
| 71 | + <tbody> |
| 72 | + <tr> |
| 73 | + <td style="text-align:left"><code>CHECK_STRICT</code> |
| 74 | + </td> |
| 75 | + <td style="text-align:left"> |
| 76 | + <p>In case <em>condition</em> is <code>callable</code> check if it result is |
| 77 | + exactly <code>true</code> |
| 78 | + </p> |
| 79 | + <p> |
| 80 | + <br />If <em>condition</em> is not <code>callable</code>, then check if array element |
| 81 | + is equal to it both by value and type</p> |
| 82 | + <p></p> |
| 83 | + <p>See <b>Condition</b> section for more info</p> |
| 84 | + </td> |
| 85 | + </tr> |
| 86 | + <tr> |
| 87 | + <td style="text-align:left"><code>CHECK_SOME</code> |
| 88 | + </td> |
| 89 | + <td style="text-align:left"> |
| 90 | + <p>Check will return <code>true</code> on first array element that match specified <em>condition</em> or |
| 91 | + false if none of them matches it.</p> |
| 92 | + <p></p> |
| 93 | + <p>By default <code>check</code> method will return <code>true</code> only if |
| 94 | + ALL of array elements meet specified <em>condition</em> |
| 95 | + </p> |
| 96 | + </td> |
| 97 | + </tr> |
| 98 | + </tbody> |
| 99 | +</table>#### Examples |
16 | 100 |
|
17 | 101 | ```php
|
18 |
| -$array = [1, 1, 1]; |
| 102 | +$array = [1, '1', true]; |
19 | 103 |
|
| 104 | +// Every array element is EQUAL to 1 ($value == '1') |
20 | 105 | Arr::check($array, '1') -> true
|
21 | 106 |
|
22 |
| -Arr::check($array, '1', true) -> false |
| 107 | +// Only one array element is the SAME as 1 ($value === '1') which is not sufficient |
| 108 | +Arr::check($array, '1', Arr::CHECK_STRICT) -> false |
23 | 109 |
|
24 |
| -Arr::check($array, 'is_int') -> true |
| 110 | +// When CHECK_SOME flag is present, one element is sufficient to pass check |
| 111 | +Arr::check($array, '1', Arr::CHECK_STRICT | Arr::CHECK_SOME) -> true |
25 | 112 |
|
| 113 | +// You can also use built-in functions to validate whole array |
| 114 | +Arr::check($array, 'is_int') -> false |
26 | 115 | Arr::check($array, 'is_string') -> false
|
27 | 116 |
|
28 |
| -Arr::check($array, function ($value) { return $value; }) -> false |
| 117 | +// When CHECK_SOME flag is present only one element need to meet specified condition |
| 118 | +Arr::check($array, 'is_int', Arr::CHECK_SOME) -> true |
| 119 | +Arr::check($array, 'is_string', Arr::CHECK_SOME) -> true |
| 120 | + |
| 121 | +// Every value of array is truthy |
| 122 | +Arr::check($array, function ($value) { return $value; }) -> true |
| 123 | +// Above check can be simplified to |
| 124 | +Arr::check($array, true) -> true |
| 125 | +// Or even shorter |
| 126 | +Arr::check($array, 1) -> true |
| 127 | + |
| 128 | +// When CHECK_STRICT flag is present, check will fail for callback return values other true |
| 129 | +Arr::check($array, function ($value) { return $value; }, Arr::CHECK_STRICT) -> false |
| 130 | +// But when callback is written as follows check will pass |
| 131 | +Arr::check($array, function ($value) { return boolval($value); }, Arr::CHECK_STRICT) -> true |
| 132 | +// Above check will pass if we add CHECK_SOME flag cause of of the elements is exactly true |
| 133 | +Arr::check($array, function ($value) { return $value; }, Arr::CHECK_STRICT | Arr::CHECK_SOME) -> true |
29 | 134 |
|
30 |
| -// In case of callback supplied as condition, strict flag checks if return value is exactly true |
31 |
| -Arr::check($array, function ($value) { return $value; }, true) -> false |
32 | 135 |
|
33 |
| -Arr::check($array, function ($value) { return $value === 1; }, true) -> true |
| 136 | +// Callback function arguments count is automatically detected |
| 137 | +Arr::check($array, function ($value, $key) { return $value > $key; }) -> false |
| 138 | +// First array element 1 is greater than its key 0 |
| 139 | +Arr::check($array, function ($value, $key) { return $value > $key; }, Arr::CHECK_SOME) -> true |
34 | 140 | ```
|
35 | 141 |
|
0 commit comments