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  use function array_search;
  26  
  27  /**
  28   * Index information model class.
  29   *
  30   * This class models the index information returned by the listIndexes command
  31   * or, for legacy servers, queries on the "system.indexes" collection. It
  32   * provides methods to access common index options, and allows access to other
  33   * options through the ArrayAccess interface (write methods are not supported).
  34   * For information on keys and index options, see the referenced
  35   * db.collection.createIndex() documentation.
  36   *
  37   * @api
  38   * @see \MongoDB\Collection::listIndexes()
  39   * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst
  40   * @see https://mongodb.com/docs/manual/reference/method/db.collection.createIndex/
  41   */
  42  class IndexInfo implements ArrayAccess
  43  {
  44      /** @var array */
  45      private $info;
  46  
  47      /**
  48       * @param array $info Index info
  49       */
  50      public function __construct(array $info)
  51      {
  52          $this->info = $info;
  53      }
  54  
  55      /**
  56       * Return the collection info as an array.
  57       *
  58       * @see https://php.net/oop5.magic#language.oop5.magic.debuginfo
  59       * @return array
  60       */
  61      public function __debugInfo()
  62      {
  63          return $this->info;
  64      }
  65  
  66      /**
  67       * Return the index name to allow casting IndexInfo to string.
  68       *
  69       * @return string
  70       */
  71      public function __toString()
  72      {
  73          return $this->getName();
  74      }
  75  
  76      /**
  77       * Return the index key.
  78       *
  79       * @return array
  80       */
  81      public function getKey()
  82      {
  83          return (array) $this->info['key'];
  84      }
  85  
  86      /**
  87       * Return the index name.
  88       *
  89       * @return string
  90       */
  91      public function getName()
  92      {
  93          return (string) $this->info['name'];
  94      }
  95  
  96      /**
  97       * Return the index namespace (e.g. "db.collection").
  98       *
  99       * @return string
 100       */
 101      public function getNamespace()
 102      {
 103          return (string) $this->info['ns'];
 104      }
 105  
 106      /**
 107       * Return the index version.
 108       *
 109       * @return integer
 110       */
 111      public function getVersion()
 112      {
 113          return (integer) $this->info['v'];
 114      }
 115  
 116      /**
 117       * Return whether or not this index is of type 2dsphere.
 118       *
 119       * @return boolean
 120       */
 121      public function is2dSphere()
 122      {
 123          return array_search('2dsphere', $this->getKey(), true) !== false;
 124      }
 125  
 126      /**
 127       * Return whether or not this index is of type geoHaystack.
 128       *
 129       * @return boolean
 130       */
 131      public function isGeoHaystack()
 132      {
 133          return array_search('geoHaystack', $this->getKey(), true) !== false;
 134      }
 135  
 136      /**
 137       * Return whether this is a sparse index.
 138       *
 139       * @see https://mongodb.com/docs/manual/core/index-sparse/
 140       * @return boolean
 141       */
 142      public function isSparse()
 143      {
 144          return ! empty($this->info['sparse']);
 145      }
 146  
 147      /**
 148       * Return whether or not this index is of type text.
 149       *
 150       * @return boolean
 151       */
 152      public function isText()
 153      {
 154          return array_search('text', $this->getKey(), true) !== false;
 155      }
 156  
 157      /**
 158       * Return whether this is a TTL index.
 159       *
 160       * @see https://mongodb.com/docs/manual/core/index-ttl/
 161       * @return boolean
 162       */
 163      public function isTtl()
 164      {
 165          return array_key_exists('expireAfterSeconds', $this->info);
 166      }
 167  
 168      /**
 169       * Return whether this is a unique index.
 170       *
 171       * @see https://mongodb.com/docs/manual/core/index-unique/
 172       * @return boolean
 173       */
 174      public function isUnique()
 175      {
 176          return ! empty($this->info['unique']);
 177      }
 178  
 179      /**
 180       * Check whether a field exists in the index information.
 181       *
 182       * @see https://php.net/arrayaccess.offsetexists
 183       * @param mixed $key
 184       * @return boolean
 185       */
 186      #[ReturnTypeWillChange]
 187      public function offsetExists($key)
 188      {
 189          return array_key_exists($key, $this->info);
 190      }
 191  
 192      /**
 193       * Return the field's value from the index information.
 194       *
 195       * This method satisfies the Enumerating Indexes specification's requirement
 196       * that index fields be made accessible under their original names. It may
 197       * also be used to access fields that do not have a helper method.
 198       *
 199       * @see https://php.net/arrayaccess.offsetget
 200       * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst#getting-full-index-information
 201       * @param mixed $key
 202       * @return mixed
 203       */
 204      #[ReturnTypeWillChange]
 205      public function offsetGet($key)
 206      {
 207          return $this->info[$key];
 208      }
 209  
 210      /**
 211       * Not supported.
 212       *
 213       * @see https://php.net/arrayaccess.offsetset
 214       * @param mixed $key
 215       * @param mixed $value
 216       * @throws BadMethodCallException
 217       * @return void
 218       */
 219      #[ReturnTypeWillChange]
 220      public function offsetSet($key, $value)
 221      {
 222          throw BadMethodCallException::classIsImmutable(self::class);
 223      }
 224  
 225      /**
 226       * Not supported.
 227       *
 228       * @see https://php.net/arrayaccess.offsetunset
 229       * @param mixed $key
 230       * @throws BadMethodCallException
 231       * @return void
 232       */
 233      #[ReturnTypeWillChange]
 234      public function offsetUnset($key)
 235      {
 236          throw BadMethodCallException::classIsImmutable(self::class);
 237      }
 238  }