From c7a62e103c471ad06430443ff6bc70c202274a25 Mon Sep 17 00:00:00 2001 From: Florian Eckerstorfer Date: Thu, 5 Apr 2018 14:38:12 +0200 Subject: [PATCH] Add JsonSerializable interface to Chain Fixes #22 --- src/AbstractChain.php | 11 ++++++++++- tests/ChainTest.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/AbstractChain.php b/src/AbstractChain.php index 87123be..f68b4bf 100644 --- a/src/AbstractChain.php +++ b/src/AbstractChain.php @@ -5,6 +5,7 @@ use ArrayAccess; use ArrayIterator; use IteratorAggregate; +use JsonSerializable; /** * Chain. @@ -12,7 +13,7 @@ * @author Florian Eckerstorfer * @copyright 2015 Florian Eckerstorfer */ -abstract class AbstractChain implements ArrayAccess, IteratorAggregate +abstract class AbstractChain implements ArrayAccess, IteratorAggregate, JsonSerializable { /** * @var array @@ -63,4 +64,12 @@ public function offsetUnset($offset) { unset($this->array[$offset]); } + + /** + * @return array + */ + public function jsonSerialize() + { + return $this->array; + } } diff --git a/tests/ChainTest.php b/tests/ChainTest.php index f0d77cc..817ca12 100644 --- a/tests/ChainTest.php +++ b/tests/ChainTest.php @@ -138,4 +138,16 @@ public function chainIsCountable() $this->assertInstanceOf('\Countable', $c); $this->assertEquals(3, count($c)); } + + /** + * @test + * @covers Cocur\Chain\Chain::jsonSerialize() + */ + public function chainIsJsonSerializable() + { + $c = Chain::create([0, 1, 2]); + + $this->assertInstanceOf('\JsonSerializable', $c); + $this->assertEquals('[0,1,2]', json_encode($c)); + } }