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 function is_array;
  25  use function is_integer;
  26  use function is_object;
  27  use function MongoDB\is_first_key_operator;
  28  use function MongoDB\is_pipeline;
  29  
  30  /**
  31   * Operation for updating a document with the findAndModify command.
  32   *
  33   * @api
  34   * @see \MongoDB\Collection::findOneAndUpdate()
  35   * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
  36   */
  37  class FindOneAndUpdate implements Executable, Explainable
  38  {
  39      const RETURN_DOCUMENT_BEFORE = 1;
  40      const RETURN_DOCUMENT_AFTER = 2;
  41  
  42      /** @var FindAndModify */
  43      private $findAndModify;
  44  
  45      /**
  46       * Constructs a findAndModify command for updating a document.
  47       *
  48       * Supported options:
  49       *
  50       *  * arrayFilters (document array): A set of filters specifying to which
  51       *    array elements an update should apply.
  52       *
  53       *  * bypassDocumentValidation (boolean): If true, allows the write to
  54       *    circumvent document level validation.
  55       *
  56       *    For servers < 3.2, this option is ignored as document level validation
  57       *    is not available.
  58       *
  59       *  * collation (document): Collation specification.
  60       *
  61       *    This is not supported for server versions < 3.4 and will result in an
  62       *    exception at execution time if used.
  63       *
  64       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  65       *    run.
  66       *
  67       *  * projection (document): Limits the fields to return for the matching
  68       *    document.
  69       *
  70       *  * returnDocument (enum): Whether to return the document before or after
  71       *    the update is applied. Must be either
  72       *    FindOneAndUpdate::RETURN_DOCUMENT_BEFORE or
  73       *    FindOneAndUpdate::RETURN_DOCUMENT_AFTER. The default is
  74       *    FindOneAndUpdate::RETURN_DOCUMENT_BEFORE.
  75       *
  76       *  * session (MongoDB\Driver\Session): Client session.
  77       *
  78       *    Sessions are not supported for server versions < 3.6.
  79       *
  80       *  * sort (document): Determines which document the operation modifies if
  81       *    the query selects multiple documents.
  82       *
  83       *  * typeMap (array): Type map for BSON deserialization.
  84       *
  85       *  * upsert (boolean): When true, a new document is created if no document
  86       *    matches the query. The default is false.
  87       *
  88       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  89       *
  90       *    This is not supported for server versions < 3.2 and will result in an
  91       *    exception at execution time if used.
  92       *
  93       * @param string       $databaseName   Database name
  94       * @param string       $collectionName Collection name
  95       * @param array|object $filter         Query by which to filter documents
  96       * @param array|object $update         Update to apply to the matched document
  97       * @param array        $options        Command options
  98       * @throws InvalidArgumentException for parameter/option parsing errors
  99       */
 100      public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
 101      {
 102          if (! is_array($filter) && ! is_object($filter)) {
 103              throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
 104          }
 105  
 106          if (! is_array($update) && ! is_object($update)) {
 107              throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
 108          }
 109  
 110          if (! is_first_key_operator($update) && ! is_pipeline($update)) {
 111              throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
 112          }
 113  
 114          $options += [
 115              'returnDocument' => self::RETURN_DOCUMENT_BEFORE,
 116              'upsert' => false,
 117          ];
 118  
 119          if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
 120              throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
 121          }
 122  
 123          if (! is_integer($options['returnDocument'])) {
 124              throw InvalidArgumentException::invalidType('"returnDocument" option', $options['returnDocument'], 'integer');
 125          }
 126  
 127          if ($options['returnDocument'] !== self::RETURN_DOCUMENT_AFTER &&
 128              $options['returnDocument'] !== self::RETURN_DOCUMENT_BEFORE) {
 129              throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']);
 130          }
 131  
 132          if (isset($options['projection'])) {
 133              $options['fields'] = $options['projection'];
 134          }
 135  
 136          $options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER;
 137  
 138          unset($options['projection'], $options['returnDocument']);
 139  
 140          $this->findAndModify = new FindAndModify(
 141              $databaseName,
 142              $collectionName,
 143              ['query' => $filter, 'update' => $update] + $options
 144          );
 145      }
 146  
 147      /**
 148       * Execute the operation.
 149       *
 150       * @see Executable::execute()
 151       * @param Server $server
 152       * @return array|object|null
 153       * @throws UnsupportedException if collation or write concern is used and unsupported
 154       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 155       */
 156      public function execute(Server $server)
 157      {
 158          return $this->findAndModify->execute($server);
 159      }
 160  
 161      public function getCommandDocument(Server $server)
 162      {
 163          return $this->findAndModify->getCommandDocument($server);
 164      }
 165  }