fromLegacy() public static method

This method handles type conversion from ext-mongo to ext-mongodb: - For all types (MongoId, MongoDate, etc.) it returns the correct BSON object instance - For arrays and objects it iterates over properties and converts each item individually - For other types it returns the value unconverted
public static fromLegacy ( mixed $value ) : mixed
$value mixed
return mixed
 /**
  * @return \MongoDB\Driver\Cursor
  */
 protected function ensureCursor()
 {
     if ($this->cursor === null) {
         $convertedCommand = TypeConverter::fromLegacy($this->command);
         if (isset($convertedCommand->cursor)) {
             if ($convertedCommand->cursor === true || $convertedCommand->cursor === []) {
                 $convertedCommand->cursor = new \stdClass();
             }
         }
         $this->cursor = $this->db->command($convertedCommand, $this->getOptions());
     }
     return $this->cursor;
 }
Exemplo n.º 2
0
 /**
  * @dataProvider getCursorOptions
  */
 public function testCursorAppliesOptions($checkOptionCallback, \Closure $applyOptionCallback = null)
 {
     $query = ['foo' => 'bar'];
     $projection = ['_id' => false, 'foo' => true];
     $collectionMock = $this->getCollectionMock();
     $collectionMock->expects($this->once())->method('find')->with($this->equalTo(TypeConverter::fromLegacy($query)), $this->callback($checkOptionCallback))->will($this->returnValue(new \ArrayIterator([])));
     $collection = $this->getCollection('test');
     $cursor = $collection->find($query, $projection);
     // Replace the original MongoDB collection with our mock
     $reflectionProperty = new \ReflectionProperty($cursor, 'collection');
     $reflectionProperty->setAccessible(true);
     $reflectionProperty->setValue($cursor, $collectionMock);
     if ($applyOptionCallback !== null) {
         $applyOptionCallback($cursor);
     }
     // Force query by converting to array
     iterator_to_array($cursor);
 }
 /**
  * Saves an object to this collection
  *
  * @link http://www.php.net/manual/en/mongocollection.save.php
  * @param array|object $a Array to save. If an object is used, it may not have protected or private properties.
  * @param array $options Options for the save.
  * @throws MongoException if the inserted document is empty or if it contains zero-length keys. Attempting to insert an object with protected and private properties will cause a zero-length key error.
  * @throws MongoCursorException if the "w" option is set and the write fails.
  * @throws MongoCursorTimeoutException if the "w" option is set to a value greater than one and the operation takes longer than MongoCursor::$timeout milliseconds to complete. This does not kill the operation on the server, it is a client-side timeout. The operation in MongoCollection::$wtimeout is milliseconds.
  * @return array|boolean If w was set, returns an array containing the status of the save.
  * Otherwise, returns a boolean representing if the array was not empty (an empty array will not be inserted).
  */
 public function save(&$a, array $options = [])
 {
     $id = $this->ensureDocumentHasMongoId($a);
     $document = (array) $a;
     $options['upsert'] = true;
     try {
         /** @var \MongoDB\UpdateResult $result */
         $result = $this->collection->replaceOne(TypeConverter::fromLegacy(['_id' => $id]), TypeConverter::fromLegacy($document), $this->convertWriteConcernOptions($options));
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         ExceptionConverter::toLegacy($e);
     }
     if (!$result->isAcknowledged()) {
         return true;
     }
     return ['ok' => 1.0, 'nModified' => $result->getModifiedCount(), 'n' => $result->getMatchedCount(), 'err' => null, 'errmsg' => null, 'updatedExisting' => $result->getUpsertedCount() == 0];
 }
 /**
  * @dataProvider converterData
  */
 public function testFromLegacy($legacyValue, $modernValue)
 {
     $this->skipTestIf(extension_loaded('mongo'));
     $this->assertEquals($modernValue, TypeConverter::fromLegacy($legacyValue));
 }
Exemplo n.º 5
0
 /**
  * Execute the query
  * @link http://www.php.net/manual/en/mongocursor.doquery.php
  * @throws MongoConnectionException if it cannot reach the database.
  * @return void
  */
 protected function doQuery()
 {
     $options = $this->getOptions() + $this->options;
     $this->cursor = $this->collection->find(TypeConverter::fromLegacy($this->query), $options);
 }
Exemplo n.º 6
0
/**
 * Serializes a PHP variable into a BSON string
 *
 * @param mixed $anything The variable to be serialized.
 * @return string Returns the serialized string.
 */
function bson_encode($anything)
{
    return \MongoDB\BSON\fromPHP(TypeConverter::fromLegacy($anything));
}
Exemplo n.º 7
0
 private function addItem(array $item)
 {
     switch ($this->batchType) {
         case self::COMMAND_UPDATE:
             $method = isset($item['multi']) ? 'updateMany' : 'updateOne';
             $options = [];
             if (isset($item['upsert']) && $item['upsert']) {
                 $options['upsert'] = true;
             }
             $this->items[] = [$method => [TypeConverter::fromLegacy($item['q']), TypeConverter::fromLegacy($item['u']), $options]];
             break;
         case self::COMMAND_INSERT:
             $this->items[] = ['insertOne' => [TypeConverter::fromLegacy($item)]];
             break;
         case self::COMMAND_DELETE:
             $method = $item['limit'] === 0 ? 'deleteMany' : 'deleteOne';
             $this->items[] = [$method => [TypeConverter::fromLegacy($item['q'])]];
             break;
     }
 }
 /**
  * Execute the query
  * @link http://www.php.net/manual/en/mongocursor.doquery.php
  * @throws MongoConnectionException if it cannot reach the database.
  * @return void
  */
 protected function doQuery()
 {
     $options = $this->getOptions() + $this->options;
     try {
         $this->cursor = $this->collection->find(TypeConverter::fromLegacy($this->query), $options);
     } catch (\MongoDB\Driver\Exception\ExecutionTimeoutException $e) {
         throw new MongoCursorTimeoutException($e->getMessage(), $e->getCode(), $e);
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         throw ExceptionConverter::toLegacy($e);
     }
 }
Exemplo n.º 9
0
 /**
  * @return array
  */
 protected function convertProjection()
 {
     return TypeConverter::fromLegacy($this->projection);
 }
Exemplo n.º 10
0
 /**
  * Counts the number of documents in this collection
  *
  * @link http://www.php.net/manual/en/mongocollection.count.php
  * @param array|stdClass $query
  * @param array $options
  * @return int Returns the number of documents matching the query.
  */
 public function count($query = [], array $options = [])
 {
     return $this->collection->count(TypeConverter::fromLegacy($query), $options);
 }