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_object;
  26  
  27  /**
  28   * Operation for deleting a document with the findAndModify command.
  29   *
  30   * @api
  31   * @see \MongoDB\Collection::findOneAndDelete()
  32   * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
  33   */
  34  class FindOneAndDelete implements Executable, Explainable
  35  {
  36      /** @var FindAndModify */
  37      private $findAndModify;
  38  
  39      /**
  40       * Constructs a findAndModify command for deleting a document.
  41       *
  42       * Supported options:
  43       *
  44       *  * collation (document): Collation specification.
  45       *
  46       *    This is not supported for server versions < 3.4 and will result in an
  47       *    exception at execution time if used.
  48       *
  49       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  50       *    run.
  51       *
  52       *  * projection (document): Limits the fields to return for the matching
  53       *    document.
  54       *
  55       *  * session (MongoDB\Driver\Session): Client session.
  56       *
  57       *    Sessions are not supported for server versions < 3.6.
  58       *
  59       *  * sort (document): Determines which document the operation modifies if
  60       *    the query selects multiple documents.
  61       *
  62       *  * typeMap (array): Type map for BSON deserialization.
  63       *
  64       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  65       *
  66       *    This is not supported for server versions < 3.2 and will result in an
  67       *    exception at execution time if used.
  68       *
  69       * @param string       $databaseName   Database name
  70       * @param string       $collectionName Collection name
  71       * @param array|object $filter         Query by which to filter documents
  72       * @param array        $options        Command options
  73       * @throws InvalidArgumentException for parameter/option parsing errors
  74       */
  75      public function __construct($databaseName, $collectionName, $filter, array $options = [])
  76      {
  77          if (! is_array($filter) && ! is_object($filter)) {
  78              throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
  79          }
  80  
  81          if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
  82              throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
  83          }
  84  
  85          if (isset($options['projection'])) {
  86              $options['fields'] = $options['projection'];
  87          }
  88  
  89          unset($options['projection']);
  90  
  91          $this->findAndModify = new FindAndModify(
  92              $databaseName,
  93              $collectionName,
  94              ['query' => $filter, 'remove' => true] + $options
  95          );
  96      }
  97  
  98      /**
  99       * Execute the operation.
 100       *
 101       * @see Executable::execute()
 102       * @param Server $server
 103       * @return array|object|null
 104       * @throws UnsupportedException if collation or write concern is used and unsupported
 105       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 106       */
 107      public function execute(Server $server)
 108      {
 109          return $this->findAndModify->execute($server);
 110      }
 111  
 112      public function getCommandDocument(Server $server)
 113      {
 114          return $this->findAndModify->getCommandDocument($server);
 115      }
 116  }