Skip to content

Commit 4eeac12

Browse files
committed
Add ArrayHelper::iterableToArray()
1 parent f846c6e commit 4eeac12

File tree

6 files changed

+51
-5
lines changed

6 files changed

+51
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Enh #168: Add benchmark, improve `ArrayHelper::htmlEncode()` performance (@samdark)
66
- Enh #169: Bump `yiisoft/strings` dependency to `^2.6` (@vjik)
7+
- New #170: Add `ArrayHelper::iterableToArray()` method (@vjik)
78

89
## 3.2.0 February 01, 2025
910

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Overall the helper has the following method groups.
8282
### Transformation
8383

8484
- index
85+
- iterableToArray
8586
- group
8687
- filter
8788
- map

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
"require-dev": {
3535
"bamarni/composer-bin-plugin": "^1.8.2",
3636
"phpunit/phpunit": "^10.5.45",
37-
"rector/rector": "^2.0.9",
38-
"roave/infection-static-analysis-plugin": "^1.35",
37+
"rector/rector": "^2.0.11",
38+
"roave/infection-static-analysis-plugin": "^1.37",
3939
"spatie/phpunit-watcher": "^1.24",
40-
"vimeo/psalm": "^5.26.1 || ^6.5.1",
41-
"phpbench/phpbench": "^1.4"
40+
"vimeo/psalm": "^5.26.1 || ^6.10",
41+
"phpbench/phpbench": "^1.4.1"
4242
},
4343
"autoload": {
4444
"psr-4": {

src/ArrayHelper.php

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Closure;
88
use InvalidArgumentException;
99
use Throwable;
10+
use Traversable;
1011
use Yiisoft\Strings\NumericHelper;
1112
use Yiisoft\Strings\StringHelper;
1213

@@ -1414,6 +1415,18 @@ public static function renameKey(array $array, int|string $from, int|string $to)
14141415
return array_combine($keys, $array);
14151416
}
14161417

1418+
/**
1419+
* Converts an iterable value to an array.
1420+
*
1421+
* @param iterable $iterable The iterable to convert.
1422+
*
1423+
* @return array The converted array.
1424+
*/
1425+
public static function iterableToArray(iterable $iterable): array
1426+
{
1427+
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable;
1428+
}
1429+
14171430
/**
14181431
* @param array[] $arrays
14191432
*/
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Arrays\Tests\ArrayHelper;
6+
7+
use ArrayIterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use Yiisoft\Arrays\ArrayHelper;
11+
12+
use function PHPUnit\Framework\assertSame;
13+
14+
final class IterableToArrayTest extends TestCase
15+
{
16+
public static function dataBase(): iterable
17+
{
18+
yield [[1, 2, 3], [1, 2, 3]];
19+
yield [[1, 2, 3], new ArrayIterator([1, 2, 3])];
20+
yield [['key' => 'value'], ['key' => 'value']];
21+
yield [['key' => 'value'], new ArrayIterator(['key' => 'value'])];
22+
}
23+
24+
#[DataProvider('dataBase')]
25+
public function testBase(array $expected, iterable $value): void
26+
{
27+
$result = ArrayHelper::iterableToArray($value);
28+
29+
assertSame($expected, $result);
30+
}
31+
}

tools/composer-require-checker/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require-dev": {
3-
"maglnet/composer-require-checker": "^4.7.1"
3+
"maglnet/composer-require-checker": "^4.16.1"
44
},
55
"config": {
66
"bump-after-update": "dev"

0 commit comments

Comments
 (0)