Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  /*
   3   * Copyright 2018-present 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   *   https://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\Command;
  21  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  22  use MongoDB\Driver\Server;
  23  use MongoDB\Driver\Session;
  24  use MongoDB\Driver\WriteConcern;
  25  use MongoDB\Exception\InvalidArgumentException;
  26  
  27  use function current;
  28  use function is_array;
  29  
  30  /**
  31   * Operation for the collMod command.
  32   *
  33   * @api
  34   * @see \MongoDB\Database::modifyCollection()
  35   * @see https://mongodb.com/docs/manual/reference/command/collMod/
  36   */
  37  class ModifyCollection implements Executable
  38  {
  39      /** @var string */
  40      private $databaseName;
  41  
  42      /** @var string */
  43      private $collectionName;
  44  
  45      /** @var array */
  46      private $collectionOptions;
  47  
  48      /** @var array */
  49      private $options;
  50  
  51      /**
  52       * Constructs a collMod command.
  53       *
  54       * Supported options:
  55       *
  56       *  * comment (mixed): BSON value to attach as a comment to this command.
  57       *
  58       *    This is not supported for servers versions < 4.4.
  59       *
  60       *  * session (MongoDB\Driver\Session): Client session.
  61       *
  62       *  * typeMap (array): Type map for BSON deserialization. This will only be
  63       *    used for the returned command result document.
  64       *
  65       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  66       *
  67       * @param string $databaseName      Database name
  68       * @param string $collectionName    Collection or view to modify
  69       * @param array  $collectionOptions Collection or view options to assign
  70       * @param array  $options           Command options
  71       * @throws InvalidArgumentException for parameter/option parsing errors
  72       */
  73      public function __construct(string $databaseName, string $collectionName, array $collectionOptions, array $options = [])
  74      {
  75          if (empty($collectionOptions)) {
  76              throw new InvalidArgumentException('$collectionOptions is empty');
  77          }
  78  
  79          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  80              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  81          }
  82  
  83          if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
  84              throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
  85          }
  86  
  87          if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
  88              throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
  89          }
  90  
  91          if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
  92              unset($options['writeConcern']);
  93          }
  94  
  95          $this->databaseName = $databaseName;
  96          $this->collectionName = $collectionName;
  97          $this->collectionOptions = $collectionOptions;
  98          $this->options = $options;
  99      }
 100  
 101      /**
 102       * Execute the operation.
 103       *
 104       * @see Executable::execute()
 105       * @return array|object Command result document
 106       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 107       */
 108      public function execute(Server $server)
 109      {
 110          $cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
 111  
 112          if (isset($this->options['typeMap'])) {
 113              $cursor->setTypeMap($this->options['typeMap']);
 114          }
 115  
 116          return current($cursor->toArray());
 117      }
 118  
 119      private function createCommand(): Command
 120      {
 121          $cmd = ['collMod' => $this->collectionName] + $this->collectionOptions;
 122  
 123          if (isset($this->options['comment'])) {
 124              $cmd['comment'] = $this->options['comment'];
 125          }
 126  
 127          return new Command($cmd);
 128      }
 129  
 130      /**
 131       * Create options for executing the command.
 132       *
 133       * @see https://php.net/manual/en/mongodb-driver-server.executewritecommand.php
 134       */
 135      private function createOptions(): array
 136      {
 137          $options = [];
 138  
 139          if (isset($this->options['session'])) {
 140              $options['session'] = $this->options['session'];
 141          }
 142  
 143          if (isset($this->options['writeConcern'])) {
 144              $options['writeConcern'] = $this->options['writeConcern'];
 145          }
 146  
 147          return $options;
 148      }
 149  }