Skip to content

Commit b5f1cf5

Browse files
committed
New function iterable_to_traversable()
1 parent a48bd24 commit b5f1cf5

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Iterable functions
1010

1111
Provides additional functions to work with [iterable](https://wiki.php.net/rfc/iterable) variables (even on PHP5.3+).
1212

13-
is_iterable
14-
-----------
13+
is_iterable()
14+
-------------
1515
To check wether or not a PHP variable can be looped over in a `foreach` statement, PHP provides an `is_iterable()` function.
1616

1717
**But this function only works on PHP7.1+**.
@@ -25,8 +25,8 @@ var_dump(is_iterable(new DirectoryIterator(__DIR__))); // true
2525
var_dump(is_iterable('foobar')); // false
2626
```
2727

28-
iterable_to_array
29-
-----------------
28+
iterable_to_array()
29+
-------------------
3030

3131
PHP offers an `iterator_to_array()` function to export any iterator into an array.
3232

@@ -41,6 +41,20 @@ var_dump(iterable_to_array(new ArrayIterator(array('foo', 'bar')))); // ['foo',
4141
var_dump(iterable_to_array(array('foo', 'bar'))); // ['foo', 'bar']
4242
```
4343

44+
iterable_to_traversable()
45+
-------------------------
46+
Useful when you have a `Traversable` type-hint, and you don't know wether or not your argument will be an array or an iterator.
47+
48+
If your variable is already an instance of `Traversable` (i.e. an `Iterator`, an `IteratorAggregate` or a `Generator`), the function simply returns it directly.
49+
50+
If your variable is an array, the function converts it to an `ArrayIterator`.
51+
52+
Usage:
53+
```php
54+
var_dump(iterable_to_traversable(array('foo', 'bar'))); // ArrayIterator(array('foo', 'bar'))
55+
var_dump(iterable_to_traversable(new ArrayIterator(array('foo', 'bar')))); // ArrayIterator(array('foo', 'bar'))
56+
```
57+
4458
Installation
4559
============
4660

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<testsuite name="Test Suite">
2020
<file>tests/TestIsIterable.php</file>
2121
<file>tests/TestIterableToArray.php</file>
22+
<file>tests/TestIterableToTraversable.php</file>
2223
</testsuite>
2324
</testsuites>
2425
<filter>

src/iterable-functions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,18 @@ function iterable_to_array($iterable)
2525
return is_array($iterable) ? $iterable : iterator_to_array($iterable);
2626
}
2727
}
28+
29+
if (!function_exists('iterable_to_traversable')) {
30+
function iterable_to_traversable($iterable)
31+
{
32+
if ($iterable instanceof Traversable) {
33+
return $iterable;
34+
}
35+
elseif (is_array($iterable)) {
36+
return new ArrayIterator($iterable);
37+
}
38+
else {
39+
throw new \InvalidArgumentException(sprintf('Expected array or \\Traversable, got %s', is_object($iterable) ? get_class($iterable) : gettype($iterable)));
40+
}
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class TestIterableToTraversable extends TestCase
6+
{
7+
8+
public function testFunctionExists()
9+
{
10+
$this->assertTrue(function_exists('iterable_to_traversable'));
11+
}
12+
13+
public function testIteratorToTraversable()
14+
{
15+
$iterator = new ArrayIterator(array('foo' => 'bar'));
16+
$traversable = iterable_to_traversable($iterator);
17+
$this->assertSame($iterator, $traversable);
18+
$this->assertInstanceOf('Traversable', $iterator);
19+
}
20+
21+
public function testArrayToTraversable()
22+
{
23+
$array = array('foo' => 'bar');
24+
$traversable = iterable_to_traversable($array);
25+
$this->assertEquals(new ArrayIterator(array('foo' => 'bar')), $traversable);
26+
$this->assertInstanceOf('Traversable', $traversable);
27+
}
28+
29+
/**
30+
* @expectedException InvalidArgumentException
31+
*/
32+
public function testInvalidArgument()
33+
{
34+
$string = 'foo';
35+
iterable_to_traversable($string);
36+
var_dump(iterable_to_traversable(array('foo', 'bar')));
37+
}
38+
}

0 commit comments

Comments
 (0)