isFirstKeyOperator() public static method

This is used for differentiating update and replacement documents.
public static isFirstKeyOperator ( array | object $document ) : boolean
$document array | object Update or replacement document
return boolean
Example #1
0
 /**
  * Constructs a findAndModify command for updating a document.
  *
  * Supported options:
  *
  *  * bypassDocumentValidation (boolean): If true, allows the write to opt
  *    out of document level validation.
  *
  *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  *    run.
  *
  *  * projection (document): Limits the fields to return for the matching
  *    document.
  *
  *  * returnDocument (enum): Whether to return the document before or after
  *    the update is applied. Must be either
  *    FindOneAndUpdate::RETURN_DOCUMENT_BEFORE or
  *    FindOneAndUpdate::RETURN_DOCUMENT_AFTER. The default is
  *    FindOneAndUpdate::RETURN_DOCUMENT_BEFORE.
  *
  *  * sort (document): Determines which document the operation modifies if
  *    the query selects multiple documents.
  *
  *  * upsert (boolean): When true, a new document is created if no document
  *    matches the query. The default is false.
  *
  *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This option
  *    is only supported for server versions >= 3.2.
  *
  * @param string       $databaseName Database name
  * @param string       $collectionName Collection name
  * @param array|object $filter Query by which to filter documents
  * @param array|object $update Update to apply to the matched document
  * @param array        $options Command options
  *
  * @throws InvalidArgumentException
  */
 public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
 {
     if (!is_array($filter) && !is_object($filter)) {
         throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
     }
     if (!is_array($update) && !is_object($update)) {
         throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
     }
     if (!Functions::isFirstKeyOperator($update)) {
         throw new InvalidArgumentException('First key in $update argument is not an update operator');
     }
     $options += ['returnDocument' => self::RETURN_DOCUMENT_BEFORE, 'upsert' => false];
     if (isset($options['projection']) && !is_array($options['projection']) && !is_object($options['projection'])) {
         throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
     }
     if (!is_integer($options['returnDocument'])) {
         throw InvalidArgumentException::invalidType('"returnDocument" option', $options['returnDocument'], 'integer');
     }
     if ($options['returnDocument'] !== self::RETURN_DOCUMENT_AFTER && $options['returnDocument'] !== self::RETURN_DOCUMENT_BEFORE) {
         throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']);
     }
     if (isset($options['projection'])) {
         $options['fields'] = $options['projection'];
     }
     $options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER;
     unset($options['projection'], $options['returnDocument']);
     $this->findAndModify = new FindAndModify($databaseName, $collectionName, ['query' => $filter, 'update' => $update] + $options);
 }
Example #2
0
 /**
  * Constructs a update command.
  *
  * Supported options:
  *
  *  * bypassDocumentValidation (boolean): If true, allows the write to opt
  *    out of document level validation.
  *
  *  * multi (boolean): When true, updates all documents matching the query.
  *    This option cannot be true if the $update argument is a replacement
  *    document (i.e. contains no update operators). The default is false.
  *
  *  * upsert (boolean): When true, a new document is created if no document
  *    matches the query. The default is false.
  *
  *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  *
  * @param string       $databaseName Database name
  * @param string       $collectionName Collection name
  * @param array|object $filter Query by which to delete documents
  * @param array|object $update Update to apply to the matched
  *                                     document(s) or a replacement document
  * @param array        $options Command options
  *
  * @throws InvalidArgumentException
  */
 public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
 {
     if (!is_array($filter) && !is_object($filter)) {
         throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
     }
     if (!is_array($update) && !is_object($update)) {
         throw InvalidArgumentException::invalidType('$update', $filter, 'array or object');
     }
     $options += ['multi' => false, 'upsert' => false];
     if (isset($options['bypassDocumentValidation']) && !is_bool($options['bypassDocumentValidation'])) {
         throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
     }
     if (!is_bool($options['multi'])) {
         throw InvalidArgumentException::invalidType('"multi" option', $options['multi'], 'boolean');
     }
     if ($options['multi'] && !Functions::isFirstKeyOperator($update)) {
         throw new InvalidArgumentException('"multi" option cannot be true if $update is a replacement document');
     }
     if (!is_bool($options['upsert'])) {
         throw InvalidArgumentException::invalidType('"upsert" option', $options['upsert'], 'boolean');
     }
     if (isset($options['writeConcern']) && !$options['writeConcern'] instanceof WriteConcern) {
         throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\\Driver\\WriteConcern');
     }
     $this->databaseName = (string) $databaseName;
     $this->collectionName = (string) $collectionName;
     $this->filter = $filter;
     $this->update = $update;
     $this->options = $options;
 }
Example #3
0
 /**
  * Constructs an update command.
  *
  * Supported options:
  *
  *  * bypassDocumentValidation (boolean): If true, allows the write to opt
  *    out of document level validation.
  *
  *  * upsert (boolean): When true, a new document is created if no document
  *    matches the query. The default is false.
  *
  *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  *
  * @param string       $databaseName Database name
  * @param string       $collectionName Collection name
  * @param array|object $filter Query by which to filter documents
  * @param array|object $update Update to apply to the matched document
  * @param array        $options Command options
  *
  * @throws InvalidArgumentException
  */
 public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
 {
     if (!is_array($update) && !is_object($update)) {
         throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
     }
     if (!Functions::isFirstKeyOperator($update)) {
         throw new InvalidArgumentException('First key in $update argument is not an update operator');
     }
     $this->update = new Update($databaseName, $collectionName, $filter, $update, ['multi' => false] + $options);
 }
Example #4
0
 /**
  * Constructs a bulk write operation.
  *
  * Example array structure for all supported operation types:
  *
  *  [
  *    [ 'deleteMany' => [ $filter ] ],
  *    [ 'deleteOne'  => [ $filter ] ],
  *    [ 'insertOne'  => [ $document ] ],
  *    [ 'replaceOne' => [ $filter, $replacement, $options ] ],
  *    [ 'updateMany' => [ $filter, $update, $options ] ],
  *    [ 'updateOne'  => [ $filter, $update, $options ] ],
  *  ]
  *
  * Arguments correspond to the respective Operation classes; however, the
  * writeConcern option is specified for the top-level bulk write operation
  * instead of each individual operation.
  *
  * Supported options for replaceOne, updateMany, and updateOne operations:
  *
  *  * upsert (boolean): When true, a new document is created if no document
  *    matches the query. The default is false.
  *
  * Supported options for the bulk write operation:
  *
  *  * bypassDocumentValidation (boolean): If true, allows the write to opt
  *    out of document level validation.
  *
  *  * ordered (boolean): If true, when an insert fails, return without
  *    performing the remaining writes. If false, when a write fails,
  *    continue with the remaining writes, if any. The default is true.
  *
  *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  *
  * @param string  $databaseName Database name
  * @param string  $collectionName Collection name
  * @param array[] $operations List of write operations
  * @param array   $options Command options
  *
  * @throws InvalidArgumentException
  */
 public function __construct($databaseName, $collectionName, array $operations, array $options = [])
 {
     if (empty($operations)) {
         throw new InvalidArgumentException('$operations is empty');
     }
     $expectedIndex = 0;
     foreach ($operations as $i => $operation) {
         if ($i !== $expectedIndex) {
             throw new InvalidArgumentException(sprintf('$operations is not a list (unexpected index: "%s")', $i));
         }
         if (!is_array($operation)) {
             throw InvalidArgumentException::invalidType(sprintf('$operations[%d]', $i), $operation, 'array');
         }
         if (count($operation) !== 1) {
             throw new InvalidArgumentException(sprintf('Expected one element in $operation[%d], actually: %d', $i, count($operation)));
         }
         $type = key($operation);
         $args = current($operation);
         if (!isset($args[0]) && !array_key_exists(0, $args)) {
             throw new InvalidArgumentException(sprintf('Missing first argument for $operations[%d]["%s"]', $i, $type));
         }
         if (!is_array($args[0]) && !is_object($args[0])) {
             throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0], 'array or object');
         }
         switch ($type) {
             case self::INSERT_ONE:
                 break;
             case self::DELETE_MANY:
             case self::DELETE_ONE:
                 $operations[$i][$type][1] = ['limit' => $type === self::DELETE_ONE ? 1 : 0];
                 break;
             case self::REPLACE_ONE:
                 if (!isset($args[1]) && !array_key_exists(1, $args)) {
                     throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
                 }
                 if (!is_array($args[1]) && !is_object($args[1])) {
                     throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
                 }
                 if (Functions::isFirstKeyOperator($args[1])) {
                     throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is an update operator', $i, $type));
                 }
                 if (!isset($args[2])) {
                     $args[2] = [];
                 }
                 if (!is_array($args[2])) {
                     throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array');
                 }
                 $args[2]['multi'] = false;
                 $args[2] += ['upsert' => false];
                 if (!is_bool($args[2]['upsert'])) {
                     throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
                 }
                 $operations[$i][$type][2] = $args[2];
                 break;
             case self::UPDATE_MANY:
             case self::UPDATE_ONE:
                 if (!isset($args[1]) && !array_key_exists(1, $args)) {
                     throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
                 }
                 if (!is_array($args[1]) && !is_object($args[1])) {
                     throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
                 }
                 if (!Functions::isFirstKeyOperator($args[1])) {
                     throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is not an update operator', $i, $type));
                 }
                 if (!isset($args[2])) {
                     $args[2] = [];
                 }
                 if (!is_array($args[2])) {
                     throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array');
                 }
                 $args[2]['multi'] = $type === self::UPDATE_MANY;
                 $args[2] += ['upsert' => false];
                 if (!is_bool($args[2]['upsert'])) {
                     throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
                 }
                 $operations[$i][$type][2] = $args[2];
                 break;
             default:
                 throw new InvalidArgumentException(sprintf('Unknown operation type "%s" in $operations[%d]', $type, $i));
         }
         $expectedIndex += 1;
     }
     $options += ['ordered' => true];
     if (isset($options['bypassDocumentValidation']) && !is_bool($options['bypassDocumentValidation'])) {
         throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
     }
     if (!is_bool($options['ordered'])) {
         throw InvalidArgumentException::invalidType('"ordered" option', $options['ordered'], 'boolean');
     }
     if (isset($options['writeConcern']) && !$options['writeConcern'] instanceof WriteConcern) {
         throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\\Driver\\WriteConcern');
     }
     $this->databaseName = (string) $databaseName;
     $this->collectionName = (string) $collectionName;
     $this->operations = $operations;
     $this->options = $options;
 }