Skip to content

Commit

Permalink
Merge pull request #78 from nguyenk/4.0
Browse files Browse the repository at this point in the history
fixing map iterator json_encode()
  • Loading branch information
moufmouf committed Mar 30, 2016
2 parents 383122d + fab522b commit 12ed494
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Mouf/Database/TDBM/MapIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* An iterator that maps element of another iterator by calling a callback on it.
*/
class MapIterator implements Iterator
class MapIterator implements Iterator, \JsonSerializable
{
/**
* @var Iterator
Expand Down Expand Up @@ -91,4 +91,9 @@ public function toArray()
{
return iterator_to_array($this);
}

public function jsonSerialize()
{
return $this->toArray();
}
}
10 changes: 10 additions & 0 deletions tests/Mouf/Database/TDBM/MapIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ public function testConstructorException2()
return $item;
});
}

public function testJsonSerialize()
{
$value = array(1, 2, 3);
$mapIterator = new MapIterator($value, function ($item) {
return $item;
});

$this->assertEquals($value, json_decode(json_encode($mapIterator), true));
}
}

0 comments on commit 12ed494

Please sign in to comment.