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

Use is_iterable instead of is_array #991

Merged
merged 1 commit into from
Mar 24, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Enforce a timeout for connecting to the server and for the requests instead of waiting indefinitely (#979)
- Add `RequestFetcherInterface` to allow customizing the request data attached to the logged event (#984)
- Log internal debug and error messages to a PSR-3 compatible logger (#989)
- Make `AbstractSerializer` to accept `Traversable` values using `is_iterable` instead of `is_array` (#991)

## 2.3.2 (2020-03-06)

Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function serializeRecursively($value, int $_depth = 0)
return $this->serializeCallableWithoutTypeHint($value);
}

if (\is_array($value)) {
if (is_iterable($value)) {
$serializedArray = [];

foreach ($value as $k => $v) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Serializer/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ public function testArraysAreArrays(bool $serializeAllObjects): void
$this->assertSame([1, 2, 3], $result);
}

/**
* @dataProvider serializeAllObjectsDataProvider
*/
public function testTraversablesAreArrays(bool $serializeAllObjects): void
{
$serializer = $this->createSerializer();

if ($serializeAllObjects) {
$serializer->setSerializeAllObjects(true);
}

$content = [1, 2, 3];
$traversable = new \ArrayIterator($content);
$result = $this->invokeSerialization($serializer, $traversable);

$this->assertSame([1, 2, 3], $result);
}

/**
* @dataProvider serializeAllObjectsDataProvider
*/
Expand Down