Skip to content

Commit db0aeb8

Browse files
committed
👽 Supports php >= 7.1 like polyfill libraries
1 parent 854f8a3 commit db0aeb8

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
},
1010
"require": {
11-
"php": "^7.3"
11+
"php": "^7.1"
1212
},
1313
"license": "MIT",
1414
"version": "4.1.0"

src/kim/present/lib/arrayutils/ArrayUtils.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* ( . .) ♥
2020
* c(")(")
2121
*
22+
* @noinspection PhpUnused
2223
* @noinspection MagicMethodsValidityInspection
2324
* @noinspection PhpDocSignatureIsNotCompleteInspection
2425
*/
@@ -65,7 +66,6 @@
6566
use function min;
6667
use function random_int;
6768
use function sort;
68-
use function str_ends_with;
6969
use function strpos;
7070
use function substr;
7171
use function substr_replace;
@@ -442,7 +442,7 @@ protected static function __chunk(array $from, int $size, bool $preserveKeys = f
442442
*
443443
* @link https://arrayutils.docs.present.kim/methods/c/column
444444
*/
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{
446446
return array_column($from, $valueKey, $indexKey);
447447
}
448448

@@ -538,14 +538,11 @@ protected static function _every(array $from, callable $callback) : bool{
538538
*
539539
* @link https://arrayutils.docs.present.kim/methods/c/fill/keys
540540
*/
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);
547544
for(; $i < $max; ++$i){
548-
$from[$i] = $value;
545+
$from[$keys[$i]] = $value;
549546
}
550547

551548
return $from;
@@ -558,7 +555,7 @@ protected static function __fill(array $from, $value, int $start = 0, int $end =
558555
*
559556
* @link https://arrayutils.docs.present.kim/methods/c/fill/keys
560557
*/
561-
protected static function __fillKeys(array $from, mixed $value) : array{
558+
protected static function __fillKeys(array $from, $value) : array{
562559
return array_fill_keys($from, $value);
563560
}
564561

@@ -744,15 +741,11 @@ protected static function __reverse(array $from, bool $preserveKeys = false) : a
744741
*
745742
* @link https://arrayutils.docs.present.kim/methods/c/slice
746743
*/
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{
748745
$array = [];
749746
$keys = array_keys($from);
750747
$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);
756749
for(; $i < $max; ++$i){
757750
if($preserveKeys){
758751
$array[$keys[$i]] = $values[$i];
@@ -863,9 +856,11 @@ protected static function _keyExists(array $from, $key) : bool{
863856
/**
864857
* Returns the first index at which a given element can be found in the array
865858
*
859+
*
860+
* @return int|string|null
866861
* @link https://arrayutils.docs.present.kim/methods/g/index-of
867862
*/
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){
869864
$keys = array_keys($from);
870865
$values = array_values($from);
871866
$count = count($from);
@@ -884,7 +879,7 @@ protected static function _indexOf(array $from, $needle, int $start = 0) : int|s
884879
*
885880
* @link https://arrayutils.docs.present.kim/methods/g/find
886881
*/
887-
protected static function _find(array $from, callable $callback) : mixed{
882+
protected static function _find(array $from, callable $callback){
888883
foreach($from as $key => $value){
889884
if($callback($value, $key, $from)){
890885
return $value;
@@ -896,9 +891,10 @@ protected static function _find(array $from, callable $callback) : mixed{
896891
/**
897892
* Returns the key of the first element that that pass the $callback function
898893
*
894+
* @return int|string|null
899895
* @link https://arrayutils.docs.present.kim/methods/g/find/index
900896
*/
901-
protected static function _findIndex(array $from, callable $callback) : int|string|null{
897+
protected static function _findIndex(array $from, callable $callback){
902898
foreach($from as $key => $value){
903899
if($callback($value, $key, $from)){
904900
return $key;
@@ -912,16 +908,17 @@ protected static function _findIndex(array $from, callable $callback) : int|stri
912908
*
913909
* @link https://arrayutils.docs.present.kim/methods/g/first
914910
*/
915-
protected static function _first(array $from) : mixed{
911+
protected static function _first(array $from){
916912
return $from[self::_keyFirst($from)];
917913
}
918914

919915
/**
920916
* Gets the first key of an array
921917
*
918+
* @return int|string|null
922919
* @link https://arrayutils.docs.present.kim/methods/g/first/key
923920
*/
924-
protected static function _keyFirst(array $from) : int|string|null{
921+
protected static function _keyFirst(array $from){
925922
return array_keys($from)[0] ?? null;
926923
}
927924

@@ -930,16 +927,17 @@ protected static function _keyFirst(array $from) : int|string|null{
930927
*
931928
* @link https://arrayutils.docs.present.kim/methods/g/last
932929
*/
933-
protected static function _last(array $from) : mixed{
930+
protected static function _last(array $from){
934931
return $from[self::_keyLast($from)];
935932
}
936933

937934
/**
938935
* Gets the last key of an array
939936
*
937+
* @return int|string|null
940938
* @link https://arrayutils.docs.present.kim/methods/g/last/key
941939
*/
942-
protected static function _keyLast(array $from) : int|string|null{
940+
protected static function _keyLast(array $from){
943941
return array_keys($from)[count($from) - 1] ?? null;
944942
}
945943

@@ -948,19 +946,20 @@ protected static function _keyLast(array $from) : int|string|null{
948946
*
949947
* @link https://arrayutils.docs.present.kim/methods/g/random
950948
*/
951-
protected static function _random(array $from) : mixed{
949+
protected static function _random(array $from){
952950
return $from[self::_keyRandom($from)];
953951
}
954952

955953
/**
956954
* Gets the random key of an array
957955
*
956+
* @return int|string|null
958957
* @link https://arrayutils.docs.present.kim/methods/g/random/key
959958
*/
960-
protected static function _keyRandom(array $from) : int|string|null{
959+
protected static function _keyRandom(array $from){
961960
try{
962961
return ($keys = array_keys($from))[random_int(0, count($keys) - 1)] ?? null;
963-
}catch(Exception){
962+
}catch(Exception $_){
964963
return null;
965964
}
966965
}
@@ -1037,9 +1036,10 @@ protected static function _splice(array &$from, int $offset, ?int $length = null
10371036
/**
10381037
* Calculate the sum of values in an array
10391038
*
1039+
* @return int|float
10401040
* @link https://arrayutils.docs.present.kim/methods/g/sum
10411041
*/
1042-
protected static function _sum(array $from) : int|float{
1042+
protected static function _sum(array $from){
10431043
return array_sum($from);
10441044
}
10451045

@@ -1054,13 +1054,20 @@ protected static function __mergeSoft(...$values) : array{
10541054
}
10551055

10561056
/** 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){
10581058
return self::_indexOf($from, $needle, $start);
10591059
}
10601060

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+
10611068
/** @throws BadMethodCallException */
10621069
public function __call(string $name, array $arguments){
1063-
if($raw = str_ends_with($name, "As")){
1070+
if($raw = (substr($name, -2) === "As")){
10641071
$name = substr($name, 0, -2);
10651072
}
10661073

0 commit comments

Comments
 (0)