This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using Traversable objects to define routes
- Added Zend\Stdlib\IteratorToArray class for recursive conversion of iterators to arrays - Updated factory() signatures, addRoutes() signatures, and routeFromArray() implementations to allow both arrays and Traversables; consues IteratorToArray for conversions when necessary - Changes will allow using Zend\Config objects directly in factories
- Loading branch information
38 parents
ceb7d8c
+
9e124d1
+
3de5912
+
b6a974a
+
10a6438
+
cb6c1e7
+
18afd6c
+
3baf1bd
+
c800904
+
f52dcb8
+
126ccb2
+
e7d6206
+
e2d24ab
+
ec1abfc
+
290ea90
+
9f4ca1b
+
edaa760
+
c4c0c95
+
d21f055
+
5b18029
+
e6b97af
+
010fb36
+
64c7b8d
+
636523e
+
4cc2cd6
+
e34098a
+
16367cd
+
943c77f
+
8226e5b
+
0b47726
+
3cd8a03
+
cc4782c
+
9c653a6
+
656dbe5
+
9bce1ba
+
7dc18ca
+
861130d
+
2d2ffbd
commit f0e5f4b
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |