Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  /*
   3   * Copyright 2020-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   *   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 Iterator;
  21  use MongoDB\Command\ListCollections as ListCollectionsCommand;
  22  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  23  use MongoDB\Driver\Server;
  24  use MongoDB\Exception\InvalidArgumentException;
  25  use MongoDB\Model\CallbackIterator;
  26  
  27  /**
  28   * Operation for the listCollectionNames helper.
  29   *
  30   * @api
  31   * @see \MongoDB\Database::listCollectionNames()
  32   * @see http://docs.mongodb.org/manual/reference/command/listCollections/
  33   */
  34  class ListCollectionNames implements Executable
  35  {
  36      /** @var ListCollectionsCommand */
  37      private $listCollections;
  38  
  39      /**
  40       * Constructs a listCollections command.
  41       *
  42       * Supported options:
  43       *
  44       *  * filter (document): Query by which to filter collections.
  45       *
  46       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  47       *    run.
  48       *
  49       *  * session (MongoDB\Driver\Session): Client session.
  50       *
  51       *    Sessions are not supported for server versions < 3.6.
  52       *
  53       * @param string $databaseName Database name
  54       * @param array  $options      Command options
  55       * @throws InvalidArgumentException for parameter/option parsing errors
  56       */
  57      public function __construct($databaseName, array $options = [])
  58      {
  59          $this->listCollections = new ListCollectionsCommand($databaseName, ['nameOnly' => true] + $options);
  60      }
  61  
  62      /**
  63       * Execute the operation.
  64       *
  65       * @see Executable::execute()
  66       * @param Server $server
  67       * @return Iterator
  68       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
  69       */
  70      public function execute(Server $server) : Iterator
  71      {
  72          return new CallbackIterator(
  73              $this->listCollections->execute($server),
  74              function (array $collectionInfo) {
  75                  return $collectionInfo['name'];
  76              }
  77          );
  78      }
  79  }