Skip to content

Commit d741a6e

Browse files
committed
Implement JsonSerializable in lazy BSON classes
1 parent b08bd8e commit d741a6e

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

.github/workflows/static-analysis.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,44 @@ jobs:
6363
- name: "Run Psalm"
6464
run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=$(nproc)"
6565

66+
rector:
67+
name: "Rector"
68+
runs-on: "ubuntu-22.04"
69+
70+
steps:
71+
- name: "Checkout"
72+
uses: "actions/checkout@v3"
73+
74+
- name: Setup cache environment
75+
id: extcache
76+
uses: shivammathur/cache-extensions@v1
77+
with:
78+
php-version: ${{ env.PHP_VERSION }}
79+
extensions: "mongodb-${{ ENV.DRIVER_VERSION }}"
80+
key: "extcache-v1"
81+
82+
- name: Cache extensions
83+
uses: actions/cache@v3
84+
with:
85+
path: ${{ steps.extcache.outputs.dir }}
86+
key: ${{ steps.extcache.outputs.key }}
87+
restore-keys: ${{ steps.extcache.outputs.key }}
88+
89+
- name: "Install PHP"
90+
uses: "shivammathur/setup-php@v2"
91+
with:
92+
coverage: "none"
93+
extensions: "mongodb-${{ ENV.DRIVER_VERSION }}"
94+
php-version: "${{ env.PHP_VERSION }}"
95+
tools: "cs2pr"
96+
97+
- name: "Show driver information"
98+
run: "php --ri mongodb"
99+
100+
- name: "Install dependencies with Composer"
101+
uses: "ramsey/[email protected]"
102+
with:
103+
composer-options: "--no-suggest"
104+
66105
- name: "Run Rector"
67106
run: "vendor/bin/rector --ansi --dry-run"

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
use stdClass;
1212

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

18+
use const JSON_THROW_ON_ERROR;
19+
1720
class LazyBSONArrayTest extends TestCase
1821
{
1922
public static function provideTestArray(): Generator
@@ -273,4 +276,14 @@ public function testSerialization(): void
273276

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

tests/Model/LazyBSONDocumentTest.php

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

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

18+
use const JSON_THROW_ON_ERROR;
19+
1720
class LazyBSONDocumentTest extends TestCase
1821
{
1922
public static function provideTestDocument(): Generator
@@ -323,4 +326,14 @@ public function testSerialization(): void
323326

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

0 commit comments

Comments
 (0)