Skip to content

Commit

Permalink
return toArray value only for traversable which are not iterator
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Gitton <[email protected]>
  • Loading branch information
garygitton authored and Ocramius committed Dec 24, 2021
1 parent 6422838 commit 2d3358f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Laminas\Stdlib;

use Iterator;
use Laminas\Stdlib\ArrayUtils\MergeRemoveKey;
use Laminas\Stdlib\ArrayUtils\MergeReplaceKeyInterface;
use Traversable;
Expand Down Expand Up @@ -240,7 +241,11 @@ public static function iteratorToArray($iterator, $recursive = true)
return iterator_to_array($iterator);
}

if (is_object($iterator) && method_exists($iterator, 'toArray')) {
if (
is_object($iterator)
&& ! $iterator instanceof Iterator

This comment has been minimized.

Copy link
@freemiumdev

freemiumdev Feb 23, 2022

Hi,
in my projext i've a JsonModel whith an entry of type HydratingResultSet. When JSON serialize for response the array is empty. before this update i've the json data filled from HydratingResultSet.
can you help me can i solve?
thans

This comment has been minimized.

Copy link
@Ocramius

Ocramius Feb 23, 2022

Member

@freemiumdev please ask such kind of questions on the forums or on slack.

This comment has been minimized.

Copy link
@freemiumdev

freemiumdev Feb 23, 2022

I think it's a bug introduced with this commit. do I still have to ask on the forum?

This comment has been minimized.

Copy link
@Ocramius

Ocramius Feb 23, 2022

Member

Either that, or providing an integration test that highlights the issue (as a PR)

&& method_exists($iterator, 'toArray')
) {
return $iterator->toArray();
}

Expand Down
27 changes: 27 additions & 0 deletions test/ArrayUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Laminas\Stdlib\ArrayUtils\MergeReplaceKeyInterface;
use Laminas\Stdlib\Exception\InvalidArgumentException;
use Laminas\Stdlib\Parameters;
use LaminasTest\Stdlib\TestAsset\IteratorWithToArrayMethod;
use PHPUnit\Framework\TestCase;
use stdClass;
use Traversable;
Expand Down Expand Up @@ -579,4 +580,30 @@ public function testInvalidCallableRaiseInvalidArgumentException(): void
$this->expectException(InvalidArgumentException::class);
ArrayUtils::filter([], "INVALID");
}

/**
* @link https://github.com/laminas/laminas-stdlib/issues/18
*/
public function testIteratorToArrayWithIteratorHavingMethodToArrayAndRecursiveIsFalse()
{
$arrayB = [
'foo' => 'bar',
];
$iteratorB = new IteratorWithToArrayMethod($arrayB);

$arrayA = [
'iteratorB' => $iteratorB,
];
$iterator = new IteratorWithToArrayMethod($arrayA);

$result = ArrayUtils::iteratorToArray($iterator, true);

$expectedResult = [
'iteratorB' => [
'foo' => 'bar',
],
];

$this->assertEquals($expectedResult, $result);
}
}
69 changes: 69 additions & 0 deletions test/TestAsset/IteratorWithToArrayMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Stdlib\TestAsset;

use Iterator;
use ReturnTypeWillChange;

use function current;
use function is_array;
use function key;
use function next;
use function reset;

class IteratorWithToArrayMethod implements Iterator
{
private array $elements = [];

public function __construct(array $elements)
{
if (is_array($elements)) {
$this->elements = $elements;
}
}

/** @return void */
#[ReturnTypeWillChange]
public function rewind()
{
reset($this->elements);
}

/** @return mixed */
#[ReturnTypeWillChange]
public function current()
{
return current($this->elements);
}

/** @return int|string */
#[ReturnTypeWillChange]
public function key()
{
return key($this->elements);
}

/** @return mixed */
#[ReturnTypeWillChange]
public function next()
{
return next($this->elements);
}

/** @return bool */
#[ReturnTypeWillChange]
public function valid()
{
$key = key($this->elements);
return $key !== null && $key !== false;
}

public function toArray(): array
{
return [
'data from to array' => 'not good',
];
}
}

0 comments on commit 2d3358f

Please sign in to comment.