-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.php
249 lines (205 loc) · 6.39 KB
/
snippets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* PHP helper functions for the JavaScript developer
*
* @author Aidan Hislop | https://github.com/amhislop
* @version 1.0.0
* @license MIT
*/
if( ! function_exists( 'array_some' ) ) {
/**
* The array_some function checks if any elements in an array
* pass a conditional set by a callback function
*
* @param callable $callback The function to execute per iteration
* @param array $arr The array to be iterated over
*
* @return boolean Returns TRUE if condition in callback is met for at least one array item
*/
function array_some( callable $callback, array $arr ): bool
{
$i = 0;
foreach( $arr as $item ) {
if( $callback( $item, $i ) ) return true;
$i++;
}
return false;
}
}
if( ! function_exists( 'array_every' ) ) {
/**
* The array_every function checks if all elements in an array
* pass a conditional set by a callback function
*
* @param callable $callback The function to execute per iteration
* @param array $arr The array to be iterated over
*
* @return boolean Returns TRUE if condition in callback is met for all array items
*/
function array_every( callable $callback, array $arr ): bool
{
$i = 0;
foreach( $arr as $item ) {
if( ! $callback( $item, $i ) ) return false;
$i++;
}
return true;
}
}
if( ! function_exists( 'array_find' ) ) {
/**
* The array_find function iterates through an array and returns the first
* item which meets the conditional set in the callback function
*
* @param callable $callback The function to execute per iteration
* @param array $arr The array to be iterated over
*
* @return mixed[]|null Returns the first item that meets condition set in callback
*/
function array_find( callable $callback, array $arr )
{
$i = 0;
foreach( $arr as $item ) {
if( $callback( $item, $i ) ) return $item;
$i++;
}
return null;
}
}
if( ! function_exists( 'array_sort' ) ) {
/**
* The array_sort function sorts the items of a given array
*
* @param array $arr The array to be sorted
* @param callable $callback A function that compares two consecutive values in the array
*
* @return array The sorted array
*/
function array_sort( array $arr, callable $callback = null ): array
{
if( ! $callback ) {
$type = false;
foreach( $arr as $item ) {
$type = gettype( $item );
if($type === 'string') break;
}
switch( $type ) {
case 'string' :
$callback = function( string $a, string $b) {
return strcmp($a, $b);
};
break;
default :
$callback = function( int $a, int $b ) {
return $a - $b;
};
break;
}
}
$sort;
do {
// Check if associative array
$associative = array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
// Convert associative arrays to entries
if( $associative ) {
$new_array = [];
foreach( $arr as $key => $value ) {
$new_array[] = [ $key, $value ];
}
$arr = $new_array;
}
for ( $i = 1, $sort = false; $i < count( $arr ); $i++ ) {
// Get current values
$a = $arr[$i - 1];
$b = $arr[$i];
$result = ! $associative ? $callback( $a, $b ) : $callback( $a[1], $b[1] );
if( $result !== 0 && ( $result > 0 || $result == false ) ) {
// Switch values
$arr[$i - 1] = $b;
$arr[$i] = $a;
// Sort required
$sort = true;
}
}
// Restore array if orignially associative
if( $associative ) {
$new_array = [];
foreach( $arr as $entry ) {
[$key, $value] = $entry;
$new_array[$key] = $value;
}
$arr = $new_array;
}
} while ( $sort );
return $arr;
}
}
if( ! function_exists( 'array_flat' ) ) {
/**
* The array_flat function creates a new array with all sub-array elements added into it recursively up to the specified depth.
*
* @param array $arr The nested array to be flattened
* @param int|INF $depth The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
*
* @return array The new array with the sub array elements added to it
*/
function array_flat( array $arr, $depth = 1 ): array
{
$depth_remaining = $depth;
$has_sub_arrays = true;
while( $depth_remaining && $has_sub_arrays ) {
$arr = array_reduce( $arr, function( $acc, $curr ) {
if( is_array( $curr ) ) {
foreach( $curr as $sub_item ) $acc[] = $sub_item;
} else {
$acc[] = $curr;
}
return $acc;
});
// Check if $arr contains an array
$has_sub_arrays = false;
foreach( $arr as $item ) {
if( is_array( $item ) ) $has_sub_arrays = true;
}
$depth_remaining--;
}
return $arr;
}
}
if( ! function_exists( 'array_entries' ) ) {
/**
* The array_entries function returns a new array containing arrays
* of the keys and values of the given array
*
* @param array $arr The array whos [key => value] pairs are to be returned
*
* @return array An array of arrays containing the given arays [key => value] pairs
*/
function array_entries( array $arr ): array
{
$new_array = [];
foreach( $arr as $key => $value ) {
$new_array[] = [$key, $value];
}
return $new_array;
}
}
if( ! function_exists( 'array_from_entries' ) ) {
/**
* The array_from_entries function restores a new [key => value] array based on
* the arrays provided by the given array
*
* @param array $arr The array whos arrays are to be restored to a string-keyed [key => value] format
*
* @return array A new array of keyed properties matching the given arrays items
*/
function array_from_entries( array $arr ): array
{
$new_array = [];
foreach( $arr as $entry ) {
[$key, $value] = $entry;
$new_array[$key] = $value;
}
return $new_array;
}
}