Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of ssh://git.zendframework.com:21652/zf
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Exception/InvalidCallbackException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
*/
namespace Zend\Stdlib\Exception;

use Zend\Stdlib\Exception,
DomainException;
use Zend\Stdlib\Exception;

/**
* Invalid callback exception
Expand Down
63 changes: 63 additions & 0 deletions src/IteratorToArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Zend\Stdlib;

use Traversable;

/**
* Convert an iterator to an array, recursively
*
* Declared abstract, as we have no need for instantiation.
*/
abstract class IteratorToArray
{
/**
* Convert an iterator to an array, recursively
*
* Converts an iterator to an array. The $recursive flag, on by default,
* hints whether or not you want to do so recursively.
*
* @param array|Traversable $iterator
* @return array
*/
public static function convert($iterator, $recursive = true)
{
if (!is_array($iterator) && !$iterator instanceof Traversable) {
throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
}

if (!$recursive) {
if (is_array($iterator)) {
return $iterator;
}

return iterator_to_array($iterator);
}

if (method_exists($iterator, 'toArray')) {
return $iterator->toArray();
}

$array = array();
foreach ($iterator as $key => $value) {
if (is_scalar($value)) {
$array[$key] = $value;
continue;
}

if ($value instanceof Traversable) {
$array[$key] = static::convert($value, $recursive);
continue;
}

if (is_array($value)) {
$array[$key] = static::convert($value, $recursive);
continue;
}

$array[$key] = $value;
}

return $array;
}
}
90 changes: 90 additions & 0 deletions test/IteratorToArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace ZendTest\Stdlib;

use ArrayObject,
PHPUnit_Framework_TestCase as TestCase,
stdClass,
Zend\Config\Config,
Zend\Stdlib\IteratorToArray;

class IteratorToArrayTest extends TestCase
{
public static function validIterators()
{
return array(
array(array(
'foo' => 'bar',
), array(
'foo' => 'bar',
)),
array(new Config(array(
'foo' => array(
'bar' => array(
'baz' => array(
'baz' => 'bat',
),
),
),
)), array(
'foo' => array(
'bar' => array(
'baz' => array(
'baz' => 'bat',
),
),
),
)),
array(new ArrayObject(array(
'foo' => array(
'bar' => array(
'baz' => array(
'baz' => 'bat',
),
),
),
)), array(
'foo' => array(
'bar' => array(
'baz' => array(
'baz' => 'bat',
),
),
),
)),
);
}

public static function invalidIterators()
{
return array(
array(null),
array(true),
array(false),
array(0),
array(1),
array(0.0),
array(1.0),
array('string'),
array(new stdClass),
);
}

/**
* @dataProvider validIterators
*/
public function testValidIteratorsReturnArrayRepresentation($test, $expected)
{
$result = IteratorToArray::convert($test);
$this->assertEquals($expected, $result);
}

/**
* @dataProvider invalidIterators
*/
public function testInvalidIteratorsRaiseInvalidArgumentException($test)
{
$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
$this->assertFalse(IteratorToArray::convert($test));
}
}

0 comments on commit 78945d0

Please sign in to comment.