From 252d9454c8a80fff6b17bf8fbdad3837ddad134f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Mon, 21 Mar 2022 16:42:12 +0500 Subject: [PATCH] json serialization for default collection --- src/ObjectCollection.php | 13 ++++++++++++- tests/ObjectCollectionTest.php | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/ObjectCollection.php b/src/ObjectCollection.php index cb9adbf..15be386 100644 --- a/src/ObjectCollection.php +++ b/src/ObjectCollection.php @@ -15,6 +15,7 @@ * Import classes */ use InvalidArgumentException; +use JsonSerializable; use RuntimeException; /** @@ -25,7 +26,7 @@ /** * ObjectCollection */ -abstract class ObjectCollection implements ObjectCollectionInterface +abstract class ObjectCollection implements ObjectCollectionInterface, JsonSerializable { /** @@ -101,4 +102,14 @@ final public function isEmpty() : bool { return [] === $this->objects; } + + /** + * {@inheritdoc} + * + * @since 2.3.0 + */ + public function jsonSerialize() : array + { + return $this->objects; + } } diff --git a/tests/ObjectCollectionTest.php b/tests/ObjectCollectionTest.php index 505d217..b834bce 100644 --- a/tests/ObjectCollectionTest.php +++ b/tests/ObjectCollectionTest.php @@ -8,6 +8,7 @@ use Sunrise\Hydrator\ObjectCollection; use Sunrise\Hydrator\ObjectCollectionInterface; use InvalidArgumentException; +use JsonSerializable; use RuntimeException; class ObjectCollectionTest extends TestCase @@ -17,6 +18,7 @@ public function testContracts() : void $collection = new Fixtures\BarCollection(); $this->assertInstanceOf(ObjectCollectionInterface::class, $collection); + $this->assertInstanceOf(JsonSerializable::class, $collection); } public function testAdd() : void @@ -61,4 +63,11 @@ public function testUnexpectedObject() : void $collection->add(0, new Fixtures\Foo()); } + + public function testJsonSerialize() : void + { + $collection = new Fixtures\BarCollection(); + + $this->assertSame($collection->all(), $collection->jsonSerialize()); + } }