Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Traversable should have priority over JsonSerializable in EnumerateValues #44456

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,12 +980,12 @@ protected function getArrayableItems($items)
return $items->all();
} elseif ($items instanceof Arrayable) {
return $items->toArray();
} elseif ($items instanceof Traversable) {
return iterator_to_array($items);
} elseif ($items instanceof Jsonable) {
return json_decode($items->toJson(), true);
} elseif ($items instanceof JsonSerializable) {
return (array) $items->jsonSerialize();
} elseif ($items instanceof Traversable) {
return iterator_to_array($items);
} elseif ($items instanceof UnitEnum) {
return [$items];
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
use Illuminate\Support\MultipleItemsFoundException;
use Illuminate\Support\Str;
use InvalidArgumentException;
use IteratorAggregate;
use JsonSerializable;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
use Symfony\Component\VarDumper\VarDumper;
use Traversable;
use UnexpectedValueException;

if (PHP_VERSION_ID >= 80100) {
Expand Down Expand Up @@ -622,6 +624,11 @@ public function testGetArrayableItems($collection)
$array = $method->invokeArgs($data, [$items]);
$this->assertSame(['foo'], $array);

$subject = [new stdClass, new stdClass];
$items = new TestTraversableAndJsonSerializableObject($subject);
$array = $method->invokeArgs($data, [$items]);
$this->assertSame($subject, $array);

$items = new $collection(['foo' => 'bar']);
$array = $method->invokeArgs($data, [$items]);
$this->assertSame(['foo' => 'bar'], $array);
Expand Down Expand Up @@ -5592,6 +5599,26 @@ public function jsonSerialize(): string
}
}

class TestTraversableAndJsonSerializableObject implements IteratorAggregate, JsonSerializable
{
public $items;

public function __construct($items)
{
$this->items = $items;
}

public function getIterator(): Traversable
{
return new ArrayIterator($this->items);
}

public function jsonSerialize(): array
{
return json_decode(json_encode($this->items), true);
}
}

class TestCollectionMapIntoObject
{
public $value;
Expand Down