Skip to content

Commit 45d37ff

Browse files
committed
Arrays: some methods accepts iterable
1 parent 0e371f6 commit 45d37ff

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

src/Utils/Arrays.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public static function pick(array &$array, $key, $default = null)
280280
* Tests whether at least one element in the array passes the test implemented by the
281281
* provided callback with signature `function ($value, $key, array $array): bool`.
282282
*/
283-
public static function some(array $array, callable $callback): bool
283+
public static function some(iterable $array, callable $callback): bool
284284
{
285285
foreach ($array as $k => $v) {
286286
if ($callback($v, $k, $array)) {
@@ -295,7 +295,7 @@ public static function some(array $array, callable $callback): bool
295295
* Tests whether all elements in the array pass the test implemented by the provided function,
296296
* which has the signature `function ($value, $key, array $array): bool`.
297297
*/
298-
public static function every(array $array, callable $callback): bool
298+
public static function every(iterable $array, callable $callback): bool
299299
{
300300
foreach ($array as $k => $v) {
301301
if (!$callback($v, $k, $array)) {
@@ -310,7 +310,7 @@ public static function every(array $array, callable $callback): bool
310310
* Calls $callback on all elements in the array and returns the array of return values.
311311
* The callback has the signature `function ($value, $key, array $array): bool`.
312312
*/
313-
public static function map(array $array, callable $callback): array
313+
public static function map(iterable $array, callable $callback): array
314314
{
315315
$res = [];
316316
foreach ($array as $k => $v) {
@@ -325,7 +325,7 @@ public static function map(array $array, callable $callback): array
325325
* @param object $object
326326
* @return object
327327
*/
328-
public static function toObject(array $array, $object)
328+
public static function toObject(iterable $array, $object)
329329
{
330330
foreach ($array as $k => $v) {
331331
$object->$k = $v;

tests/Utils/Arrays.every().phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,14 @@ test('', function () {
7878
Assert::true($res);
7979
Assert::same([['a', 'x', $arr], ['b', 'y', $arr]], $log);
8080
});
81+
82+
test('', function () {
83+
$arr = new ArrayIterator(['x' => 'a', 'y' => 'b']);
84+
$log = [];
85+
$res = Arrays::every(
86+
$arr,
87+
function ($v, $k, $arr) use (&$log) { $log[] = func_get_args(); return true; }
88+
);
89+
Assert::true($res);
90+
Assert::same([['a', 'x', $arr], ['b', 'y', $arr]], $log);
91+
});

tests/Utils/Arrays.map().phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,14 @@ test('', function () {
4545
Assert::same(['x' => 'aa', 'y' => 'bb'], $res);
4646
Assert::same([['a', 'x', $arr], ['b', 'y', $arr]], $log);
4747
});
48+
49+
test('', function () {
50+
$arr = new ArrayIterator(['x' => 'a', 'y' => 'b']);
51+
$log = [];
52+
$res = Arrays::map(
53+
$arr,
54+
function ($v, $k, $arr) use (&$log) { $log[] = func_get_args(); return $v . $v; }
55+
);
56+
Assert::same(['x' => 'aa', 'y' => 'bb'], $res);
57+
Assert::same([['a', 'x', $arr], ['b', 'y', $arr]], $log);
58+
});

tests/Utils/Arrays.some().phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,14 @@ test('', function () {
7878
Assert::true($res);
7979
Assert::same([['a', 'x', $arr]], $log);
8080
});
81+
82+
test('', function () {
83+
$arr = new ArrayIterator(['x' => 'a', 'y' => 'b']);
84+
$log = [];
85+
$res = Arrays::some(
86+
$arr,
87+
function ($v, $k, $arr) use (&$log) { $log[] = func_get_args(); return $v === 'a'; }
88+
);
89+
Assert::true($res);
90+
Assert::same([['a', 'x', $arr]], $log);
91+
});

tests/Utils/Arrays.toObject.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ test('', function () {
2828
Assert::type(stdClass::class, $res);
2929
Assert::same(['a' => 1, 'b' => 2], (array) $res);
3030
});
31+
32+
test('', function () {
33+
$obj = new stdClass;
34+
$res = Arrays::toObject(new ArrayIterator(['a' => 1, 'b' => 2]), $obj);
35+
Assert::same($res, $obj);
36+
Assert::type(stdClass::class, $res);
37+
Assert::same(['a' => 1, 'b' => 2], (array) $res);
38+
});

0 commit comments

Comments
 (0)