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

Commit 78945d0

Browse files
author
Ralph Schindler
committed
Merge branch 'master' of ssh://git.zendframework.com:21652/zf

File tree

3 files changed

+154
-2
lines changed

3 files changed

+154
-2
lines changed

src/Exception/InvalidCallbackException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
*/
2424
namespace Zend\Stdlib\Exception;
2525

26-
use Zend\Stdlib\Exception,
27-
DomainException;
26+
use Zend\Stdlib\Exception;
2827

2928
/**
3029
* Invalid callback exception

src/IteratorToArray.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Zend\Stdlib;
4+
5+
use Traversable;
6+
7+
/**
8+
* Convert an iterator to an array, recursively
9+
*
10+
* Declared abstract, as we have no need for instantiation.
11+
*/
12+
abstract class IteratorToArray
13+
{
14+
/**
15+
* Convert an iterator to an array, recursively
16+
*
17+
* Converts an iterator to an array. The $recursive flag, on by default,
18+
* hints whether or not you want to do so recursively.
19+
*
20+
* @param array|Traversable $iterator
21+
* @return array
22+
*/
23+
public static function convert($iterator, $recursive = true)
24+
{
25+
if (!is_array($iterator) && !$iterator instanceof Traversable) {
26+
throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
27+
}
28+
29+
if (!$recursive) {
30+
if (is_array($iterator)) {
31+
return $iterator;
32+
}
33+
34+
return iterator_to_array($iterator);
35+
}
36+
37+
if (method_exists($iterator, 'toArray')) {
38+
return $iterator->toArray();
39+
}
40+
41+
$array = array();
42+
foreach ($iterator as $key => $value) {
43+
if (is_scalar($value)) {
44+
$array[$key] = $value;
45+
continue;
46+
}
47+
48+
if ($value instanceof Traversable) {
49+
$array[$key] = static::convert($value, $recursive);
50+
continue;
51+
}
52+
53+
if (is_array($value)) {
54+
$array[$key] = static::convert($value, $recursive);
55+
continue;
56+
}
57+
58+
$array[$key] = $value;
59+
}
60+
61+
return $array;
62+
}
63+
}

test/IteratorToArrayTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace ZendTest\Stdlib;
4+
5+
use ArrayObject,
6+
PHPUnit_Framework_TestCase as TestCase,
7+
stdClass,
8+
Zend\Config\Config,
9+
Zend\Stdlib\IteratorToArray;
10+
11+
class IteratorToArrayTest extends TestCase
12+
{
13+
public static function validIterators()
14+
{
15+
return array(
16+
array(array(
17+
'foo' => 'bar',
18+
), array(
19+
'foo' => 'bar',
20+
)),
21+
array(new Config(array(
22+
'foo' => array(
23+
'bar' => array(
24+
'baz' => array(
25+
'baz' => 'bat',
26+
),
27+
),
28+
),
29+
)), array(
30+
'foo' => array(
31+
'bar' => array(
32+
'baz' => array(
33+
'baz' => 'bat',
34+
),
35+
),
36+
),
37+
)),
38+
array(new ArrayObject(array(
39+
'foo' => array(
40+
'bar' => array(
41+
'baz' => array(
42+
'baz' => 'bat',
43+
),
44+
),
45+
),
46+
)), array(
47+
'foo' => array(
48+
'bar' => array(
49+
'baz' => array(
50+
'baz' => 'bat',
51+
),
52+
),
53+
),
54+
)),
55+
);
56+
}
57+
58+
public static function invalidIterators()
59+
{
60+
return array(
61+
array(null),
62+
array(true),
63+
array(false),
64+
array(0),
65+
array(1),
66+
array(0.0),
67+
array(1.0),
68+
array('string'),
69+
array(new stdClass),
70+
);
71+
}
72+
73+
/**
74+
* @dataProvider validIterators
75+
*/
76+
public function testValidIteratorsReturnArrayRepresentation($test, $expected)
77+
{
78+
$result = IteratorToArray::convert($test);
79+
$this->assertEquals($expected, $result);
80+
}
81+
82+
/**
83+
* @dataProvider invalidIterators
84+
*/
85+
public function testInvalidIteratorsRaiseInvalidArgumentException($test)
86+
{
87+
$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
88+
$this->assertFalse(IteratorToArray::convert($test));
89+
}
90+
}

0 commit comments

Comments
 (0)