@@ -35,9 +35,9 @@ public static function accessible($value): bool
35
35
*/
36
36
public static function isAssoc (array $ array ): bool
37
37
{
38
- $ keys = array_keys ($ array );
38
+ $ keys = \ array_keys ($ array );
39
39
40
- return array_keys ($ keys ) !== $ keys ;
40
+ return \ array_keys ($ keys ) !== $ keys ;
41
41
}
42
42
43
43
/**
@@ -64,9 +64,9 @@ public static function toObject($array, $class = \stdClass::class)
64
64
$ object = new $ class ;
65
65
66
66
foreach ($ array as $ name => $ value ) {
67
- $ name = trim ($ name );
67
+ $ name = \ trim ($ name );
68
68
69
- if (!$ name || is_numeric ($ name )) {
69
+ if (!$ name || \ is_numeric ($ name )) {
70
70
continue ;
71
71
}
72
72
@@ -104,7 +104,7 @@ public static function gets(array &$data, array $needKeys = [], $unsetKey = fals
104
104
if (\is_int ($ default )) {
105
105
$ value = (int )$ value ;
106
106
} elseif (\is_string ($ default )) {
107
- $ value = trim ($ value );
107
+ $ value = \ trim ($ value );
108
108
} elseif (\is_array ($ default )) {
109
109
$ value = (array )$ value ;
110
110
}
@@ -145,7 +145,7 @@ public static function merge($src, array $new): array
145
145
} else {
146
146
$ src [$ key ] = $ value ;
147
147
}
148
- } elseif (array_key_exists ($ key , $ src ) && \is_array ($ value )) {
148
+ } elseif (\ array_key_exists ($ key , $ src ) && \is_array ($ value )) {
149
149
$ src [$ key ] = self ::merge ($ src [$ key ], $ new [$ key ]);
150
150
} else {
151
151
$ src [$ key ] = $ value ;
@@ -196,12 +196,12 @@ public static function merge2(...$args): array
196
196
*/
197
197
public static function valueTrim (array $ data )
198
198
{
199
- if (is_scalar ($ data )) {
200
- return trim ($ data );
199
+ if (\ is_scalar ($ data )) {
200
+ return \ trim ($ data );
201
201
}
202
202
203
- array_walk_recursive ($ data , function (&$ value ) {
204
- $ value = trim ($ value );
203
+ \ array_walk_recursive ($ data , function (&$ value ) {
204
+ $ value = \ trim ($ value );
205
205
});
206
206
207
207
return $ data ;
@@ -215,7 +215,7 @@ public static function valueTrim(array $data)
215
215
*/
216
216
public static function keyExists ($ key , array $ arr ): bool
217
217
{
218
- return array_key_exists (strtolower ($ key ), array_change_key_case ($ arr ));
218
+ return \ array_key_exists (\ strtolower ($ key ), \ array_change_key_case ($ arr ));
219
219
}
220
220
221
221
/**
@@ -251,7 +251,7 @@ public static function changeValueCase($arr, $toUpper = 1): array
251
251
if (\is_array ($ v )) {
252
252
$ newArr [$ k ] = self ::changeValueCase ($ v , $ toUpper );
253
253
} else {
254
- $ v = trim ($ v );
254
+ $ v = \ trim ($ v );
255
255
$ newArr [$ k ] = $ function ($ v );
256
256
}
257
257
}
@@ -271,11 +271,11 @@ public static function valueExistsAll($check, array $sampleArr): bool
271
271
{
272
272
// 以逗号分隔的会被拆开,组成数组
273
273
if (\is_string ($ check )) {
274
- $ check = trim ($ check , ', ' );
275
- $ check = strpos ($ check , ', ' ) !== false ? explode (', ' , $ check ) : [$ check ];
274
+ $ check = \ trim ($ check , ', ' );
275
+ $ check = \ strpos ($ check , ', ' ) !== false ? explode (', ' , $ check ) : [$ check ];
276
276
}
277
277
278
- return !array_diff ((array )$ check , $ sampleArr );
278
+ return !\ array_diff ((array )$ check , $ sampleArr );
279
279
}
280
280
281
281
/**
@@ -289,11 +289,11 @@ public static function valueExistsOne($check, array $sampleArr): bool
289
289
{
290
290
// 以逗号分隔的会被拆开,组成数组
291
291
if (\is_string ($ check )) {
292
- $ check = trim ($ check , ', ' );
293
- $ check = strpos ($ check , ', ' ) !== false ? explode (', ' , $ check ) : [$ check ];
292
+ $ check = \ trim ($ check , ', ' );
293
+ $ check = \ strpos ($ check , ', ' ) !== false ? explode (', ' , $ check ) : [$ check ];
294
294
}
295
295
296
- return (bool )array_intersect ((array )$ check , $ sampleArr );
296
+ return (bool )\ array_intersect ((array )$ check , $ sampleArr );
297
297
}
298
298
299
299
/**
@@ -377,8 +377,8 @@ public static function getKeyMaxWidth(array $data, $expectInt = true): int
377
377
378
378
foreach ($ data as $ key => $ value ) {
379
379
// key is not a integer
380
- if (!$ expectInt || !is_numeric ($ key )) {
381
- $ width = mb_strlen ($ key , 'UTF-8 ' );
380
+ if (!$ expectInt || !\ is_numeric ($ key )) {
381
+ $ width = \ mb_strlen ($ key , 'UTF-8 ' );
382
382
$ keyMaxWidth = $ width > $ keyMaxWidth ? $ width : $ keyMaxWidth ;
383
383
}
384
384
}
@@ -426,6 +426,29 @@ public static function getByPath($data, string $path, $default = null, string $s
426
426
return $ dataTmp ;
427
427
}
428
428
429
+ /**
430
+ * findValueByNodes
431
+ * @param array $data
432
+ * @param array $nodes
433
+ * @param mixed $default
434
+ * @return mixed
435
+ */
436
+ public static function getValueByNodes (array $ data , array $ nodes , $ default = null )
437
+ {
438
+ $ temp = $ data ;
439
+
440
+ foreach ($ nodes as $ name ) {
441
+ if (isset ($ temp [$ name ])) {
442
+ $ temp = $ temp [$ name ];
443
+ } else {
444
+ $ temp = $ default ;
445
+ break ;
446
+ }
447
+ }
448
+
449
+ return $ temp ;
450
+ }
451
+
429
452
/**
430
453
* setByPath
431
454
* @param array|\ArrayAccess &$data
@@ -435,12 +458,12 @@ public static function getByPath($data, string $path, $default = null, string $s
435
458
*/
436
459
public static function setByPath (&$ data , string $ path , $ value , string $ separator = '. ' )
437
460
{
438
- if (false === strpos ($ path , $ separator )) {
461
+ if (false === \ strpos ($ path , $ separator )) {
439
462
$ data [$ path ] = $ value ;
440
463
return ;
441
464
}
442
465
443
- if (!$ nodes = array_filter (explode ($ separator , $ path ))) {
466
+ if (!$ nodes = \ array_filter (\ explode ($ separator , $ path ))) {
444
467
return ;
445
468
}
446
469
@@ -473,7 +496,7 @@ public static function setByPath(&$data, string $path, $value, string $separator
473
496
* @param array $array
474
497
* @return array
475
498
*/
476
- public static function collapse ($ array ): array
499
+ public static function collapse (array $ array ): array
477
500
{
478
501
$ results = [];
479
502
@@ -484,10 +507,11 @@ public static function collapse($array): array
484
507
continue ;
485
508
}
486
509
487
- $ results = \array_merge ($ results , $ values );
510
+ // $results = \array_merge($results, $values);
511
+ $ results [] = $ values ;
488
512
}
489
513
490
- return $ results ;
514
+ return \array_merge (... $ results) ;
491
515
}
492
516
493
517
/**
@@ -513,7 +537,7 @@ public static function crossJoin(...$arrays): array
513
537
*/
514
538
public static function divide ($ array ): array
515
539
{
516
- return [array_keys ($ array ), array_values ($ array )];
540
+ return [\ array_keys ($ array ), \ array_values ($ array )];
517
541
}
518
542
519
543
/**
@@ -522,7 +546,7 @@ public static function divide($array): array
522
546
* @param string $prepend
523
547
* @return array
524
548
*/
525
- public static function dot ($ array , $ prepend = '' ): array
549
+ public static function dot (array $ array , string $ prepend = '' ): array
526
550
{
527
551
$ results = [];
528
552
@@ -543,7 +567,7 @@ public static function dot($array, $prepend = ''): array
543
567
* @param array|string $keys
544
568
* @return array
545
569
*/
546
- public static function except ($ array , $ keys ): array
570
+ public static function except (array $ array , $ keys ): array
547
571
{
548
572
static ::forget ($ array , $ keys );
549
573
@@ -556,7 +580,7 @@ public static function except($array, $keys): array
556
580
* @param string|int $key
557
581
* @return bool
558
582
*/
559
- public static function exists ($ array , $ key ): bool
583
+ public static function exists (array $ array , $ key ): bool
560
584
{
561
585
if ($ array instanceof \ArrayAccess) {
562
586
return $ array ->offsetExists ($ key );
@@ -572,7 +596,7 @@ public static function exists($array, $key): bool
572
596
* @param mixed $value
573
597
* @return array
574
598
*/
575
- public static function add ($ array , $ key , $ value ): array
599
+ public static function add (array $ array , $ key , $ value ): array
576
600
{
577
601
if (static ::has ($ array , $ key )) {
578
602
static ::set ($ array , $ key , $ value );
@@ -621,13 +645,13 @@ public static function get($array, $key, $default = null)
621
645
* @param mixed $value
622
646
* @return array
623
647
*/
624
- public static function set (&$ array , $ key , $ value ): array
648
+ public static function set (array &$ array , $ key , $ value ): array
625
649
{
626
650
if (null === $ key ) {
627
651
return ($ array = $ value );
628
652
}
629
653
630
- $ keys = explode ('. ' , $ key );
654
+ $ keys = \ explode ('. ' , $ key );
631
655
632
656
while (\count ($ keys ) > 1 ) {
633
657
$ key = array_shift ($ keys );
@@ -641,7 +665,7 @@ public static function set(&$array, $key, $value): array
641
665
$ array = &$ array [$ key ];
642
666
}
643
667
644
- $ array [array_shift ($ keys )] = $ value ;
668
+ $ array [\ array_shift ($ keys )] = $ value ;
645
669
646
670
return $ array ;
647
671
}
@@ -658,14 +682,14 @@ public static function flatten($array, $depth = INF): array
658
682
$ item = $ item instanceof CollectionInterface ? $ item ->all () : $ item ;
659
683
660
684
if (!\is_array ($ item )) {
661
- return array_merge ($ result , [$ item ]);
685
+ return \ array_merge ($ result , [$ item ]);
662
686
}
663
687
664
688
if ($ depth === 1 ) {
665
- return array_merge ($ result , array_values ($ item ));
689
+ return \ array_merge ($ result , \ array_values ($ item ));
666
690
}
667
691
668
- return array_merge ($ result , static ::flatten ($ item , $ depth - 1 ));
692
+ return \ array_merge ($ result , static ::flatten ($ item , $ depth - 1 ));
669
693
}, []);
670
694
}
671
695
0 commit comments