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