Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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 MongoDB\UpdateResult;
  25  use function is_array;
  26  use function is_object;
  27  use function MongoDB\is_first_key_operator;
  28  use function MongoDB\is_pipeline;
  29  
  30  /**
  31   * Operation for replacing a single document with the update command.
  32   *
  33   * @api
  34   * @see \MongoDB\Collection::replaceOne()
  35   * @see http://docs.mongodb.org/manual/reference/command/update/
  36   */
  37  class ReplaceOne implements Executable
  38  {
  39      /** @var Update */
  40      private $update;
  41  
  42      /**
  43       * Constructs an update command.
  44       *
  45       * Supported options:
  46       *
  47       *  * bypassDocumentValidation (boolean): If true, allows the write to
  48       *    circumvent document level validation.
  49       *
  50       *    For servers < 3.2, this option is ignored as document level validation
  51       *    is not available.
  52       *
  53       *  * collation (document): Collation specification.
  54       *
  55       *    This is not supported for server versions < 3.4 and will result in an
  56       *    exception at execution time if used.
  57       *
  58       *  * session (MongoDB\Driver\Session): Client session.
  59       *
  60       *    Sessions are not supported for server versions < 3.6.
  61       *
  62       *  * upsert (boolean): When true, a new document is created if no document
  63       *    matches the query. The default is false.
  64       *
  65       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  66       *
  67       * @param string       $databaseName   Database name
  68       * @param string       $collectionName Collection name
  69       * @param array|object $filter         Query by which to filter documents
  70       * @param array|object $replacement    Replacement document
  71       * @param array        $options        Command options
  72       * @throws InvalidArgumentException for parameter/option parsing errors
  73       */
  74      public function __construct($databaseName, $collectionName, $filter, $replacement, array $options = [])
  75      {
  76          if (! is_array($replacement) && ! is_object($replacement)) {
  77              throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object');
  78          }
  79  
  80          if (is_first_key_operator($replacement)) {
  81              throw new InvalidArgumentException('First key in $replacement argument is an update operator');
  82          }
  83  
  84          if (is_pipeline($replacement)) {
  85              throw new InvalidArgumentException('$replacement argument is a pipeline');
  86          }
  87  
  88          $this->update = new Update(
  89              $databaseName,
  90              $collectionName,
  91              $filter,
  92              $replacement,
  93              ['multi' => false] + $options
  94          );
  95      }
  96  
  97      /**
  98       * Execute the operation.
  99       *
 100       * @see Executable::execute()
 101       * @param Server $server
 102       * @return UpdateResult
 103       * @throws UnsupportedException if collation is used and unsupported
 104       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 105       */
 106      public function execute(Server $server)
 107      {
 108          return $this->update->execute($server);
 109      }
 110  }