Skip to content

Commit 6c7a01c

Browse files
committed
fix some error
1 parent e8c65f7 commit 6c7a01c

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/Arr/ArrayHelper.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use ArrayAccess;
1313
use ArrayObject;
1414
use stdClass;
15-
use Toolkit\Collection\CollectionInterface;
15+
use Toolkit\Stdlib\Php;
1616
use Traversable;
1717
use function array_change_key_case;
1818
use function array_diff;
@@ -39,6 +39,7 @@
3939
use function is_scalar;
4040
use function is_string;
4141
use function mb_strlen;
42+
use function method_exists;
4243
use function strlen;
4344
use function strpos;
4445
use function strtolower;
@@ -250,7 +251,7 @@ public static function valueTrim(array $data)
250251
return trim($data);
251252
}
252253

253-
array_walk_recursive($data, function (&$value): void {
254+
array_walk_recursive($data, static function (&$value): void {
254255
$value = trim($value);
255256
});
256257

@@ -330,7 +331,7 @@ public static function valueExistsAll($check, array $sampleArr): bool
330331
// 以逗号分隔的会被拆开,组成数组
331332
if (is_string($check)) {
332333
$check = trim($check, ', ');
333-
$check = strpos($check, ',') !== false ? explode(',', $check) : [$check];
334+
$check = strpos($check, ',') !== false ? (array)explode(',', $check) : [$check];
334335
}
335336

336337
return !array_diff((array)$check, $sampleArr);
@@ -350,7 +351,7 @@ public static function valueExistsOne($check, array $sampleArr): bool
350351
// 以逗号分隔的会被拆开,组成数组
351352
if (is_string($check)) {
352353
$check = trim($check, ', ');
353-
$check = strpos($check, ',') !== false ? explode(',', $check) : [$check];
354+
$check = strpos($check, ',') !== false ? (array)explode(',', $check) : [$check];
354355
}
355356

356357
return (bool)array_intersect((array)$check, $sampleArr);
@@ -569,8 +570,8 @@ public static function collapse(array $array): array
569570
$results = [];
570571

571572
foreach ($array as $values) {
572-
if ($values instanceof CollectionInterface) {
573-
$values = $values->all();
573+
if (is_object($values) && method_exists($values, 'toArray')) {
574+
$values = $values->toArray();
574575
} elseif (!is_array($values)) {
575576
continue;
576577
}
@@ -591,9 +592,9 @@ public static function collapse(array $array): array
591592
*/
592593
public static function crossJoin(...$arrays): array
593594
{
594-
return array_reduce($arrays, function ($results, $array) {
595-
return static::collapse(array_map(function ($parent) use ($array) {
596-
return array_map(function ($item) use ($parent) {
595+
return array_reduce($arrays, static function ($results, $array) {
596+
return static::collapse(array_map(static function ($parent) use ($array) {
597+
return array_map(static function ($item) use ($parent) {
597598
return array_merge($parent, [$item]);
598599
}, $array);
599600
}, $results));
@@ -698,7 +699,7 @@ public static function add(array $array, $key, $value): array
698699
public static function get($array, $key, $default = null)
699700
{
700701
if (!static::accessible($array)) {
701-
return value($default);
702+
return Php::value($default);
702703
}
703704

704705
if (null === $key) {
@@ -713,7 +714,7 @@ public static function get($array, $key, $default = null)
713714
if (static::accessible($array) && static::exists($array, $segment)) {
714715
$array = $array[$segment];
715716
} else {
716-
return value($default);
717+
return Php::value($default);
717718
}
718719
}
719720

@@ -763,10 +764,12 @@ public static function set(array &$array, $key, $value): array
763764
*
764765
* @return array
765766
*/
766-
public static function flatten($array, $depth = INF): array
767+
public static function flatten($array, int $depth = INF): array
767768
{
768-
return array_reduce($array, function ($result, $item) use ($depth) {
769-
$item = $item instanceof CollectionInterface ? $item->all() : $item;
769+
return array_reduce($array, static function ($result, $item) use ($depth) {
770+
if (is_object($item) && method_exists($item, 'toArray')) {
771+
$item = $item->toArray();
772+
}
770773

771774
if (!is_array($item)) {
772775
return array_merge($result, [$item]);
@@ -1078,6 +1081,11 @@ public static function toFormatString($array, $length = 400)
10781081
return $string;
10791082
}
10801083

1084+
/**
1085+
* @param $array
1086+
*
1087+
* @return array
1088+
*/
10811089
public static function toLimitOut($array): array
10821090
{
10831091
if (!is_array($array)) {

src/Helper/PhpHelper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function array_sum;
1414
use function explode;
1515
use function is_array;
16+
use function is_callable;
1617
use function is_object;
1718
use function is_string;
1819
use function memory_get_peak_usage;
@@ -35,6 +36,20 @@
3536
*/
3637
class PhpHelper
3738
{
39+
/**
40+
* @param $value
41+
*
42+
* @return mixed
43+
*/
44+
public static function value($value)
45+
{
46+
if (is_callable($value)) {
47+
return $value();
48+
}
49+
50+
return $value;
51+
}
52+
3853
/**
3954
* get $_SERVER value
4055
*

src/Str/StringHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public static function substr(string $str, int $start, int $length = null, strin
410410
public static function utf8SubStr(string $str, int $start = 0, int $end = null): string
411411
{
412412
if (empty($str)) {
413-
return false;
413+
return '';
414414
}
415415

416416
if (function_exists('mb_substr')) {

0 commit comments

Comments
 (0)