Skip to content

Commit 6502e85

Browse files
Merge pull request #23 from cocur/json-serializable
Add JsonSerializable interface to Chain
2 parents 095b72b + c7a62e1 commit 6502e85

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/AbstractChain.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
use ArrayAccess;
66
use ArrayIterator;
77
use IteratorAggregate;
8+
use JsonSerializable;
89

910
/**
1011
* Chain.
1112
*
1213
* @author Florian Eckerstorfer
1314
* @copyright 2015 Florian Eckerstorfer
1415
*/
15-
abstract class AbstractChain implements ArrayAccess, IteratorAggregate
16+
abstract class AbstractChain implements ArrayAccess, IteratorAggregate, JsonSerializable
1617
{
1718
/**
1819
* @var array
@@ -63,4 +64,12 @@ public function offsetUnset($offset)
6364
{
6465
unset($this->array[$offset]);
6566
}
67+
68+
/**
69+
* @return array
70+
*/
71+
public function jsonSerialize()
72+
{
73+
return $this->array;
74+
}
6675
}

tests/ChainTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,16 @@ public function chainIsCountable()
138138
$this->assertInstanceOf('\Countable', $c);
139139
$this->assertEquals(3, count($c));
140140
}
141+
142+
/**
143+
* @test
144+
* @covers Cocur\Chain\Chain::jsonSerialize()
145+
*/
146+
public function chainIsJsonSerializable()
147+
{
148+
$c = Chain::create([0, 1, 2]);
149+
150+
$this->assertInstanceOf('\JsonSerializable', $c);
151+
$this->assertEquals('[0,1,2]', json_encode($c));
152+
}
141153
}

0 commit comments

Comments
 (0)