mirror of
https://github.com/halaxa/json-machine.git
synced 2025-07-16 03:56:23 +02:00
Helper function to cast iterator of arrays to iterator of objects
This commit is contained in:
@ -22,6 +22,7 @@
|
|||||||
"guzzlehttp/guzzle": "^6"
|
"guzzlehttp/guzzle": "^6"
|
||||||
},
|
},
|
||||||
"autoload" : {
|
"autoload" : {
|
||||||
"psr-4": {"JsonMachine\\": "src/"}
|
"psr-4": {"JsonMachine\\": "src/"},
|
||||||
|
"files": ["src/functions.php"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
src/functions.php
Normal file
14
src/functions.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace JsonMachine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param iterable $iterable
|
||||||
|
* @return \Generator
|
||||||
|
*/
|
||||||
|
function objects($iterable)
|
||||||
|
{
|
||||||
|
foreach ($iterable as $item) {
|
||||||
|
yield (object) $item;
|
||||||
|
}
|
||||||
|
}
|
27
test/JsonMachineTest/FunctionsTest.php
Normal file
27
test/JsonMachineTest/FunctionsTest.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace JsonMachineTest;
|
||||||
|
|
||||||
|
use function JsonMachine\objects;
|
||||||
|
|
||||||
|
class FunctionsTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @dataProvider dataObjectsOnEmptyInput
|
||||||
|
* @param $expected
|
||||||
|
* @param $data
|
||||||
|
*/
|
||||||
|
public function testObjectsOnEmptyInput($expected, $data)
|
||||||
|
{
|
||||||
|
$this->assertEquals($expected, iterator_to_array(objects($data)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataObjectsOnEmptyInput()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[[], []],
|
||||||
|
[[new \stdClass()], [[]]],
|
||||||
|
[[(object)["one" => "two"]], [["one" => "two"]]],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user