Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of deprecated BSON functions #1361

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/tutorial/custom-types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ back to ``LocalDateTime``.
.. code-block:: php

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

var_dump($document);
var_dump($document->date->toDateTime());
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial/example-data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ example imports the ``zips.json`` file into a ``test.zips`` collection using the
$bulk = new MongoDB\Driver\BulkWrite;

foreach ($lines as $line) {
$bson = MongoDB\BSON\fromJSON($line);
$document = MongoDB\BSON\toPHP($bson);
$bson = MongoDB\BSON\Document::fromJSON($line);
$document = $bson->toPHP();
$bulk->insert($document);
}

Expand Down
5 changes: 2 additions & 3 deletions examples/aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

namespace MongoDB\Examples\Aggregate;

use MongoDB\BSON\Document;
use MongoDB\Client;

use function assert;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;
use function random_int;

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

function toJSON(object $document): string
{
return toRelaxedExtendedJSON(fromPHP($document));
return Document::fromPHP($document)->toRelaxedExtendedJSON();
}

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
Expand Down
5 changes: 2 additions & 3 deletions examples/bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

namespace MongoDB\Examples\Bulk;

use MongoDB\BSON\Document;
use MongoDB\Client;
use MongoDB\Driver\WriteConcern;

use function assert;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;

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

function toJSON(object $document): string
{
return toRelaxedExtendedJSON(fromPHP($document));
return Document::fromPHP($document)->toRelaxedExtendedJSON();
}

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
Expand Down
5 changes: 2 additions & 3 deletions examples/changestream.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

namespace MongoDB\Examples\ChangeStream;

use MongoDB\BSON\Document;
use MongoDB\Client;

use function assert;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;
use function time;

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

function toJSON(object $document): string
{
return toRelaxedExtendedJSON(fromPHP($document));
return Document::fromPHP($document)->toRelaxedExtendedJSON();
}

// Change streams require a replica set or sharded cluster
Expand Down
5 changes: 2 additions & 3 deletions examples/command_logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace MongoDB\Examples\CommandLogger;

use MongoDB\BSON\Document;
use MongoDB\Client;
use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
Expand All @@ -13,15 +14,13 @@
use function get_class;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;

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

function toJSON(object $document): string
{
return toRelaxedExtendedJSON(fromPHP($document));
return Document::fromPHP($document)->toRelaxedExtendedJSON();
}

class CommandLogger implements CommandSubscriber
Expand Down
5 changes: 2 additions & 3 deletions examples/sdam_logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace MongoDB\Examples;

use MongoDB\BSON\Document;
use MongoDB\Client;
use MongoDB\Driver\Monitoring\SDAMSubscriber;
use MongoDB\Driver\Monitoring\ServerChangedEvent;
Expand All @@ -17,16 +18,14 @@

use function get_class;
use function getenv;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;

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

/** @param array|object $document */
function toJSON($document): string
{
return toRelaxedExtendedJSON(fromPHP($document));
return Document::fromPHP($document)->toRelaxedExtendedJSON();
}

class SDAMLogger implements SDAMSubscriber
Expand Down
5 changes: 2 additions & 3 deletions examples/with_transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@

namespace MongoDB\Examples\WithTransaction;

use MongoDB\BSON\Document;
use MongoDB\Client;
use MongoDB\Driver\Session;

use function assert;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function MongoDB\with_transaction;
use function printf;

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

function toJSON(object $document): string
{
return toRelaxedExtendedJSON(fromPHP($document));
return Document::fromPHP($document)->toRelaxedExtendedJSON();
}

// Transactions require a replica set (MongoDB >= 4.0) or sharded cluster (MongoDB >= 4.2)
Expand Down
13 changes: 0 additions & 13 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,6 @@
<code>stdClass</code>
</MoreSpecificReturnType>
</file>
<file src="src/Model/BSONIterator.php">
<MixedArgument>
<code>$documentLength</code>
<code>$documentLength</code>
<code><![CDATA[$this->options['typeMap']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$this->position]]></code>
</MixedAssignment>
<MixedOperand>
<code>$documentLength</code>
</MixedOperand>
</file>
<file src="src/Model/ChangeStreamIterator.php">
<MixedArgument>
<code><![CDATA[$reply->cursor->nextBatch]]></code>
Expand Down
4 changes: 1 addition & 3 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
use function is_string;
use function method_exists;
use function MongoDB\apply_type_map_to_document;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toJSON;
use function property_exists;
use function sprintf;
use function str_contains;
Expand Down Expand Up @@ -705,7 +703,7 @@ public function uploadFromStream(string $filename, $source, array $options = [])
private function createPathForFile(object $file): string
{
if (is_array($file->_id) || (is_object($file->_id) && ! method_exists($file->_id, '__toString'))) {
$id = toJSON(fromPHP(['_id' => $file->_id]));
$id = Document::fromPHP(['_id' => $file->_id])->toRelaxedExtendedJSON();
} else {
$id = (string) $file->_id;
}
Expand Down
5 changes: 2 additions & 3 deletions src/GridFS/Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

namespace MongoDB\GridFS\Exception;

use MongoDB\BSON\Document;
use MongoDB\Exception\RuntimeException;

use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toJSON;
use function sprintf;

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

return new self(sprintf('File "%s" not found in "%s"', $json, $namespace));
}
Expand Down
5 changes: 2 additions & 3 deletions src/GridFS/Exception/StreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace MongoDB\GridFS\Exception;

use MongoDB\BSON\Document;
use MongoDB\Exception\RuntimeException;

use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toJSON;
use function sprintf;
use function stream_get_meta_data;

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

Expand Down
8 changes: 6 additions & 2 deletions src/Model/BSONIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
namespace MongoDB\Model;

use Iterator;
use MongoDB\BSON\Document;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use ReturnTypeWillChange;

use function assert;
use function is_array;
use function MongoDB\BSON\toPHP;
use function is_int;
use function sprintf;
use function strlen;
use function substr;
Expand All @@ -49,6 +51,7 @@ class BSONIterator implements Iterator

private int $position = 0;

/** @var array{typeMap: array, ...} */
private array $options;

/**
Expand Down Expand Up @@ -142,12 +145,13 @@ private function advance(): void
}

[, $documentLength] = unpack('V', substr($this->buffer, $this->position, self::BSON_SIZE));
assert(is_int($documentLength));

if ($this->bufferLength - $this->position < $documentLength) {
throw new UnexpectedValueException(sprintf('Expected %d bytes; %d remaining', $documentLength, $this->bufferLength - $this->position));
}

$this->current = toPHP(substr($this->buffer, $this->position, $documentLength), $this->options['typeMap']);
$this->current = Document::fromBSON(substr($this->buffer, $this->position, $documentLength))->toPHP($this->options['typeMap']);
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
$this->position += $documentLength;
}
}
4 changes: 1 addition & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
use function is_array;
use function is_object;
use function is_string;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toPHP;
use function str_ends_with;
use function substr;

Expand Down Expand Up @@ -115,7 +113,7 @@ function apply_type_map_to_document($document, array $typeMap)
throw InvalidArgumentException::expectedDocumentType('$document', $document);
}

return toPHP(fromPHP($document), $typeMap);
return Document::fromPHP($document)->toPHP($typeMap);
}

/**
Expand Down
Loading