1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-16 20:48:17 +01:00

NestedIterator remnants deleted

This commit is contained in:
Filip Halaxa 2024-11-23 13:09:45 +01:00
parent f051ff57c0
commit da15ab2467
2 changed files with 0 additions and 141 deletions

View File

@ -1,94 +0,0 @@
<?php
declare(strict_types=1);
namespace JsonMachine;
use Iterator;
use JsonMachine\Exception\JsonMachineException;
class NestedIterator implements \RecursiveIterator
{
/** @var Iterator */
private $iterator;
public function __construct(Iterator $iterator)
{
$this->iterator = $iterator;
}
#[\ReturnTypeWillChange]
public function current()
{
return $this->iterator->current();
}
#[\ReturnTypeWillChange]
public function next()
{
$this->iterator->next();
}
#[\ReturnTypeWillChange]
public function key()
{
return $this->iterator->key();
}
#[\ReturnTypeWillChange]
public function valid()
{
return $this->iterator->valid();
}
#[\ReturnTypeWillChange]
public function rewind()
{
$this->iterator->rewind();
}
#[\ReturnTypeWillChange]
public function hasChildren()
{
return $this->iterator->current() instanceof Iterator;
}
#[\ReturnTypeWillChange]
public function getChildren()
{
return $this->hasChildren() ? new self($this->current()) : null;
}
public function advanceToKey($key)
{
$iterator = $this->iterator;
while ($key !== $iterator->key() && $iterator->valid()) {
$iterator->next();
}
if ($key !== $iterator->key()) {
throw new JsonMachineException("Key '$key' was not found.");
}
return $iterator->current();
}
public function toArray(): array
{
return self::toArrayRecursive($this);
}
private static function toArrayRecursive(\Traversable $traversable): array
{
$array = [];
foreach ($traversable as $key => $value) {
if ($value instanceof \Traversable) {
$value = self::toArrayRecursive($value);
}
$array[$key] = $value;
}
return $array;
}
}

View File

@ -1,47 +0,0 @@
<?php
declare(strict_types=1);
namespace JsonMachineTest;
use JsonMachine\NestedIterator;
use PHPUnit\Framework\TestCase;
use RecursiveIteratorIterator;
/**
* @covers \JsonMachine\NestedIterator
*/
class NestedIteratorTest extends TestCase
{
public function testIteratesOverPassedIterator()
{
$generator = function () {yield from [1, 2, 3]; };
$iterator = new NestedIterator($generator());
$result = iterator_to_array($iterator);
$this->assertSame([1, 2, 3], $result);
}
public function testHasChildrenIgnoresArrays()
{
$generator = function () {yield from [1, [2], 3]; };
$iterator = new NestedIterator($generator());
foreach ($iterator as $item) {
$this->assertFalse($iterator->hasChildren());
}
}
public function testGetChildrenReturnsCorrectItems()
{
$generator = function () {yield from [1, new \ArrayIterator([2]), 3]; };
$iterator = new RecursiveIteratorIterator(
new NestedIterator($generator())
);
$result = iterator_to_array($iterator, false);
$this->assertSame([1, 2, 3], $result);
}
}