Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 39 and 311]

   1  <?php
   2  /*
   3   * Copyright 2015-2017 MongoDB, Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *   http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  namespace MongoDB\Operation;
  19  
  20  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  21  use MongoDB\Driver\Server;
  22  use MongoDB\Exception\InvalidArgumentException;
  23  use MongoDB\Exception\UnsupportedException;
  24  use function is_array;
  25  use function is_integer;
  26  use function is_object;
  27  use function MongoDB\is_first_key_operator;
  28  
  29  /**
  30   * Operation for replacing a document with the findAndModify command.
  31   *
  32   * @api
  33   * @see \MongoDB\Collection::findOneAndReplace()
  34   * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
  35   */
  36  class FindOneAndReplace implements Executable, Explainable
  37  {
  38      const RETURN_DOCUMENT_BEFORE = 1;
  39      const RETURN_DOCUMENT_AFTER = 2;
  40  
  41      /** @var FindAndModify */
  42      private $findAndModify;
  43  
  44      /**
  45       * Constructs a findAndModify command for replacing a document.
  46       *
  47       * Supported options:
  48       *
  49       *  * bypassDocumentValidation (boolean): If true, allows the write to
  50       *    circumvent document level validation.
  51       *
  52       *    For servers < 3.2, this option is ignored as document level validation
  53       *    is not available.
  54       *
  55       *  * collation (document): Collation specification.
  56       *
  57       *    This is not supported for server versions < 3.4 and will result in an
  58       *    exception at execution time if used.
  59       *
  60       *  * hint (string|document): The index to use. Specify either the index
  61       *    name as a string or the index key pattern as a document. If specified,
  62       *    then the query system will only consider plans using the hinted index.
  63       *
  64       *    This is not supported for server versions < 4.4 and will result in an
  65       *    exception at execution time if used.
  66       *
  67       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  68       *    run.
  69       *
  70       *  * projection (document): Limits the fields to return for the matching
  71       *    document.
  72       *
  73       *  * returnDocument (enum): Whether to return the document before or after
  74       *    the update is applied. Must be either
  75       *    FindOneAndReplace::RETURN_DOCUMENT_BEFORE or
  76       *    FindOneAndReplace::RETURN_DOCUMENT_AFTER. The default is
  77       *    FindOneAndReplace::RETURN_DOCUMENT_BEFORE.
  78       *
  79       *  * session (MongoDB\Driver\Session): Client session.
  80       *
  81       *    Sessions are not supported for server versions < 3.6.
  82       *
  83       *  * sort (document): Determines which document the operation modifies if
  84       *    the query selects multiple documents.
  85       *
  86       *  * typeMap (array): Type map for BSON deserialization.
  87       *
  88       *  * upsert (boolean): When true, a new document is created if no document
  89       *    matches the query. The default is false.
  90       *
  91       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  92       *
  93       *    This is not supported for server versions < 3.2 and will result in an
  94       *    exception at execution time if used.
  95       *
  96       * @param string       $databaseName   Database name
  97       * @param string       $collectionName Collection name
  98       * @param array|object $filter         Query by which to filter documents
  99       * @param array|object $replacement    Replacement document
 100       * @param array        $options        Command options
 101       * @throws InvalidArgumentException for parameter/option parsing errors
 102       */
 103      public function __construct($databaseName, $collectionName, $filter, $replacement, array $options = [])
 104      {
 105          if (! is_array($filter) && ! is_object($filter)) {
 106              throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
 107          }
 108  
 109          if (! is_array($replacement) && ! is_object($replacement)) {
 110              throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object');
 111          }
 112  
 113          if (is_first_key_operator($replacement)) {
 114              throw new InvalidArgumentException('First key in $replacement argument is an update operator');
 115          }
 116  
 117          $options += [
 118              'returnDocument' => self::RETURN_DOCUMENT_BEFORE,
 119              'upsert' => false,
 120          ];
 121  
 122          if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
 123              throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
 124          }
 125  
 126          if (! is_integer($options['returnDocument'])) {
 127              throw InvalidArgumentException::invalidType('"returnDocument" option', $options['returnDocument'], 'integer');
 128          }
 129  
 130          if ($options['returnDocument'] !== self::RETURN_DOCUMENT_AFTER &&
 131              $options['returnDocument'] !== self::RETURN_DOCUMENT_BEFORE) {
 132              throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']);
 133          }
 134  
 135          if (isset($options['projection'])) {
 136              $options['fields'] = $options['projection'];
 137          }
 138  
 139          $options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER;
 140  
 141          unset($options['projection'], $options['returnDocument']);
 142  
 143          $this->findAndModify = new FindAndModify(
 144              $databaseName,
 145              $collectionName,
 146              ['query' => $filter, 'update' => $replacement] + $options
 147          );
 148      }
 149  
 150      /**
 151       * Execute the operation.
 152       *
 153       * @see Executable::execute()
 154       * @param Server $server
 155       * @return array|object|null
 156       * @throws UnsupportedException if collation or write concern is used and unsupported
 157       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 158       */
 159      public function execute(Server $server)
 160      {
 161          return $this->findAndModify->execute($server);
 162      }
 163  
 164      public function getCommandDocument(Server $server)
 165      {
 166          return $this->findAndModify->getCommandDocument($server);
 167      }
 168  }