Skip to content

Commit 519065b

Browse files
committed
Remove usage of deprecated BSON functions
1 parent de3aaf8 commit 519065b

19 files changed

+51
-69
lines changed

docs/tutorial/custom-types.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ back to ``LocalDateTime``.
151151
.. code-block:: php
152152

153153
<?php
154-
$bson = MongoDB\BSON\fromPHP(['date' => new LocalDateTime]);
155-
$document = MongoDB\BSON\toPHP($bson);
154+
$bson = MongoDB\BSON\Document::fromPHP(['date' => new LocalDateTime]);
155+
$document = $bson->toPHP();
156156

157157
var_dump($document);
158158
var_dump($document->date->toDateTime());

docs/tutorial/example-data.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ example imports the ``zips.json`` file into a ``test.zips`` collection using the
2222
$bulk = new MongoDB\Driver\BulkWrite;
2323

2424
foreach ($lines as $line) {
25-
$bson = MongoDB\BSON\fromJSON($line);
26-
$document = MongoDB\BSON\toPHP($bson);
25+
$bson = MongoDB\BSON\Document::fromJSON($line);
26+
$document = $bson->toPHP();
2727
$bulk->insert($document);
2828
}
2929

examples/aggregate.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace MongoDB\Examples\Aggregate;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78

89
use function assert;
910
use function getenv;
1011
use function is_object;
11-
use function MongoDB\BSON\fromPHP;
12-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1312
use function printf;
1413
use function random_int;
1514

1615
require __DIR__ . '/../vendor/autoload.php';
1716

1817
function toJSON(object $document): string
1918
{
20-
return toRelaxedExtendedJSON(fromPHP($document));
19+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2120
}
2221

2322
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

examples/bulk.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace MongoDB\Examples\Bulk;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78
use MongoDB\Driver\WriteConcern;
89

910
use function assert;
1011
use function getenv;
1112
use function is_object;
12-
use function MongoDB\BSON\fromPHP;
13-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1413
use function printf;
1514

1615
require __DIR__ . '/../vendor/autoload.php';
1716

1817
function toJSON(object $document): string
1918
{
20-
return toRelaxedExtendedJSON(fromPHP($document));
19+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2120
}
2221

2322
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

examples/changestream.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace MongoDB\Examples\ChangeStream;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78

89
use function assert;
910
use function getenv;
1011
use function is_object;
11-
use function MongoDB\BSON\fromPHP;
12-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1312
use function printf;
1413
use function time;
1514

1615
require __DIR__ . '/../vendor/autoload.php';
1716

1817
function toJSON(object $document): string
1918
{
20-
return toRelaxedExtendedJSON(fromPHP($document));
19+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2120
}
2221

2322
// Change streams require a replica set or sharded cluster

examples/command_logger.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MongoDB\Examples\CommandLogger;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78
use MongoDB\Driver\Monitoring\CommandFailedEvent;
89
use MongoDB\Driver\Monitoring\CommandStartedEvent;
@@ -13,15 +14,13 @@
1314
use function get_class;
1415
use function getenv;
1516
use function is_object;
16-
use function MongoDB\BSON\fromPHP;
17-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1817
use function printf;
1918

2019
require __DIR__ . '/../vendor/autoload.php';
2120

2221
function toJSON(object $document): string
2322
{
24-
return toRelaxedExtendedJSON(fromPHP($document));
23+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2524
}
2625

2726
class CommandLogger implements CommandSubscriber

examples/sdam_logger.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MongoDB\Examples;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78
use MongoDB\Driver\Monitoring\SDAMSubscriber;
89
use MongoDB\Driver\Monitoring\ServerChangedEvent;
@@ -17,16 +18,14 @@
1718

1819
use function get_class;
1920
use function getenv;
20-
use function MongoDB\BSON\fromPHP;
21-
use function MongoDB\BSON\toRelaxedExtendedJSON;
2221
use function printf;
2322

2423
require __DIR__ . '/../vendor/autoload.php';
2524

2625
/** @param array|object $document */
2726
function toJSON($document): string
2827
{
29-
return toRelaxedExtendedJSON(fromPHP($document));
28+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
3029
}
3130

3231
class SDAMLogger implements SDAMSubscriber

examples/with_transaction.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33

44
namespace MongoDB\Examples\WithTransaction;
55

6+
use MongoDB\BSON\Document;
67
use MongoDB\Client;
78
use MongoDB\Driver\Session;
89

910
use function assert;
1011
use function getenv;
1112
use function is_object;
12-
use function MongoDB\BSON\fromPHP;
13-
use function MongoDB\BSON\toRelaxedExtendedJSON;
1413
use function MongoDB\with_transaction;
1514
use function printf;
1615

1716
require __DIR__ . '/../vendor/autoload.php';
1817

1918
function toJSON(object $document): string
2019
{
21-
return toRelaxedExtendedJSON(fromPHP($document));
20+
return Document::fromPHP($document)->toRelaxedExtendedJSON();
2221
}
2322

2423
// Transactions require a replica set (MongoDB >= 4.0) or sharded cluster (MongoDB >= 4.2)

src/GridFS/Bucket.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
use function is_string;
5353
use function method_exists;
5454
use function MongoDB\apply_type_map_to_document;
55-
use function MongoDB\BSON\fromPHP;
56-
use function MongoDB\BSON\toJSON;
5755
use function property_exists;
5856
use function sprintf;
5957
use function str_contains;
@@ -705,7 +703,7 @@ public function uploadFromStream(string $filename, $source, array $options = [])
705703
private function createPathForFile(object $file): string
706704
{
707705
if (is_array($file->_id) || (is_object($file->_id) && ! method_exists($file->_id, '__toString'))) {
708-
$id = toJSON(fromPHP(['_id' => $file->_id]));
706+
$id = Document::fromPHP(['_id' => $file->_id])->toRelaxedExtendedJSON();
709707
} else {
710708
$id = (string) $file->_id;
711709
}

src/GridFS/Exception/FileNotFoundException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
namespace MongoDB\GridFS\Exception;
1919

20+
use MongoDB\BSON\Document;
2021
use MongoDB\Exception\RuntimeException;
2122

22-
use function MongoDB\BSON\fromPHP;
23-
use function MongoDB\BSON\toJSON;
2423
use function sprintf;
2524

2625
class FileNotFoundException extends RuntimeException
@@ -58,7 +57,7 @@ public static function byFilenameAndRevision(string $filename, int $revision, st
5857
*/
5958
public static function byId($id, string $namespace)
6059
{
61-
$json = toJSON(fromPHP(['_id' => $id]));
60+
$json = Document::fromPHP(['_id' => $id])->toRelaxedExtendedJSON();
6261

6362
return new self(sprintf('File "%s" not found in "%s"', $json, $namespace));
6463
}

src/GridFS/Exception/StreamException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace MongoDB\GridFS\Exception;
44

5+
use MongoDB\BSON\Document;
56
use MongoDB\Exception\RuntimeException;
67

7-
use function MongoDB\BSON\fromPHP;
8-
use function MongoDB\BSON\toJSON;
98
use function sprintf;
109
use function stream_get_meta_data;
1110

@@ -30,7 +29,7 @@ public static function downloadFromFilenameFailed(string $filename, $source, $de
3029
*/
3130
public static function downloadFromIdFailed($id, $source, $destination): self
3231
{
33-
$idString = toJSON(fromPHP(['_id' => $id]));
32+
$idString = Document::fromPHP(['_id' => $id])->toRelaxedExtendedJSON();
3433
$sourceMetadata = stream_get_meta_data($source);
3534
$destinationMetadata = stream_get_meta_data($destination);
3635

src/Model/BSONIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
namespace MongoDB\Model;
1919

2020
use Iterator;
21+
use MongoDB\BSON\Document;
2122
use MongoDB\Exception\InvalidArgumentException;
2223
use MongoDB\Exception\UnexpectedValueException;
2324
use ReturnTypeWillChange;
2425

2526
use function is_array;
26-
use function MongoDB\BSON\toPHP;
2727
use function sprintf;
2828
use function strlen;
2929
use function substr;
@@ -147,7 +147,7 @@ private function advance(): void
147147
throw new UnexpectedValueException(sprintf('Expected %d bytes; %d remaining', $documentLength, $this->bufferLength - $this->position));
148148
}
149149

150-
$this->current = toPHP(substr($this->buffer, $this->position, $documentLength), $this->options['typeMap']);
150+
$this->current = Document::fromBSON(substr($this->buffer, $this->position, $documentLength))->toPHP($this->options['typeMap']);
151151
$this->position += $documentLength;
152152
}
153153
}

src/functions.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
use function is_array;
4444
use function is_object;
4545
use function is_string;
46-
use function MongoDB\BSON\fromPHP;
47-
use function MongoDB\BSON\toPHP;
4846
use function str_ends_with;
4947
use function substr;
5048

@@ -115,7 +113,7 @@ function apply_type_map_to_document($document, array $typeMap)
115113
throw InvalidArgumentException::expectedDocumentType('$document', $document);
116114
}
117115

118-
return toPHP(fromPHP($document), $typeMap);
116+
return Document::fromPHP($document)->toPHP($typeMap);
119117
}
120118

121119
/**

tests/Model/BSONIteratorTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace MongoDB\Tests\Model;
44

5+
use MongoDB\BSON\Document;
56
use MongoDB\Exception\UnexpectedValueException;
67
use MongoDB\Model\BSONIterator;
78
use MongoDB\Tests\TestCase;
89

910
use function array_map;
1011
use function implode;
1112
use function iterator_to_array;
12-
use function MongoDB\BSON\fromPHP;
1313
use function substr;
1414

1515
class BSONIteratorTest extends TestCase
@@ -30,7 +30,7 @@ public function provideTypeMapOptionsAndExpectedDocuments()
3030
[
3131
null,
3232
implode(array_map(
33-
'MongoDB\BSON\fromPHP',
33+
fn ($input) => (string) Document::fromPHP($input),
3434
[
3535
['_id' => 1, 'x' => ['foo' => 'bar']],
3636
['_id' => 3, 'x' => ['foo' => 'bar']],
@@ -44,7 +44,7 @@ public function provideTypeMapOptionsAndExpectedDocuments()
4444
[
4545
['root' => 'array', 'document' => 'array'],
4646
implode(array_map(
47-
'MongoDB\BSON\fromPHP',
47+
fn ($input) => (string) Document::fromPHP($input),
4848
[
4949
['_id' => 1, 'x' => ['foo' => 'bar']],
5050
['_id' => 3, 'x' => ['foo' => 'bar']],
@@ -58,7 +58,7 @@ public function provideTypeMapOptionsAndExpectedDocuments()
5858
[
5959
['root' => 'object', 'document' => 'array'],
6060
implode(array_map(
61-
'MongoDB\BSON\fromPHP',
61+
fn ($input) => (string) Document::fromPHP($input),
6262
[
6363
['_id' => 1, 'x' => ['foo' => 'bar']],
6464
['_id' => 3, 'x' => ['foo' => 'bar']],
@@ -72,7 +72,7 @@ public function provideTypeMapOptionsAndExpectedDocuments()
7272
[
7373
['root' => 'array', 'document' => 'stdClass'],
7474
implode(array_map(
75-
'MongoDB\BSON\fromPHP',
75+
fn ($input) => (string) Document::fromPHP($input),
7676
[
7777
['_id' => 1, 'x' => ['foo' => 'bar']],
7878
['_id' => 3, 'x' => ['foo' => 'bar']],
@@ -88,7 +88,7 @@ public function provideTypeMapOptionsAndExpectedDocuments()
8888

8989
public function testCannotReadLengthFromFirstDocument(): void
9090
{
91-
$binaryString = substr(fromPHP([]), 0, 3);
91+
$binaryString = substr((string) Document::fromPHP([]), 0, 3);
9292

9393
$bsonIt = new BSONIterator($binaryString);
9494

@@ -99,7 +99,7 @@ public function testCannotReadLengthFromFirstDocument(): void
9999

100100
public function testCannotReadLengthFromSubsequentDocument(): void
101101
{
102-
$binaryString = fromPHP([]) . substr(fromPHP([]), 0, 3);
102+
$binaryString = (string) Document::fromPHP([]) . substr((string) Document::fromPHP([]), 0, 3);
103103

104104
$bsonIt = new BSONIterator($binaryString);
105105
$bsonIt->rewind();
@@ -111,7 +111,7 @@ public function testCannotReadLengthFromSubsequentDocument(): void
111111

112112
public function testCannotReadFirstDocument(): void
113113
{
114-
$binaryString = substr(fromPHP([]), 0, 4);
114+
$binaryString = substr((string) Document::fromPHP([]), 0, 4);
115115

116116
$bsonIt = new BSONIterator($binaryString);
117117

@@ -122,7 +122,7 @@ public function testCannotReadFirstDocument(): void
122122

123123
public function testCannotReadSecondDocument(): void
124124
{
125-
$binaryString = fromPHP([]) . substr(fromPHP([]), 0, 4);
125+
$binaryString = (string) Document::fromPHP([]) . substr((string) Document::fromPHP([]), 0, 4);
126126

127127
$bsonIt = new BSONIterator($binaryString);
128128
$bsonIt->rewind();

tests/SpecTests/DocumentsMatchConstraintTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\BSON\Binary;
66
use MongoDB\BSON\Decimal128;
7+
use MongoDB\BSON\Document;
78
use MongoDB\BSON\Int64;
89
use MongoDB\BSON\Javascript;
910
use MongoDB\BSON\MaxKey;
@@ -17,9 +18,6 @@
1718
use MongoDB\Tests\TestCase;
1819
use PHPUnit\Framework\ExpectationFailedException;
1920

20-
use function MongoDB\BSON\fromJSON;
21-
use function MongoDB\BSON\toPHP;
22-
2321
use const PHP_INT_SIZE;
2422

2523
class DocumentsMatchConstraintTest extends TestCase
@@ -82,9 +80,9 @@ public function testBSONTypeAssertions($type, $value): void
8280

8381
public function provideBSONTypes()
8482
{
85-
$undefined = toPHP(fromJSON('{ "x": {"$undefined": true} }'))->x;
86-
$symbol = toPHP(fromJSON('{ "x": {"$symbol": "test"} }'))->x;
87-
$dbPointer = toPHP(fromJSON('{ "x": {"$dbPointer": {"$ref": "db.coll", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }'))->x;
83+
$undefined = Document::fromJSON('{ "x": {"$undefined": true} }')->toPHP()->x;
84+
$symbol = Document::fromJSON('{ "x": {"$symbol": "test"} }')->toPHP()->x;
85+
$dbPointer = Document::fromJSON('{ "x": {"$dbPointer": {"$ref": "db.coll", "$id" : { "$oid" : "5a2e78accd485d55b405ac12" } }} }')->toPHP()->x;
8886
$int64 = new Int64(1);
8987
$long = PHP_INT_SIZE == 4 ? new Int64('4294967296') : 4_294_967_296;
9088

0 commit comments

Comments
 (0)