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 2015-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\Exception\RuntimeException as DriverRuntimeException;
  21  use MongoDB\Driver\ReadConcern;
  22  use MongoDB\Driver\ReadPreference;
  23  use MongoDB\Driver\Server;
  24  use MongoDB\Driver\Session;
  25  use MongoDB\Exception\InvalidArgumentException;
  26  use MongoDB\Exception\UnexpectedValueException;
  27  use MongoDB\Exception\UnsupportedException;
  28  
  29  use function array_intersect_key;
  30  use function is_integer;
  31  
  32  /**
  33   * Operation for obtaining an estimated count of documents in a collection
  34   *
  35   * @api
  36   * @see \MongoDB\Collection::estimatedDocumentCount()
  37   * @see https://mongodb.com/docs/manual/reference/command/count/
  38   */
  39  class EstimatedDocumentCount implements Executable, Explainable
  40  {
  41      /** @var string */
  42      private $databaseName;
  43  
  44      /** @var string */
  45      private $collectionName;
  46  
  47      /** @var array */
  48      private $options;
  49  
  50      /** @var int */
  51      private static $errorCodeCollectionNotFound = 26;
  52  
  53      /** @var int */
  54      private static $wireVersionForCollStats = 12;
  55  
  56      /**
  57       * Constructs a command to get the estimated number of documents in a
  58       * collection.
  59       *
  60       * Supported options:
  61       *
  62       *  * comment (mixed): BSON value to attach as a comment to this command.
  63       *
  64       *    This is not supported for servers versions < 4.4.
  65       *
  66       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  67       *    run.
  68       *
  69       *  * readConcern (MongoDB\Driver\ReadConcern): Read concern.
  70       *
  71       *  * readPreference (MongoDB\Driver\ReadPreference): Read preference.
  72       *
  73       *  * session (MongoDB\Driver\Session): Client session.
  74       *
  75       * @param string $databaseName   Database name
  76       * @param string $collectionName Collection name
  77       * @param array  $options        Command options
  78       * @throws InvalidArgumentException for parameter/option parsing errors
  79       */
  80      public function __construct(string $databaseName, string $collectionName, array $options = [])
  81      {
  82          $this->databaseName = $databaseName;
  83          $this->collectionName = $collectionName;
  84  
  85          if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
  86              throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
  87          }
  88  
  89          if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
  90              throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], ReadConcern::class);
  91          }
  92  
  93          if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
  94              throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], ReadPreference::class);
  95          }
  96  
  97          if (isset($options['session']) && ! $options['session'] instanceof Session) {
  98              throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
  99          }
 100  
 101          $this->options = array_intersect_key($options, ['comment' => 1, 'maxTimeMS' => 1, 'readConcern' => 1, 'readPreference' => 1, 'session' => 1]);
 102      }
 103  
 104      /**
 105       * Execute the operation.
 106       *
 107       * @see Executable::execute()
 108       * @return integer
 109       * @throws UnexpectedValueException if the command response was malformed
 110       * @throws UnsupportedException if collation or read concern is used and unsupported
 111       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 112       */
 113      public function execute(Server $server)
 114      {
 115          return $this->createCount()->execute($server);
 116      }
 117  
 118      /**
 119       * Returns the command document for this operation.
 120       *
 121       * @see Explainable::getCommandDocument()
 122       * @return array
 123       */
 124      public function getCommandDocument(Server $server)
 125      {
 126          return $this->createCount()->getCommandDocument($server);
 127      }
 128  
 129      private function createCount(): Count
 130      {
 131          return new Count($this->databaseName, $this->collectionName, [], $this->options);
 132      }
 133  }