From f9a9ab9019c4fa1e4e336cc169718bb2f35f1d66 Mon Sep 17 00:00:00 2001 From: webeweb Date: Wed, 15 May 2024 15:27:24 +0200 Subject: [PATCH] Update JSON serializer: - improve serializeArray() --- src/Serializer/JsonSerializer.php | 8 ++++++-- tests/Serializer/JsonSerializerTest.php | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Serializer/JsonSerializer.php b/src/Serializer/JsonSerializer.php index 41bd03573..d0d8c1617 100644 --- a/src/Serializer/JsonSerializer.php +++ b/src/Serializer/JsonSerializer.php @@ -26,10 +26,14 @@ class JsonSerializer { /** * Serialize an array. * - * @param array $models The models. + * @param array|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 = []; diff --git a/tests/Serializer/JsonSerializerTest.php b/tests/Serializer/JsonSerializerTest.php index d50c55401..f0dce24ba 100644 --- a/tests/Serializer/JsonSerializerTest.php +++ b/tests/Serializer/JsonSerializerTest.php @@ -40,5 +40,8 @@ public function testSerializeArray(): void { $res = JsonSerializer::serializeArray($models); $this->assertEquals([[], []], $res); + + $this->assertEquals([], JsonSerializer::serializeArray(null)); + $this->assertEquals([], JsonSerializer::serializeArray([])); } }