Skip to content

Commit

Permalink
Merge pull request #29205 from staudenmeir/collection
Browse files Browse the repository at this point in the history
[5.8] Fix collections with JsonSerializable items and mixed values
  • Loading branch information
taylorotwell authored Jul 18, 2019
2 parents 99522fc + 0fbd751 commit 7e9a7c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,7 @@ protected function getArrayableItems($items)
} elseif ($items instanceof Jsonable) {
return json_decode($items->toJson(), true);
} elseif ($items instanceof JsonSerializable) {
return $items->jsonSerialize();
return (array) $items->jsonSerialize();
} elseif ($items instanceof Traversable) {
return iterator_to_array($items);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ public function testGetArrayableItems()
$array = $method->invokeArgs($collection, [$items]);
$this->assertSame(['foo' => 'bar'], $array);

$items = new TestJsonSerializeWithScalarValueObject;
$array = $method->invokeArgs($collection, [$items]);
$this->assertSame(['foo'], $array);

$items = new Collection(['foo' => 'bar']);
$array = $method->invokeArgs($collection, [$items]);
$this->assertSame(['foo' => 'bar'], $array);
Expand Down Expand Up @@ -3297,6 +3301,14 @@ public function jsonSerialize()
}
}

class TestJsonSerializeWithScalarValueObject implements JsonSerializable
{
public function jsonSerialize()
{
return 'foo';
}
}

class TestCollectionMapIntoObject
{
public $value;
Expand Down

0 comments on commit 7e9a7c6

Please sign in to comment.