/**
  * @return \MongoDB\Driver\Cursor
  */
 protected function ensureCursor()
 {
     if ($this->cursor === null) {
         $this->cursor = $this->db->command(TypeConverter::convertLegacyArrayToObject($this->command), $this->getOptions());
     }
     return $this->cursor;
 }
Пример #2
0
 /**
  * @link http://php.net/manual/en/mongocode.construct.php
  * @param string $code A string of code
  * @param array $scope The scope to use for the code
  */
 public function __construct($code, array $scope = [])
 {
     if ($code instanceof \MongoDB\BSON\Javascript) {
         $javascript = $code;
         $code = $javascript->getCode();
         $scope = TypeConverter::toLegacy($javascript->getScope());
     }
     $this->code = $code;
     $this->scope = $scope;
 }
 /**
  * @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;
 }
Пример #4
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];
 }
Пример #6
0
 /**
  * Returns the current element
  * @link http://www.php.net/manual/en/mongocursor.current.php
  * @return array
  */
 public function current()
 {
     $document = $this->ensureIterator()->current();
     if ($document !== null) {
         $document = TypeConverter::toLegacy($document);
     }
     return $document;
 }
 /**
  * Querys this collection, returning a single element
  * @link http://www.php.net/manual/en/mongocollection.findone.php
  * @param array $query The fields for which to search.
  * @param array $fields Fields of the results to return.
  * @return array|null
  */
 public function findOne(array $query = array(), array $fields = array())
 {
     $document = $this->collection->findOne(TypeConverter::convertLegacyArrayToObject($query), ['projection' => $fields]);
     if ($document !== null) {
         $document = TypeConverter::convertObjectToLegacyArray($document);
     }
     return $document;
 }
 /**
  * @dataProvider converterData
  */
 public function testFromLegacy($legacyValue, $modernValue)
 {
     $this->skipTestIf(extension_loaded('mongo'));
     $this->assertEquals($modernValue, TypeConverter::fromLegacy($legacyValue));
 }
Пример #9
0
 /**
  * Drops a collection
  *
  * @link http://www.php.net/manual/en/mongodb.dropcollection.php
  * @param MongoCollection|string $coll MongoCollection or name of collection to drop.
  * @return array Returns the database response.
  *
  * @deprecated Use MongoCollection::drop() instead.
  */
 public function dropCollection($coll)
 {
     if ($coll instanceof MongoCollection) {
         $coll = $coll->getName();
     }
     return TypeConverter::toLegacy($this->db->dropCollection((string) $coll));
 }
Пример #10
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);
 }
Пример #11
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));
}
Пример #12
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;
     }
 }
Пример #13
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;
     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);
     }
 }
Пример #14
0
 /**
  * @return array
  */
 protected function convertProjection()
 {
     return TypeConverter::fromLegacy($this->projection);
 }
Пример #15
0
 /**
  * Stores the current cursor element.
  *
  * This is necessary because hasNext() might advance the iterator but we still
  * need to be able to return the current object.
  */
 protected function storeIteratorState()
 {
     if (!$this->startedIterating) {
         $this->current = null;
         $this->key = null;
         $this->valid = false;
         return null;
     }
     $this->current = $this->ensureIterator()->current();
     $this->key = $this->ensureIterator()->key();
     $this->valid = $this->ensureIterator()->valid();
     if ($this->current !== null) {
         $this->current = TypeConverter::toLegacy($this->current);
     }
     return $this->current;
 }
Пример #16
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);
 }