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\Model;
  19  
  20  use ArrayAccess;
  21  use MongoDB\Exception\BadMethodCallException;
  22  use ReturnTypeWillChange;
  23  
  24  use function array_key_exists;
  25  
  26  /**
  27   * Collection information model class.
  28   *
  29   * This class models the collection information returned by the listCollections
  30   * command or, for legacy servers, queries on the "system.namespaces"
  31   * collection. It provides methods to access options for the collection.
  32   *
  33   * @api
  34   * @see \MongoDB\Database::listCollections()
  35   * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst
  36   */
  37  class CollectionInfo implements ArrayAccess
  38  {
  39      /** @var array */
  40      private $info;
  41  
  42      /**
  43       * @param array $info Collection info
  44       */
  45      public function __construct(array $info)
  46      {
  47          $this->info = $info;
  48      }
  49  
  50      /**
  51       * Return the collection info as an array.
  52       *
  53       * @see https://php.net/oop5.magic#language.oop5.magic.debuginfo
  54       * @return array
  55       */
  56      public function __debugInfo()
  57      {
  58          return $this->info;
  59      }
  60  
  61      /**
  62       * Return the maximum number of documents to keep in the capped collection.
  63       *
  64       * @deprecated 1.0 Deprecated in favor of using getOptions
  65       *
  66       * @return integer|null
  67       */
  68      public function getCappedMax()
  69      {
  70          /* The MongoDB server might return this number as an integer or float */
  71          return isset($this->info['options']['max']) ? (integer) $this->info['options']['max'] : null;
  72      }
  73  
  74      /**
  75       * Return the maximum size (in bytes) of the capped collection.
  76       *
  77       * @deprecated 1.0 Deprecated in favor of using getOptions
  78       *
  79       * @return integer|null
  80       */
  81      public function getCappedSize()
  82      {
  83          /* The MongoDB server might return this number as an integer or float */
  84          return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null;
  85      }
  86  
  87      /**
  88       * Return information about the _id index for the collection.
  89       */
  90      public function getIdIndex(): array
  91      {
  92          return (array) ($this->info['idIndex'] ?? []);
  93      }
  94  
  95      /**
  96       * Return the "info" property of the server response.
  97       *
  98       * @see https://mongodb.com/docs/manual/reference/command/listCollections/#output
  99       */
 100      public function getInfo(): array
 101      {
 102          return (array) ($this->info['info'] ?? []);
 103      }
 104  
 105      /**
 106       * Return the collection name.
 107       *
 108       * @see https://mongodb.com/docs/manual/reference/command/listCollections/#output
 109       * @return string
 110       */
 111      public function getName()
 112      {
 113          return (string) $this->info['name'];
 114      }
 115  
 116      /**
 117       * Return the collection options.
 118       *
 119       * @see https://mongodb.com/docs/manual/reference/command/listCollections/#output
 120       * @return array
 121       */
 122      public function getOptions()
 123      {
 124          return (array) ($this->info['options'] ?? []);
 125      }
 126  
 127      /**
 128       * Return the collection type.
 129       *
 130       * @see https://mongodb.com/docs/manual/reference/command/listCollections/#output
 131       */
 132      public function getType(): string
 133      {
 134          return (string) $this->info['type'];
 135      }
 136  
 137      /**
 138       * Return whether the collection is a capped collection.
 139       *
 140       * @deprecated 1.0 Deprecated in favor of using getOptions
 141       *
 142       * @return boolean
 143       */
 144      public function isCapped()
 145      {
 146          return ! empty($this->info['options']['capped']);
 147      }
 148  
 149      /**
 150       * Check whether a field exists in the collection information.
 151       *
 152       * @see https://php.net/arrayaccess.offsetexists
 153       * @param mixed $key
 154       * @return boolean
 155       */
 156      #[ReturnTypeWillChange]
 157      public function offsetExists($key)
 158      {
 159          return array_key_exists($key, $this->info);
 160      }
 161  
 162      /**
 163       * Return the field's value from the collection information.
 164       *
 165       * @see https://php.net/arrayaccess.offsetget
 166       * @param mixed $key
 167       * @return mixed
 168       */
 169      #[ReturnTypeWillChange]
 170      public function offsetGet($key)
 171      {
 172          return $this->info[$key];
 173      }
 174  
 175      /**
 176       * Not supported.
 177       *
 178       * @see https://php.net/arrayaccess.offsetset
 179       * @param mixed $key
 180       * @param mixed $value
 181       * @throws BadMethodCallException
 182       * @return void
 183       */
 184      #[ReturnTypeWillChange]
 185      public function offsetSet($key, $value)
 186      {
 187          throw BadMethodCallException::classIsImmutable(self::class);
 188      }
 189  
 190      /**
 191       * Not supported.
 192       *
 193       * @see https://php.net/arrayaccess.offsetunset
 194       * @param mixed $key
 195       * @throws BadMethodCallException
 196       * @return void
 197       */
 198      #[ReturnTypeWillChange]
 199      public function offsetUnset($key)
 200      {
 201          throw BadMethodCallException::classIsImmutable(self::class);
 202      }
 203  }