Skip to content

Commit

Permalink
Merge pull request #23 from cocur/json-serializable
Browse files Browse the repository at this point in the history
Add JsonSerializable interface to Chain
  • Loading branch information
florianeckerstorfer authored Apr 5, 2018
2 parents 095b72b + c7a62e1 commit 6502e85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/AbstractChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use ArrayAccess;
use ArrayIterator;
use IteratorAggregate;
use JsonSerializable;

/**
* Chain.
*
* @author Florian Eckerstorfer
* @copyright 2015 Florian Eckerstorfer
*/
abstract class AbstractChain implements ArrayAccess, IteratorAggregate
abstract class AbstractChain implements ArrayAccess, IteratorAggregate, JsonSerializable
{
/**
* @var array
Expand Down Expand Up @@ -63,4 +64,12 @@ public function offsetUnset($offset)
{
unset($this->array[$offset]);
}

/**
* @return array
*/
public function jsonSerialize()
{
return $this->array;
}
}
12 changes: 12 additions & 0 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 6502e85

Please sign in to comment.