1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-03-15 17:09:39 +01:00

Helper function to cast iterator of arrays to iterator of objects

This commit is contained in:
Filip Halaxa 2019-10-15 12:21:03 +02:00
parent c865e037ee
commit cbc45bdcff
3 changed files with 43 additions and 1 deletions

View File

@ -22,6 +22,7 @@
"guzzlehttp/guzzle": "^6"
},
"autoload" : {
"psr-4": {"JsonMachine\\": "src/"}
"psr-4": {"JsonMachine\\": "src/"},
"files": ["src/functions.php"]
}
}

14
src/functions.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace JsonMachine;
/**
* @param iterable $iterable
* @return \Generator
*/
function objects($iterable)
{
foreach ($iterable as $item) {
yield (object) $item;
}
}

View 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"]]],
];
}
}