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:
parent
c865e037ee
commit
cbc45bdcff
@ -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
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"]]],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user