Skip to content

Commit a9e219b

Browse files
committed
Implement JsonSerializable in lazy BSON classes
1 parent e9ec904 commit a9e219b

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/Model/LazyBSONArray.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use CallbackFilterIterator;
2424
use Countable;
2525
use IteratorAggregate;
26+
use JsonSerializable;
2627
use MongoDB\BSON\PackedArray;
2728
use MongoDB\Codec\CodecLibrary;
2829
use MongoDB\Codec\LazyBSONCodecLibrary;
@@ -37,6 +38,7 @@
3738
use function count;
3839
use function is_array;
3940
use function is_numeric;
41+
use function iterator_to_array;
4042
use function max;
4143
use function MongoDB\recursive_copy;
4244
use function sprintf;
@@ -54,7 +56,7 @@
5456
* @template-implements ArrayAccess<int, TValue>
5557
* @template-implements IteratorAggregate<int, TValue>
5658
*/
57-
final class LazyBSONArray implements ArrayAccess, Countable, IteratorAggregate
59+
final class LazyBSONArray implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
5860
{
5961
/** @var PackedArray<TValue> */
6062
private PackedArray $bson;
@@ -184,6 +186,11 @@ function ($value, int $offset) use (&$seen) {
184186
);
185187
}
186188

189+
public function jsonSerialize(): array
190+
{
191+
return iterator_to_array($this->getIterator());
192+
}
193+
187194
/** @param mixed $offset */
188195
public function offsetExists($offset): bool
189196
{

src/Model/LazyBSONDocument.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Countable;
2525
use Iterator;
2626
use IteratorAggregate;
27+
use JsonSerializable;
2728
use MongoDB\BSON\Document;
2829
use MongoDB\Codec\CodecLibrary;
2930
use MongoDB\Codec\LazyBSONCodecLibrary;
@@ -38,6 +39,7 @@
3839
use function is_array;
3940
use function is_object;
4041
use function is_string;
42+
use function iterator_to_array;
4143
use function MongoDB\recursive_copy;
4244
use function sprintf;
4345
use function trigger_error;
@@ -54,7 +56,7 @@
5456
* @template-implements ArrayAccess<string, TValue>
5557
* @template-implements IteratorAggregate<string, TValue>
5658
*/
57-
final class LazyBSONDocument implements ArrayAccess, Countable, IteratorAggregate
59+
final class LazyBSONDocument implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
5860
{
5961
/** @var Document<TValue> */
6062
private Document $bson;
@@ -218,6 +220,11 @@ function ($value, string $key) use (&$seen) {
218220
);
219221
}
220222

223+
public function jsonSerialize(): array
224+
{
225+
return iterator_to_array($this->getIterator());
226+
}
227+
221228
/** @param mixed $offset */
222229
public function offsetExists($offset): bool
223230
{

tests/Model/LazyBSONArrayTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use stdClass;
1212

1313
use function iterator_to_array;
14+
use function json_encode;
1415
use function serialize;
1516
use function unserialize;
1617

@@ -273,4 +274,14 @@ public function testSerialization(): void
273274

274275
$this->assertEquals(['foobar', 'baz', 'yay!'], iterator_to_array($unserialized));
275276
}
277+
278+
public function testJsonSerialize(): void
279+
{
280+
$array = new LazyBSONArray(PackedArray::fromPHP(['foo', 'bar', 'baz']));
281+
$array[0] = 'foobar';
282+
$array[3] = 'yay!';
283+
unset($array[1]);
284+
285+
$this->assertJsonStringEqualsJsonString('["foobar","baz","yay!"]', json_encode($array));
286+
}
276287
}

tests/Model/LazyBSONDocumentTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use stdClass;
1212

1313
use function iterator_to_array;
14+
use function json_encode;
1415
use function serialize;
1516
use function unserialize;
1617

@@ -323,4 +324,14 @@ public function testSerialization(): void
323324

324325
$this->assertEquals(['foo' => 'foobar', 'baz' => 'yay!'], iterator_to_array($unserialized));
325326
}
327+
328+
public function testJsonSerialize(): void
329+
{
330+
$document = new LazyBSONDocument(Document::fromPHP(['foo' => 'bar', 'bar' => 'baz']));
331+
$document['foo'] = 'foobar';
332+
$document['baz'] = 'yay!';
333+
unset($document['bar']);
334+
335+
$this->assertJsonStringEqualsJsonString('{"foo":"foobar","baz":"yay!"}', json_encode($document));
336+
}
326337
}

0 commit comments

Comments
 (0)