19
19
* ( . .) ♥
20
20
* c(")(")
21
21
*
22
+ * @noinspection PhpUnused
22
23
* @noinspection MagicMethodsValidityInspection
23
24
* @noinspection PhpDocSignatureIsNotCompleteInspection
24
25
*/
65
66
use function min ;
66
67
use function random_int ;
67
68
use function sort ;
68
- use function str_ends_with ;
69
69
use function strpos ;
70
70
use function substr ;
71
71
use function substr_replace ;
@@ -442,7 +442,7 @@ protected static function __chunk(array $from, int $size, bool $preserveKeys = f
442
442
*
443
443
* @link https://arrayutils.docs.present.kim/methods/c/column
444
444
*/
445
- protected static function __column (array $ from , int | string $ valueKey , int | string | null $ indexKey = null ) : array {
445
+ protected static function __column (array $ from , $ valueKey , $ indexKey = null ) : array {
446
446
return array_column ($ from , $ valueKey , $ indexKey );
447
447
}
448
448
@@ -538,14 +538,11 @@ protected static function _every(array $from, callable $callback) : bool{
538
538
*
539
539
* @link https://arrayutils.docs.present.kim/methods/c/fill/keys
540
540
*/
541
- protected static function __fill (array $ from , $ value , int $ start = 0 , int $ end = null ) : array {
542
- $ count = count ($ from );
543
- $ end = $ end ?? $ count ;
544
-
545
- $ i = $ start < 0 ? max ($ count + $ start , 0 ) : min ($ start , $ count );
546
- $ max = $ end < 0 ? max ($ count + $ end , 0 ) : min ($ end , $ count );
541
+ protected static function __fill (array $ from , $ value , int $ start = 0 , int $ end = PHP_INT_MAX ) : array {
542
+ $ keys = array_keys ($ from );
543
+ [$ i , $ max ] = self ::range (count ($ keys ), $ start , $ end );
547
544
for (; $ i < $ max ; ++$ i ){
548
- $ from [$ i ] = $ value ;
545
+ $ from [$ keys [ $ i ] ] = $ value ;
549
546
}
550
547
551
548
return $ from ;
@@ -558,7 +555,7 @@ protected static function __fill(array $from, $value, int $start = 0, int $end =
558
555
*
559
556
* @link https://arrayutils.docs.present.kim/methods/c/fill/keys
560
557
*/
561
- protected static function __fillKeys (array $ from , mixed $ value ) : array {
558
+ protected static function __fillKeys (array $ from , $ value ) : array {
562
559
return array_fill_keys ($ from , $ value );
563
560
}
564
561
@@ -744,15 +741,11 @@ protected static function __reverse(array $from, bool $preserveKeys = false) : a
744
741
*
745
742
* @link https://arrayutils.docs.present.kim/methods/c/slice
746
743
*/
747
- protected static function __slice (array $ from , int $ start = 0 , int $ end = null , bool $ preserveKeys = false ) : array {
744
+ protected static function __slice (array $ from , int $ start = 0 , int $ end = PHP_INT_MAX , bool $ preserveKeys = false ) : array {
748
745
$ array = [];
749
746
$ keys = array_keys ($ from );
750
747
$ values = array_values ($ from );
751
- $ count = count ($ from );
752
- $ end = $ end ?? $ count ;
753
-
754
- $ i = $ start < 0 ? max ($ count + $ start , 0 ) : min ($ start , $ count );
755
- $ max = $ end < 0 ? max ($ count + $ end , 0 ) : min ($ end , $ count );
748
+ [$ i , $ max ] = self ::range (count ($ keys ), $ start , $ end );
756
749
for (; $ i < $ max ; ++$ i ){
757
750
if ($ preserveKeys ){
758
751
$ array [$ keys [$ i ]] = $ values [$ i ];
@@ -863,9 +856,11 @@ protected static function _keyExists(array $from, $key) : bool{
863
856
/**
864
857
* Returns the first index at which a given element can be found in the array
865
858
*
859
+ *
860
+ * @return int|string|null
866
861
* @link https://arrayutils.docs.present.kim/methods/g/index-of
867
862
*/
868
- protected static function _indexOf (array $ from , $ needle , int $ start = 0 ) : int | string | null {
863
+ protected static function _indexOf (array $ from , $ needle , int $ start = 0 ){
869
864
$ keys = array_keys ($ from );
870
865
$ values = array_values ($ from );
871
866
$ count = count ($ from );
@@ -884,7 +879,7 @@ protected static function _indexOf(array $from, $needle, int $start = 0) : int|s
884
879
*
885
880
* @link https://arrayutils.docs.present.kim/methods/g/find
886
881
*/
887
- protected static function _find (array $ from , callable $ callback ) : mixed {
882
+ protected static function _find (array $ from , callable $ callback ){
888
883
foreach ($ from as $ key => $ value ){
889
884
if ($ callback ($ value , $ key , $ from )){
890
885
return $ value ;
@@ -896,9 +891,10 @@ protected static function _find(array $from, callable $callback) : mixed{
896
891
/**
897
892
* Returns the key of the first element that that pass the $callback function
898
893
*
894
+ * @return int|string|null
899
895
* @link https://arrayutils.docs.present.kim/methods/g/find/index
900
896
*/
901
- protected static function _findIndex (array $ from , callable $ callback ) : int | string | null {
897
+ protected static function _findIndex (array $ from , callable $ callback ){
902
898
foreach ($ from as $ key => $ value ){
903
899
if ($ callback ($ value , $ key , $ from )){
904
900
return $ key ;
@@ -912,16 +908,17 @@ protected static function _findIndex(array $from, callable $callback) : int|stri
912
908
*
913
909
* @link https://arrayutils.docs.present.kim/methods/g/first
914
910
*/
915
- protected static function _first (array $ from ) : mixed {
911
+ protected static function _first (array $ from ){
916
912
return $ from [self ::_keyFirst ($ from )];
917
913
}
918
914
919
915
/**
920
916
* Gets the first key of an array
921
917
*
918
+ * @return int|string|null
922
919
* @link https://arrayutils.docs.present.kim/methods/g/first/key
923
920
*/
924
- protected static function _keyFirst (array $ from ) : int | string | null {
921
+ protected static function _keyFirst (array $ from ){
925
922
return array_keys ($ from )[0 ] ?? null ;
926
923
}
927
924
@@ -930,16 +927,17 @@ protected static function _keyFirst(array $from) : int|string|null{
930
927
*
931
928
* @link https://arrayutils.docs.present.kim/methods/g/last
932
929
*/
933
- protected static function _last (array $ from ) : mixed {
930
+ protected static function _last (array $ from ){
934
931
return $ from [self ::_keyLast ($ from )];
935
932
}
936
933
937
934
/**
938
935
* Gets the last key of an array
939
936
*
937
+ * @return int|string|null
940
938
* @link https://arrayutils.docs.present.kim/methods/g/last/key
941
939
*/
942
- protected static function _keyLast (array $ from ) : int | string | null {
940
+ protected static function _keyLast (array $ from ){
943
941
return array_keys ($ from )[count ($ from ) - 1 ] ?? null ;
944
942
}
945
943
@@ -948,19 +946,20 @@ protected static function _keyLast(array $from) : int|string|null{
948
946
*
949
947
* @link https://arrayutils.docs.present.kim/methods/g/random
950
948
*/
951
- protected static function _random (array $ from ) : mixed {
949
+ protected static function _random (array $ from ){
952
950
return $ from [self ::_keyRandom ($ from )];
953
951
}
954
952
955
953
/**
956
954
* Gets the random key of an array
957
955
*
956
+ * @return int|string|null
958
957
* @link https://arrayutils.docs.present.kim/methods/g/random/key
959
958
*/
960
- protected static function _keyRandom (array $ from ) : int | string | null {
959
+ protected static function _keyRandom (array $ from ){
961
960
try {
962
961
return ($ keys = array_keys ($ from ))[random_int (0 , count ($ keys ) - 1 )] ?? null ;
963
- }catch (Exception ){
962
+ }catch (Exception $ _ ){
964
963
return null ;
965
964
}
966
965
}
@@ -1037,9 +1036,10 @@ protected static function _splice(array &$from, int $offset, ?int $length = null
1037
1036
/**
1038
1037
* Calculate the sum of values in an array
1039
1038
*
1039
+ * @return int|float
1040
1040
* @link https://arrayutils.docs.present.kim/methods/g/sum
1041
1041
*/
1042
- protected static function _sum (array $ from ) : int | float {
1042
+ protected static function _sum (array $ from ){
1043
1043
return array_sum ($ from );
1044
1044
}
1045
1045
@@ -1054,13 +1054,20 @@ protected static function __mergeSoft(...$values) : array{
1054
1054
}
1055
1055
1056
1056
/** Alias of @see ArrayUtils::_indexOf() */
1057
- protected static function _search (array $ from , $ needle , int $ start = 0 ) : int | string | null {
1057
+ protected static function _search (array $ from , $ needle , int $ start = 0 ){
1058
1058
return self ::_indexOf ($ from , $ needle , $ start );
1059
1059
}
1060
1060
1061
+ private static function range (int $ count , int $ start , int $ end = PHP_INT_MAX ) : array {
1062
+ return [
1063
+ $ start < 0 ? max ($ count + $ start , 0 ) : min ($ start , $ count ),
1064
+ $ end < 0 ? max ($ count + $ end , 0 ) : min ($ end , $ count )
1065
+ ];
1066
+ }
1067
+
1061
1068
/** @throws BadMethodCallException */
1062
1069
public function __call (string $ name , array $ arguments ){
1063
- if ($ raw = str_ends_with ( $ name , "As " )){
1070
+ if ($ raw = ( substr ( $ name , - 2 ) === "As " )){
1064
1071
$ name = substr ($ name , 0 , -2 );
1065
1072
}
1066
1073
0 commit comments