PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / utils / array-util.php

array-util.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/utils/array-util.php

216 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Utils;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 class ArrayUtil {
10 public static function insert_assoc_on_pos( array $array, array $daa, $key, bool $after = false ): array {
11 $index = array_search( $key, $array );
12 $index = $after ? $index + 1 : $index;
13 $arrayEnd = array_splice( $array, $index );
14 $arrayStart = array_splice( $array, 0, $index );
15
16 return array_merge( $arrayStart, $daa, $arrayEnd );
17 }
18
19 /**
20 * Flatten multidimensional array.
21 *
22 * @param array $array
23 * @param bool $keys
24 *
25 * @return array
26 */
27 public static function flatten( array $array, bool $keys = false ): array {
28 $return = [];
29
30 if ( $keys ) {
31 array_walk_recursive( $array, function ( $a, $b ) use ( &$return ) {
32 $return[ $b ] = $a;
33 } );
34
35 return $return;
36 }
37
38 array_walk_recursive( $array, function ( $a ) use ( &$return ) {
39 $return[] = $a;
40 } );
41
42 return $return;
43 }
44
45 /**
46 * Sort array by priority key.
47 *
48 * @param array{priority?:int}&array $list
49 *
50 * @return void
51 * @since 1.8.0
52 */
53 public static function priority_sort( array &$list ) {
54 uasort( $list, fn( $a, $b ) => ( $a['priority'] ?? 0 ) <=> ( $b['priority'] ?? 0 ) );
55 }
56
57 /**
58 * Determine whether all elements in the array satisfy the given predicate.
59 *
60 * Iterates over each element and evaluates the predicate. Returns `false`
61 * immediately on the first failure (short-circuit).
62 *
63 * @template T
64 *
65 * @param array<T> $arr Input array.
66 * @param callable(T):mixed $predicate Predicate function. Should return a boolean
67 * (or truthy/falsy value in non-strict mode).
68 * @param bool $strict When true, predicate must return `true` (=== true).
69 * When false, truthy/falsy evaluation is used.
70 *
71 * @return bool True if all elements satisfy the predicate, otherwise false.
72 *
73 * @example
74 * // All numbers are positive
75 * ArrayUtil::every([1, 2, 3], fn($n) => $n > 0); // true
76 *
77 * @example
78 * // One element fails
79 * ArrayUtil::every([1, -1, 3], fn($n) => $n > 0); // false
80 *
81 * @example
82 * // Strict mode (must return === true)
83 * ArrayUtil::every([true, 1], fn($v) => $v, true); // false
84 *
85 * @example
86 * // Empty array vacuously true
87 * ArrayUtil::every([], fn($v) => false); // true
88 *
89 * @since 1.8.0
90 */
91 public static function every( array $arr, callable $predicate, bool $strict = false ): bool {
92 foreach ( $arr as $e ) {
93 $result = $predicate( $e );
94
95 if ( $strict ) {
96 if ( true !== $result ) {
97 return false;
98 }
99 } else {
100 if ( ! $result ) {
101 return false;
102 }
103 }
104 }
105
106 return true;
107 }
108
109 /**
110 * Determine whether at least one element in the array satisfies the predicate.
111 *
112 * Iterates over each element and evaluates the predicate. Returns `true`
113 * immediately on the first match (short-circuit).
114 *
115 * @template T
116 *
117 * @param array<T> $arr Input array.
118 * @param callable(T):mixed $predicate Predicate function. Should return a boolean
119 * (or truthy/falsy value in non-strict mode).
120 * @param bool $strict When true, predicate must return `true` (=== true).
121 * When false, truthy/falsy evaluation is used.
122 *
123 * @return bool True if any element satisfies the predicate, otherwise false.
124 *
125 * @example
126 * // At least one even number
127 * ArrayUtil::any([1, 3, 4], fn($n) => $n % 2 === 0); // true
128 *
129 * @example
130 * // No matches
131 * ArrayUtil::any([1, 3, 5], fn($n) => $n % 2 === 0); // false
132 *
133 * @example
134 * // Strict mode
135 * ArrayUtil::any([1, true], fn($v) => $v, true); // true
136 *
137 * @example
138 * // Empty array always false
139 * ArrayUtil::any([], fn($v) => true); // false
140 *
141 * @since 1.8.0
142 */
143 public static function any( array $arr, callable $predicate, bool $strict = false ): bool {
144 foreach ( $arr as $e ) {
145 $result = $predicate( $e );
146
147 if ( $strict ) {
148 if ( true === $result ) {
149 return true;
150 }
151 } else {
152 if ( $result ) {
153 return true;
154 }
155 }
156 }
157
158 return false;
159 }
160
161 /**
162 * Determine whether no elements in the array satisfy the predicate.
163 *
164 * Equivalent to negating {@see ArrayUtil::any()}.
165 *
166 * @template T
167 *
168 * @param array<T> $arr
169 * @param callable(T):mixed $predicate
170 * @param bool $strict
171 *
172 * @return bool True if no elements satisfy the predicate, otherwise false.
173 *
174 * @since 1.8.0
175 */
176 public static function none( array $arr, callable $predicate, bool $strict = false ): bool {
177 return ! self::any( $arr, $predicate, $strict );
178 }
179
180 /**
181 * Alias of {@see ArrayUtil::every()}.
182 *
183 * @template T
184 *
185 * @param array<T> $arr
186 * @param callable(T):mixed $predicate
187 * @param bool $strict
188 *
189 * @return bool
190 *
191 * @since 1.8.0
192 */
193 public static function all( array $arr, callable $predicate, bool $strict = false ): bool {
194 return self::every( $arr, $predicate, $strict );
195 }
196
197 /**
198 * Alias of {@see ArrayUtil::any()}.
199 *
200 * @template T
201 *
202 * @param array<T> $arr
203 * @param callable(T):mixed $predicate
204 * @param bool $strict
205 *
206 * @return bool
207 *
208 * @since 1.8.0
209 */
210 public static function some( array $arr, callable $predicate, bool $strict = false ): bool {
211 return self::any( $arr, $predicate, $strict );
212 }
213 }
214
215 // End of file array-util.php.
216