Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 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\UnexpectedValueException;
  24  use MongoDB\Exception\UnsupportedException;
  25  use function array_intersect_key;
  26  
  27  /**
  28   * Operation for obtaining an estimated count of documents in a collection
  29   *
  30   * @api
  31   * @see \MongoDB\Collection::estimatedDocumentCount()
  32   * @see http://docs.mongodb.org/manual/reference/command/count/
  33   */
  34  class EstimatedDocumentCount implements Executable, Explainable
  35  {
  36      /** @var string */
  37      private $databaseName;
  38  
  39      /** @var string */
  40      private $collectionName;
  41  
  42      /** @var array */
  43      private $options;
  44  
  45      /** @var Count */
  46      private $count;
  47  
  48      /**
  49       * Constructs a count command.
  50       *
  51       * Supported options:
  52       *
  53       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  54       *    run.
  55       *
  56       *  * readConcern (MongoDB\Driver\ReadConcern): Read concern.
  57       *
  58       *    This is not supported for server versions < 3.2 and will result in an
  59       *    exception at execution time if used.
  60       *
  61       *  * readPreference (MongoDB\Driver\ReadPreference): Read preference.
  62       *
  63       *  * session (MongoDB\Driver\Session): Client session.
  64       *
  65       *    Sessions are not supported for server versions < 3.6.
  66       *
  67       * @param string $databaseName   Database name
  68       * @param string $collectionName Collection name
  69       * @param array  $options        Command options
  70       * @throws InvalidArgumentException for parameter/option parsing errors
  71       */
  72      public function __construct($databaseName, $collectionName, array $options = [])
  73      {
  74          $this->databaseName = (string) $databaseName;
  75          $this->collectionName = (string) $collectionName;
  76          $this->options = array_intersect_key($options, ['maxTimeMS' => 1, 'readConcern' => 1, 'readPreference' => 1, 'session' => 1]);
  77  
  78          $this->count = $this->createCount();
  79      }
  80  
  81      /**
  82       * Execute the operation.
  83       *
  84       * @see Executable::execute()
  85       * @param Server $server
  86       * @return integer
  87       * @throws UnexpectedValueException if the command response was malformed
  88       * @throws UnsupportedException if collation or read concern is used and unsupported
  89       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
  90       */
  91      public function execute(Server $server)
  92      {
  93          return $this->count->execute($server);
  94      }
  95  
  96      public function getCommandDocument(Server $server)
  97      {
  98          return $this->count->getCommandDocument($server);
  99      }
 100  
 101      /**
 102       * @return Count
 103       */
 104      private function createCount()
 105      {
 106          return new Count($this->databaseName, $this->collectionName, [], $this->options);
 107      }
 108  }