toLegacy() 공개 정적인 메소드

This method handles type conversion from ext-mongodb to ext-mongo: - For all instances of BSON\Type it returns an object of the corresponding legacy type (MongoId, MongoDate, etc.) - For arrays and objects it iterates over properties and converts each item individually - For other types it returns the value unconverted
public static toLegacy ( mixed $value ) : mixed
$value mixed
리턴 mixed
예제 #1
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;
 }
 /**
  * Delete all indexes for this collection
  *
  * @link http://www.php.net/manual/en/mongocollection.deleteindexes.php
  * @return array Returns the database response.
  */
 public function deleteIndexes()
 {
     return TypeConverter::toLegacy($this->collection->dropIndexes());
 }
예제 #3
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;
 }
예제 #4
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));
 }
예제 #5
0
 /**
  * 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));
         if (!$result->isAcknowledged()) {
             return true;
         }
         $resultArray = ['ok' => 1.0, 'nModified' => $result->getModifiedCount(), 'n' => $result->getUpsertedCount() + $result->getModifiedCount(), 'err' => null, 'errmsg' => null, 'updatedExisting' => $result->getUpsertedCount() == 0];
         if ($result->getUpsertedId() !== null) {
             $resultArray['upserted'] = TypeConverter::toLegacy($result->getUpsertedId());
         }
         return $resultArray;
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         throw ExceptionConverter::toLegacy($e);
     }
 }
예제 #6
0
/**
 * Deserializes a BSON object into a PHP array
 *
 * @param string $bson The BSON to be deserialized.
 * @return array Returns the deserialized BSON object.
 */
function bson_decode($bson)
{
    return TypeConverter::toLegacy(\MongoDB\BSON\toPHP($bson));
}
예제 #7
0
 /**
  * Executes a batch of write operations
  *
  * @see http://php.net/manual/en/mongowritebatch.execute.php
  * @param array $writeOptions
  * @return array
  */
 public final function execute(array $writeOptions = [])
 {
     $writeOptions += $this->writeOptions;
     if (!count($this->items)) {
         return ['ok' => true];
     }
     if (isset($writeOptions['j'])) {
         trigger_error('j parameter is not supported', E_WARNING);
     }
     if (isset($writeOptions['fsync'])) {
         trigger_error('fsync parameter is not supported', E_WARNING);
     }
     $options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions);
     if (isset($writeOptions['ordered'])) {
         $options['ordered'] = $writeOptions['ordered'];
     }
     try {
         $writeResult = $this->collection->getCollection()->bulkWrite($this->items, $options);
         $resultDocument = [];
         $ok = true;
     } catch (BulkWriteException $e) {
         $writeResult = $e->getWriteResult();
         $resultDocument = ['writeErrors' => $this->convertWriteErrors($writeResult)];
         $ok = false;
     }
     $this->items = [];
     switch ($this->batchType) {
         case self::COMMAND_UPDATE:
             $upsertedIds = [];
             foreach ($writeResult->getUpsertedIds() as $index => $id) {
                 $upsertedIds[] = ['index' => $index, '_id' => TypeConverter::toLegacy($id)];
             }
             $resultDocument += ['nMatched' => $writeResult->getMatchedCount(), 'nModified' => $writeResult->getModifiedCount(), 'nUpserted' => $writeResult->getUpsertedCount(), 'ok' => true];
             if (count($upsertedIds)) {
                 $resultDocument['upserted'] = $upsertedIds;
             }
             break;
         case self::COMMAND_DELETE:
             $resultDocument += ['nRemoved' => $writeResult->getDeletedCount(), 'ok' => true];
             break;
         case self::COMMAND_INSERT:
             $resultDocument += ['nInserted' => $writeResult->getInsertedCount(), 'ok' => true];
             break;
     }
     if (!$ok) {
         // Exception code is hardcoded to the value in ext-mongo, see
         // https://github.com/mongodb/mongo-php-driver-legacy/blob/ab4bc0d90e93b3f247f6bcb386d0abc8d2fa7d74/batch/write.c#L428
         throw new \MongoWriteConcernException('Failed write', 911, null, $resultDocument);
     }
     return $resultDocument;
 }
예제 #8
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;
 }
 /**
  * Executes a batch of write operations
  *
  * @see http://php.net/manual/en/mongowritebatch.execute.php
  * @param array $writeOptions
  * @return array
  */
 public final function execute(array $writeOptions = [])
 {
     $writeOptions += $this->writeOptions;
     if (!count($this->items)) {
         return ['ok' => true];
     }
     if (isset($writeOptions['j'])) {
         trigger_error('j parameter is not supported', E_WARNING);
     }
     if (isset($writeOptions['fsync'])) {
         trigger_error('fsync parameter is not supported', E_WARNING);
     }
     $options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions);
     if (isset($writeOptions['ordered'])) {
         $options['ordered'] = $writeOptions['ordered'];
     }
     $collection = $this->collection->getCollection();
     try {
         $result = $collection->BulkWrite($this->items, $options);
         $ok = true;
     } catch (\MongoDB\Driver\Exception\BulkWriteException $e) {
         $result = $e->getWriteResult();
         $ok = false;
     }
     if ($ok === true) {
         $this->items = [];
     }
     switch ($this->batchType) {
         case self::COMMAND_UPDATE:
             $upsertedIds = [];
             foreach ($result->getUpsertedIds() as $index => $id) {
                 $upsertedIds[] = ['index' => $index, '_id' => TypeConverter::toLegacy($id)];
             }
             $result = ['nMatched' => $result->getMatchedCount(), 'nModified' => $result->getModifiedCount(), 'nUpserted' => $result->getUpsertedCount(), 'ok' => $ok];
             if (count($upsertedIds)) {
                 $result['upserted'] = $upsertedIds;
             }
             return $result;
         case self::COMMAND_DELETE:
             return ['nRemoved' => $result->getDeletedCount(), 'ok' => $ok];
         case self::COMMAND_INSERT:
             return ['nInserted' => $result->getInsertedCount(), 'ok' => $ok];
     }
 }