-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAmslib_Array.php
executable file
·889 lines (760 loc) · 27.5 KB
/
Amslib_Array.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
<?php
/*******************************************************************************
* Copyright (c) {15/03/2008} {Christopher Thomas}
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contributors/Author:
* {Christopher Thomas} - Creator - [email protected]
*
*******************************************************************************/
/**
* class: Amslib_Array
*
* group: core
*
* file: Amslib_Array.php
*
* description: todo, write description
*
* todo: write documentation
*
* notes:
* - there are sometimes better versions of methods here in Amslib_Array2 object which is being used
* to explore better ideas, consider those before using versions in this object
*
*/
class Amslib_Array
{
/**
* method: testKeys
*
* Test whether keys exist in an array or not
*
* NOTE: I think I could replace some of this code with a cheaper array_intersect or something instead
*/
static protected function testKeys($array,$keys,$ignoreFailure=false,$test=false)
{
if(!is_array($array)) return NULL;
if(is_string($keys) && strlen($keys)) $keys = array($keys);
if(!count($keys)) return NULL;
$found = array();
foreach(self::valid($keys) as $k){
if(array_key_exists($k,$array) === $test){
if($test){
$found[$k] = $array[$k];
}
}else{
if($ignoreFailure == false){
return false;
}
}
}
return $found;
}
/**
* method: valid
*
* todo: write documentation
*
* notes:
* - if you return nothing from a method, then catch that into a parameter
* pass that parameter into this function, you'll freeze the browser
* well done francisco for catching this :)
*
* - Extended this method to support passing in a variable and then asking
* for a key instead of passing it directly. This is useful when you have
* a variable but not sure whether the key exists or not, if it doesnt, then
* it'll cause an error to go into the log, but testing it by passing the
* variable and the key separately means we can handle the situation more gracefully
*/
static public function valid($array=NULL,$key=NULL)
{
if($key !== NULL && is_string($key)){
$array = array_key_exists($key,$array) ? $array[$key] : array();
}
// Invalid values return an empty array
if(empty($array) || !$array || !is_array($array) || is_null($array)){
return array();
}
// cast objects to arrays
if(is_object($array)){
$array = (array)$a;
}
// return the original value
return $array;
}
/**
* method: isMulti
*
* Check the array is a multidimensional array or not
* returns true if multi, false if not
*
* notes:
* - It's quite hard to know whether an array is an array containing results which are
* interesting, say an array of SQL rows, all containing various fields, or whether
* an array is just a normal array which may contain an array, but is not multi-dimensional
* - This method simplifies this a lot, but may present issues in itself, because it
* basically considers an array to be multidimensional, IF and ONLY IF, the array
* being tested, contains a single type of data "array", but this is ignoring quite
* a few types of array I can think of which are still multidimensional
*/
static public function isMulti($array)
{
$result = array_unique(array_map("gettype",$array));
return count($result) == 1 && array_shift($result) == "array";
}
static public function reindexByKeyValue($array,$key,$value)
{
$array = self::valid($array);
if(!is_string($key) || !is_string($value)){
return $array;
}
$copy = array();
foreach($array as $item){
if(isset($item[$key]) && isset($item[$value])){
$copy[$item[$key]] = $item[$value];
}
}
return $copy;
}
/**
* method: reindexByKey
*
* todo: write documentation
*
* FIXME: \$optimise value is never used
*/
static public function reindexByKey($array,$key,$optimise=false)
{
$array = self::valid($array);
if(!is_string($key)) return $array;
$copy = array();
foreach($array as $item){
if(isset($item[$key])){
$copy[$item[$key]] = $item;
}
}
return $copy;
}
/**
* method: fillMissing
*
* todo: write documentation
*
* notes:
* - This method allows me to batch fix missing keys in arrays in broken code quickly and easily.
*/
static public function fillMissing($array,$key,$value=NULL)
{
if(is_string($key)) $key = array($key);
return array_merge(array_fill_keys($key,$value),$array);
}
/**
* method: hasKeys
*
* Searches an array for a set of keys and returns either an
* array of the matching key/values, boolean false, or null depending on what the array contained
*
* parameters:
* $array - The array to search
* $keys - The keys used to search in the array
* $ignoreFailure - Whether or not to ignore failed tests and return what did match
*
* returns:
* - NULL if the array if not valid, boolean false for failure, or an array of matching key/values
* depending on the value of $ignoreFailure
*/
static public function hasKeys($array,$keys,$ignoreFailure=false)
{
if(is_string($keys) && is_string($ignoreFailure)){
Amslib_Debug::log(
"keys and ignoreFailure parameters are both detected as strings, probably you ".
"forgot this method accepts an array of strings and are just passing the keys ".
"one by one to the parameters of this function which will result in the first ".
"being tested and ignoreFailure will be enabled, I don't think this is what you intended",
$keys,
$ignoreFailure,
"stack_trace"
);
}
return self::testKeys($array,$keys,$ignoreFailure,true);
}
/**
* method: notKeys
*
* Searches an array for a set of keys and returns either an
* array of the non matching key/values, boolean false, or null depending on what the array contained
*
* parameters:
* $array - The array to search
* $keys - The keys used to search in the array
* $ignoreFailure - Whether or not to ignore failed tests and return what did match
*
* returns:
* - NULL if the array if not valid, boolean false for failure, or an array of non-matching key/values
*/
static public function notKeys($array,$keys)
{
$return = self::testKeys($array,$keys,false,false);
return is_array($return) ? true : $return;
}
/**
* method: sort
*
* todo: write documentation
*/
static public function sort($array,$index)
{
if(count($array) < 2) return $array;
$left = $right = array();
reset($array);
$pivot_key = key($array);
$pivot = array_shift($array);
if(!isset($pivot[$index])){
Amslib_Debug::log("ERROR, PIVOT INDEX DOES NOT EXIST","stack_trace");
}
foreach($array as $k => $v) {
if($v[$index] < $pivot[$index]){
$left[$k] = $v;
}else{
$right[$k] = $v;
}
}
return array_merge(self::sort($left,$index), array($pivot_key => $pivot), self::sort($right,$index));
}
/**
* method: pluck
*
* todo: write documentation
*/
static public function pluck($array,$key)
{
if(!is_array($array)){
return array();
}
if (!is_array($key)) {
$key = func_get_args();
array_shift($key);
}
if(self::isMulti($array)){
foreach($array as &$item){
$item = self::pluck($item,$key);
}
if(count($key) == 1){
foreach($array as &$r){
$r = $r[$key[0]];
}
}
return $array;
}
return array_intersect_key($array,array_flip($key));
}
static public function toKeyValue($list)
{
$list = self::valid($list);
$array = array();
if(empty($list) || count($list) == 1) return $array;
$k = NULL;
foreach($list as $index=>$item){
if($k == NULL){
if(is_numeric($item)){
$k = intval($item);
}else if(is_string($item)){
$k = $item;
}else{
Amslib_Debug::log("list was invalid, key not string or number",$index,$item,$k,$array);
return $array;
}
}else{
$array[$k] = $item;
$k = NULL;
}
}
return $array;
}
static private function ______UPGRADE_METHODS_BELOW(){}
/**
* method: min
*
* todo: write documentation
*/
static public function min($array,$key,$returnKey=NULL)
{
$min = NULL;
foreach(self::valid($array) as $item){
if($min === NULL) $min = $item;
if($item[$key] < $min[$key]) $min = $item;
}
return $returnKey !== NULL && isset($min[$returnKey]) ? $min[$returnKey] : $min;
}
/**
* method: max
*
* todo: write documentation
*/
static public function max($array,$key,$returnKey=NULL)
{
$max = NULL;
foreach(self::valid($array) as $item){
if($max === NULL) $max = $item;
if($item[$key] > $max[$key]) $max = $item;
}
return $returnKey !== NULL && isset($max[$returnKey]) ? $max[$returnKey] : $max;
}
/**
* method: unique
*
* todo: write documentation
*/
static public function unique($array,$field)
{
if(!self::isMulti($array)) return false;
$v = $unique = array();
foreach(self::valid($array) as $k=>$a){
if(isset($a[$field]) && !in_array($a[$field],$v)){
$v[] = $a[$field];
$unique[$k] = $a;
}
}
return $unique;
}
/**
* method: removeValue
*
* todo: write documentation
*/
static public function removeValue(array $array,$value,$strict=false)
{
return array_diff_key($array, array_flip(array_keys($array, $value, $strict)));
}
/**
* method:removeKeys
*
* This method is a wrapper around unset to make it easier and less verbose in your code to remove multiple elements
*
* note:
* - I'm not entirely sure this code does 100% of what I think it should
*/
static public function removeKeys($array,$keys)
{
$args = func_get_args()+array(array(),"");
$array = self::valid(array_shift($args));
foreach(self::valid($args) as $k){
if(is_array($k)){
array_unshift($k,$array);
$array = call_user_func_array("self::removeKeys",$k);
}else if(is_string($k) || is_numeric($k)){
unset($array[$k]);
}
}
return $array;
}
/**
* method: findKey
*
* look into an array and find a key which has a partial match for the key parameter
*
* parameters:
* $source - The source array to scan
* $key - The partial array key to look for
*
* returns:
* Boolean false if it was not found, or the first match key that was found
*
* notes:
* - we should extend this with the ability to find an exact or partial key
*/
static public function findKey($source,$key)
{
if(is_array($source)) foreach($source as $k=>$ignore){
if(strpos($k,$key) !== false) return $k;
}
return false;
}
/**
* method: diff
*
* todo: write documentation
*
* notes:
* - I got this function from here: http://stackoverflow.com/questions/8917039/php-checking-difference-between-two-multidim-arrays
* - 06/02/2013=> I modified this with the $strict parameter to let me check types as well as values
* - this method will tell you the missing keys and different values in the second array
*/
static public function diff($array1, $array2, $strict=false)
{
$return = array();
if(!is_array($array1) || !is_array($array2)) return $return;
foreach($array1 as $key => $value)
{
if(array_key_exists($key, $array2))
{
if (is_array($value))
{
$diff = self::diff($value, $array2[$key],$strict);
if(count($diff)){
$return[$key] = $diff;
}
}else{
if((!$strict && $value != $array2[$key]) || ($strict && $value !== $array2[$key])){
$return[$key] = $value;
}
}
}else{
$return[$key] = $value;
}
}
return $return;
}
/**
* method: diffBoth
*
* todo: write documentation
*
* notes:
* - this will give both sets of changes from both arrays compared against each other instead of in one direction only
*/
static public function diffBoth($array1,$array2,$strict=false)
{
return array(
"src" => $a=self::diff($array1,$array2,$strict),
"dst" => $b=self::diff($array2,$array1,$strict),
"equal" => count($a) == 0 && count($b) == 0
);
}
/**
* method: countValues
*
* todo: write documentation
*
* TODO: this method is a little open to abuse and in some situations wouldn't do the right thing
* TODO: explain in words what this does and how it should work
* NOTE: I am not sure what the above comment actually means, I should have been more explicit in my explanation
*/
static public function countValues($array)
{
$counts = array();
foreach(self::valid($array) as $v){
if(!isset($counts[$v])) $counts[$v] = 0;
$counts[$v]++;
}
return $counts;
}
/**
* method: find
*
* todo: write documentation
*/
static public function find($array,$key,$value)
{
$result = NULL;
foreach(self::valid($array) as $a){
if(array_key_exists($key,$a) && $a[$key] == $value){
return $a;
}
}
return false;
}
/* I don't know how to integrate this into the api yet, but it's a cool function!!!
// author: d3x from freenode's #php channel
// usage: find($array,$k1,$k2,$k3,$k4)
// description: you have a multi-dimensional array, the keys will represent a path to the final kv pair
function find($array) {
$args = func_get_args();
array_shift($args);
if ($args) {
$key = array_shift($args);
array_unshift($args, $array[$key]);
return call_user_func_array(__FUNCTION__, $args);
}
return $array;
}*/
/**
* method: searchKeys
*
* todo: write documentation
*/
static public function searchKeys($array,$filter)
{
$matches = array();
foreach(self::valid($array) as $k=>$v){
if(strpos($k,$filter) !== false) $matches[$k] = $v;
}
return $matches;
}
/**
* method: stripSlashesMulti
*
* todo: write documentation
*/
static public function stripSlashesMulti($array,$key=NULL)
{
if(is_string($key)) $key = array($key);
$array = self::valid($array);
foreach($array as &$element){
if(!$key){
$element = array_map("stripslashes",$element);
}else{
foreach($key as $index) $element[$index] = stripslashes($element[$index]);
}
}
return $array;
}
/**
* method: stripSlashesSingle
*
* todo: write documentation
*/
static public function stripSlashesSingle($array,$key="")
{
if($key == "") $key = array_keys($array);
if(!is_array($key)){
$array[$key] = stripslashes($array[$key]);
}else{
foreach($key as $index){
$array[$index] = stripslashes($array[$index]);
}
}
return $array;
}
/**
* method: stripSlashes
*
* todo: write documentation
*/
static public function stripSlashes($array,$key="")
{
if(!is_array($array) || empty($array)) return $array;
if($key == "") $key = array_keys($array);
return (self::isMulti($array)) ?
self::stripSlashesMulti($array,$key) :
self::stripSlashesSingle($array,$key);
}
// deprecated: this is not supposed to be here
// FIXME: glob() on an array object? when it refers to the filesystem or array? I think it's a mistake to put this method here
/**
* method: glob
*
* todo: write documentation
*/
static public function glob($location,$relative=false)
{
$items = glob(Amslib_Website::absolute($location));
if($relative){
foreach($items as &$i){
$i = Amslib_Website::relative($i);
}
}
return $items;
}
/**
* @param $array
* @param $callback
* @return array
*/
static public function filterFunction($array,$callback)
{
if(!is_callable($callback)){
throw new InvalidArgumentException("\$callback parameter was not callable function");
}
// find and search the values which are strings, filtering out those which are not
$map = array_map($callback,$array);
$map = array_filter($map);
// diff the keys against the original array to find the values which failed
$diff = array_diff_key($array,$map);
// build an array of false values in those array positions which failed
$diff = array_keys($diff);
$diff = array_fill_keys($diff,false);
// combine the two arrays missing keys first, to make sure the false block the invalid array
// indexes from copying and filter the result, leaving only the strings
$array = array_filter($diff+$array);
return $array;
}
/**
* method: filterType
*
* todo: write documentation
*
* notes:
* I dont think "filterType" is a good function name for this, when
* it takes a callback, perhaps filterCallback instead?
*/
static public function filterType($array,$callback)
{
return function_exists($callback) ? array_filter(self::valid($array),$callback) : $array;
}
/**
* method: filterKey
*
* todo: write documentation
*/
static public function filterKey($array,$filter,$similar=false,$multi=true)
{
if($similar) return self::filter($array,NULL,$filter,true,true);
$array = self::valid($array);
if(is_string($filter)) $filter = array($filter);
if(self::isMulti($array) && $multi){
// NOTE: perhaps I should recurse here?
// EXAMPLE: foreach($array as &$a) $a = self::FilterKey($a,$filter,$similar);
// NOTE: can we use array_map here??
// NOTE: I don't think array_map will work because this method requires parameters that it can't forward
foreach($array as &$a){
if(is_array($filter)){
$a = array_intersect_key($a, array_flip($filter));
}else if(isset($a[$filter])){
$a = $a[$filter];
}else{
$a = NULL;
}
}
}else{
if(!is_array($filter)){
Amslib_Debug::log("stack_trace","filter is not an array",$filter);
}else{
$array = array_intersect_key($array, array_flip($filter));
}
}
return $array;
}
/**
* method: filter
*
* todo: write documentation
*/
static public function filter($array,$key,$value,$returnFiltered=false,$similar=false)
{
$filter = array();
foreach(self::valid($array) as $k=>$v)
{
$found = false;
// TODO: I'm sure that there are more situations I could take into account here
// TODO: I should document exactly what this method does, because right now I can't remember
// FIXME: there is a bug here if the key doesnt exist in the array
// NOTE: there is a side effect of the bug, if the key doesnt exist, it'll return NULL,
// passing $value as true will mean it'll compare either the existing key against
// true or null against true, so it in effect is a facinatingly cool way to filter
// by key and only return arrays which have a particular key. This whilst being very nice
// is still a bug, I either need to codify this to make it official, or fix the bug
$search = $key ? $v[$key] : $v;
if($similar == true && strlen($value) && strpos($search,$value) !== false) $found = true;
else if($search == $value) $found = true;
else if(is_array($value)){
if($similar == true){
foreach($value as $subvalue){
if(strpos($search,$subvalue) !== false){
// NOTE: here I am adjusting the key to reply the key=>value meaning the subvalue=>value
// NOTE: but I am not sure whether I want to do this 100% of the time, for now, it's a good choice
$k = $subvalue;
$found = true;
break;
}
}
}else if(in_array($search,$value)){
$found = true;
}
}
if($found){
$filter[$k] = $v;
unset($array[$k]);
}
}
return $returnFiltered ? $filter : $array;
}
/**
* method: filter2
*
* todo: write documentation
*
* NOTE: I am trying to create a better documented, better understood version of this method
* cause the above method has gone a little crazy, with me not really understanding why
* I added certain code, but I don't want to change it directly in case it subtly breaks
* stuff that depends on it's method of operation until I can identify and change them all
*/
static public function filter2($array,$key,$value,$returnFiltered=false,$similar=false)
{
$matched = array();
foreach(self::valid($array) as $k=>$v){
$found = false;
if($key !== NULL && is_array($v) && isset($v[$key])){
// if each array element is an array, we need to search by the key given in each child array
$v = $v[$key];
}
if($similar == true && !is_array($v) && strpos($v,$value) !== false){
// similar was enabled, search value was not an array and a fuzzy match was found
$found = true;
}else if(!is_array($v) && $v == $value){
// similar was not enabled, search value was not an array, and a perfect match was found
$found = true;
}
if($found){
// match was found, add it to the matched array and remove it from the unmatched remaining array items
$matched[$k] = $v;
unset($array[$k]);
}
}
// If you ask for the filtered items, we return either the matched array or the remaining items not matched
return $returnFiltered ? $matched : $array;
}
/**
* method: wrap
*
* todo: write documentation
*
* NOTE: I am adding slashes to the " character, but this might not be the only character which
* causes a problem here, we need to investigate any potential others, but I think for now this is ok
*/
static public function wrap($array,$char)
{
$wrap = $char=="\"" ? addslashes($char) : $char;
return array_map(function($str) use ($char,$wrap){
return $wrap.trim($str,$char).$wrap;
},$array);
}
/**
* method: implode
*
* todo: write documentation
*/
static public function implode($array,$join=",",$quote="")
{
return $quote.implode($quote.$join.$quote,$array).$quote;
}
static private function ______DEPRECATED_METHODS_BELOW(){}
static public function missingKeys($array,$key,$value=NULL)
{
Amslib_Debug::log("DEPRECATED METHOD, USE self::fillMissing DIRECTLY");
return self::fillMissing($array,$key,$value);
}
static public function pluck2($array,$keys)
{
Amslib_Debug::log("DEPRECATED METHOD, USE self::pluck DIRECTLY");
return self::pluck($array,$keys);
}
static public function pluckMulti2($array,$keys)
{
Amslib_Debug::log("DEPRECATED METHOD, USE self::pluck DIRECTLY");
return self::pluck($array,$keys);
}
static public function implodeQuoteSingle($array,$join=",")
{
Amslib_Debug::log("DEPRECATED METHOD, USE self::implode DIRECTLY");
return self::implode($array,$join,"'");
}
static public function implodeQuoteDouble($array,$join=",")
{
Amslib_Debug::log("DEPRECATED METHOD, USE self::implode DIRECTLY");
return self::implode($array,$join,"\"");
}
}