-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCollection.php
364 lines (344 loc) · 11.7 KB
/
Collection.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php /** @noinspection DuplicatedCode */
namespace mapache_commons;
/**
* Class Collection
*
* @package mapache_commons
* @version 1.12 2019-dec-26
* @copyright Jorge Castro Castillo
* @license Apache-2.0
* @see https://github.com/EFTEC/mapache-commons
*/
class Collection {
/**
* Returns true if array is an associative array, false is it's an indexed array
*
* @param array $array input array
*
* @return bool
* @see https://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
*/
public static function isAssoc($array) {
return (array_values($array) !== $array);
}
/**
* Returns the first element of an array.
*
* @param array $array input array
*
* @return mixed
* @see https://stackoverflow.com/questions/1921421/get-the-first-element-of-an-array
*/
public static function first($array) {
return reset($array);
}
/**
* Returns the first key of an array.
*
* @param array $array input array
*
* @return mixed
* @see https://stackoverflow.com/questions/1921421/get-the-first-element-of-an-array
*/
public static function firstKey($array) {
reset($array);
return key($array);
}
/**
* Change the case of the key to lowercase
*
* @param array $array input array
*
* @return array
* @see https://stackoverflow.com/questions/1444484/how-to-convert-all-keys-in-a-multi-dimenional-array-to-snake-case
*/
public static function arrayKeyLower($array) {
return array_map(function ($item) {
if (is_array($item)) {
$item = self::arrayKeyLower($item);
}
return $item;
}, array_change_key_case($array, CASE_LOWER));
}
/**
* Change the case of the key to lowercase
*
* @param array $array input array
*
* @return array
* @see https://stackoverflow.com/questions/1444484/how-to-convert-all-keys-in-a-multi-dimenional-array-to-snake-case
*/
public static function arrayKeyUpper($array) {
return array_map(function ($item) {
if (is_array($item)) {
$item = self::arrayKeyUpper($item);
}
return $item;
}, array_change_key_case($array, CASE_UPPER));
}
/**
* Generate a table from an array
*
* @param array|null $array input array
* @param string|bool $css if true then it uses the build in style. If false then it doesn't use style. If string then it uses as class
*
* @return string
* @see https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array
*/
public static function generateTable($array, $css = true,$percentage=false) {
if($percentage) {
$min=min($array);
}
if (!isset($array[0])) {
$tmp = $array;
$array = array();
$array[0] = $tmp;
} // create an array with a single element
if ($array[0] === null) {
return "NULL<br>";
}
if ($css === true) {
$html = '<style>.generateTable {
border-collapse: collapse;
width: 100%;
}
.generateTable td, .generateTable th {
border: 1px solid #ddd;
padding: 8px;
}
.generateTable tr:nth-child(even){background-color: #f2f2f2;}
.generateTable tr:hover {background-color: #ddd;}
.generateTable th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
</style>';
} else {
$html = '';
}
$html .= '<table class="' . (is_string($css) ? $css : 'generateTable') . '">';
// header row
$html .= '<thead><tr >';
foreach ($array[0] as $key => $value) {
$html .= '<th >' . htmlspecialchars($key) . '</th>';
}
$html .= '</tr></thead>';
// data rows
foreach ($array as $key => $value) {
$html .= '<tr >';
foreach ($value as $key2 => $value2) {
if (is_array($value2)) {
$html .= '<td >' . self::generateTable($value2) . '</td>';
} else {
if($percentage) {
$p=round($value2/$min*100 ,2).'%';
$html .= '<td >' . htmlspecialchars($value2) .' '.$p.'</td>';
} else {
$html .= '<td >' . htmlspecialchars($value2) .'</td>';
}
}
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}
/**
* Split a string using an opening and closing tag.<br/>
* Example:<br/>
* Collection::splitOpeningClosing("a(B,C,D)e(F,G,H)"); // ['a','B,C,D','e','F,G,H']<br/>
* Collection::splitOpeningClosing("a(B,C,D)e(F,G,H)i"); // ['a','B,C,D','e','F,G,H','i'] <br>
*
* @param string $text input text to separated
* @param string $openingTag Opening tag
* @param string $closingTag closing tag
* @param int $startPosition start position (by default it is zero)
* @param bool $excludeEmpty if true then it excludes all empty values.
* @param bool $includeTag if true then it includes the tag.
*
* @return array If errror then it returns an empty array
*/
public static function splitOpeningClosing(
$text,
$openingTag = '(',
$closingTag = ')',
$startPosition = 0,
$excludeEmpty = true,
$includeTag = false
) {
if (!$text) {
return [];
}
$p0 = $startPosition;
$oL = strlen($openingTag);
$cL = strlen($closingTag);
$result = [];
// starting.
$even = false;
while ($p0 !== false) {
if (!$even) {
$p1 = strpos($text, $openingTag, $p0);
if ($p1 === false) {
$result[] = substr($text, $p0);
break;
}
$result[] = substr($text, $p0, $p1 - $p0);
$even = true;
$p0 = $p1 + $oL;
} else {
$p1 = strpos($text, $closingTag, $p0);
if ($p1 === false) {
$result[] = substr($text, $p0);
break;
}
$result[] = $includeTag ? $openingTag . substr($text, $p0, $p1 - $p0) . $closingTag
: substr($text, $p0, $p1 - $p0);
$even = false;
$p0 = $p1 + $cL;
}
}
if ($excludeEmpty) {
return array_values(array_filter($result, function ($value) {
return $value !== "";
})); // array_values for rebuild the index (array_filter deletes items but not reindex
} else {
return $result;
}
}
/**
* Split a string by ignoring parts of string where values are between " or '.<br>
* Example:<br/>
* Collection::splitNotString('a,b,"CC,D,E",e,f' ,","); // ['a','b','CC,D,E','e','f']<br/>
*
* @param string $text input text
* @param string $separator
* @param int $offset
* @param bool $excludeEmpty
*
* @return array
*/
public static function splitNotString($text, $separator, $offset = 0, $excludeEmpty = true) {
$p0 = $offset;
$even = false;
$sL = strlen($separator);
$pc = null;
$result = [];
while ($p0 !== false) {
if (!$even) {
$p1 = strpos($text, $separator, $p0);
$p1 = ($p1 === false) ? PHP_INT_MAX : $p1;
$p2 = strpos($text, '"', $p0);
$p2 = ($p2 === false) ? PHP_INT_MAX : $p2;
$p3 = strpos($text, "'", $p0);
$p3 = ($p3 === false) ? PHP_INT_MAX : $p3;
$ptxt = min($p2, $p3);
if ($p1 == PHP_INT_MAX && $ptxt == PHP_INT_MAX) {
// end
$result[] = substr($text, $p0);
break;
}
if ($p1 < $ptxt) {
// the next separator is a separator
$result[] = substr($text, $p0, $p1 - $p0);
$p0 = $p1 + $sL;
} else {
// the next separator is a string
$pc = substr($text, $ptxt, 1); // " or '
$even = true;
$p0 = $ptxt + 1;
}
} else {
// we are inside a string
$p1 = strpos($text, $pc, $p0);
if ($p1 === false) {
// we don't found the closing tag
$result[] = substr($text, $p0);
break;
}
$result[] = substr($text, $p0, $p1 - $p0);
$even = false; // and we are out of the string
$p0 = $p1 + 1;
}
}
if ($excludeEmpty) {
return array_values(array_filter($result, function ($value) {
return $value !== "";
}));
} else {
return $result;
}
}
/**
* It changes the case (to lower or upper case) of the keys of an array recursively
*
* @param array $array input array
*
* @param int $case [optional] by default is CASE_LOWER <p>
* Either CASE_UPPER or
* CASE_LOWER (default)</p>
*
* @return array
* @see https://www.php.net/manual/en/function.array-change-key-case.php
*/
public static function arrayChangeKeyCaseRecursive($array, $case = CASE_LOWER) {
return array_map(function ($item) {
if (is_array($item)) {
$item = self::arrayChangeKeyCaseRecursive($item);
}
return $item;
}, array_change_key_case($array, $case));
}
/**
* It returns the first (or all) key(s) inside an array/object in an array that matches the value of the field<br>
* Example: arraySearchField([['name'=>'john'],['name'=>'mary']],'name','mary'); // returns 1
*
* @param array $array input array
* @param string|int $fieldName name of index of the field
* @param mixed $value value to search
*
* @param bool $returnAll if true then it returns all matches. If false it returns the first value.
*
* @return int|string|bool|array return false if not found or if error.
*/
public static function arraySearchField($array, $fieldName, $value, $returnAll = false) {
$first = reset($array);
$result = [];
if (is_object($first)) {
foreach ($array as $k => $v) {
if (@$v->{$fieldName} === $value) {
if ($returnAll) {
$result[] = $k;
} else {
return $k;
}
}
}
if ($returnAll) {
return $result;
} else {
return false;
}
}
if (is_array($first)) {
foreach ($array as $k => $v) {
if (@$v[$fieldName] === $value) {
if ($returnAll) {
$result[] = $k;
} else {
return $k;
}
}
}
if ($returnAll) {
return $result;
} else {
return false;
}
}
return false;
}
}