Skip to content

Commit

Permalink
Update JSON serializer:
Browse files Browse the repository at this point in the history
- improve serializeArray()
  • Loading branch information
webeweb committed May 15, 2024
1 parent 15e650e commit f9a9ab9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Serializer/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ class JsonSerializer {
/**
* Serialize an array.
*
* @param array<JsonSerializable|null> $models The models.
* @param array<JsonSerializable|null>|null $models The models.
* @return mixed[] Returns the serialized array.
*/
public static function serializeArray(array $models): array {
public static function serializeArray(?array $models): array {

if (null === $models || 0 === count($models)) {
return [];
}

$output = [];

Expand Down
3 changes: 3 additions & 0 deletions tests/Serializer/JsonSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ public function testSerializeArray(): void {

$res = JsonSerializer::serializeArray($models);
$this->assertEquals([[], []], $res);

$this->assertEquals([], JsonSerializer::serializeArray(null));
$this->assertEquals([], JsonSerializer::serializeArray([]));
}
}

0 comments on commit f9a9ab9

Please sign in to comment.