|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Model; |
| 4 | + |
| 5 | +use ArrayAccess; |
| 6 | +use BadMethodCallException; |
| 7 | + |
| 8 | +/** |
| 9 | + * Index information model class. |
| 10 | + * |
| 11 | + * This class models the index information returned by the listIndexes command |
| 12 | + * or, for legacy servers, queries on the "system.indexes" collection. It |
| 13 | + * provides methods to access common index options, and allows access to other |
| 14 | + * options through the ArrayAccess interface (write methods are not supported). |
| 15 | + * For information on keys and index options, see the referenced |
| 16 | + * db.collection.createIndex() documentation. |
| 17 | + * |
| 18 | + * @api |
| 19 | + * @see MongoDB\Collection::listIndexes() |
| 20 | + * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst |
| 21 | + * @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/ |
| 22 | + */ |
| 23 | +class IndexInfo implements ArrayAccess |
| 24 | +{ |
| 25 | + private $info; |
| 26 | + |
| 27 | + /** |
| 28 | + * Constructor. |
| 29 | + * |
| 30 | + * @param array $info Index info |
| 31 | + */ |
| 32 | + public function __construct(array $info) |
| 33 | + { |
| 34 | + $this->info = $info; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Return the index key(s). |
| 39 | + * |
| 40 | + * @return array |
| 41 | + */ |
| 42 | + public function getKeys() |
| 43 | + { |
| 44 | + return (array) $this->info['key']; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Return the index name. |
| 49 | + * |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function getName() |
| 53 | + { |
| 54 | + return (string) $this->info['name']; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Return the index namespace (e.g. "db.collection"). |
| 59 | + * |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + public function getNamespace() |
| 63 | + { |
| 64 | + return (string) $this->info['ns']; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Return the index version. |
| 69 | + * |
| 70 | + * @return integer |
| 71 | + */ |
| 72 | + public function getVersion() |
| 73 | + { |
| 74 | + return (integer) $this->info['v']; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Return whether this is a sparse index. |
| 79 | + * |
| 80 | + * @see http://docs.mongodb.org/manual/core/index-sparse/ |
| 81 | + * @return boolean |
| 82 | + */ |
| 83 | + public function isSparse() |
| 84 | + { |
| 85 | + return ! empty($this->info['sparse']); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Return whether this is a TTL index. |
| 90 | + * |
| 91 | + * @see http://docs.mongodb.org/manual/core/index-ttl/ |
| 92 | + * @return boolean |
| 93 | + */ |
| 94 | + public function isTtl() |
| 95 | + { |
| 96 | + return array_key_exists('expireAfterSeconds', $this->info); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return whether this is a unique index. |
| 101 | + * |
| 102 | + * @see http://docs.mongodb.org/manual/core/index-unique/ |
| 103 | + * @return boolean |
| 104 | + */ |
| 105 | + public function isUnique() |
| 106 | + { |
| 107 | + return ! empty($this->info['unique']); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Check whether a field exists in the index information. |
| 112 | + * |
| 113 | + * @see http://php.net/arrayaccess.offsetexists |
| 114 | + * @param mixed $key |
| 115 | + * @return boolean |
| 116 | + */ |
| 117 | + public function offsetExists($key) |
| 118 | + { |
| 119 | + return array_key_exists($key, $this->info); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Return the field's value from the index information. |
| 124 | + * |
| 125 | + * This method satisfies the Enumerating Indexes specification's requirement |
| 126 | + * that index fields be made accessible under their original names. It may |
| 127 | + * also be used to access fields that do not have a helper method. |
| 128 | + * |
| 129 | + * @see http://php.net/arrayaccess.offsetget |
| 130 | + * @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst#getting-full-index-information |
| 131 | + * @param mixed $key |
| 132 | + * @return mixed |
| 133 | + */ |
| 134 | + public function offsetGet($key) |
| 135 | + { |
| 136 | + return $this->data[$key]; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Not supported. |
| 141 | + * |
| 142 | + * @see http://php.net/arrayaccess.offsetset |
| 143 | + * @throws BadMethodCallException IndexInfo is read-only |
| 144 | + */ |
| 145 | + public function offsetSet($key, $value) |
| 146 | + { |
| 147 | + throw new BadMethodCallException('IndexInfo is read-only'); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Not supported. |
| 152 | + * |
| 153 | + * @see http://php.net/arrayaccess.offsetunset |
| 154 | + * @throws BadMethodCallException IndexInfo is read-only |
| 155 | + */ |
| 156 | + public function offsetUnset($key) |
| 157 | + { |
| 158 | + throw new BadMethodCallException('IndexInfo is read-only'); |
| 159 | + } |
| 160 | +} |
0 commit comments