Skip to content

Commit 40fa724

Browse files
committed
PHPLIB-46: Index info and corresponding iterator class
1 parent f0ee8fb commit 40fa724

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

src/Model/IndexInfo.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
}

src/Model/IndexInfoIterator.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace MongoDB\Model;
4+
5+
use Iterator;
6+
7+
/**
8+
* IndexInfoIterator interface.
9+
*
10+
* This iterator is used for enumerating indexes in a collection.
11+
*
12+
* @api
13+
* @see MongoDB\Collection::listIndexes()
14+
*/
15+
interface IndexInfoIterator extends Iterator
16+
{
17+
/**
18+
* Return the current element as a IndexInfo instance.
19+
*
20+
* @return IndexInfo
21+
*/
22+
public function current();
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MongoDB\Model;
4+
5+
use IteratorIterator;
6+
7+
/**
8+
* IndexInfoIterator for both listIndexes command and legacy query results.
9+
*
10+
* This common iterator may be used to wrap a Cursor returned by both the
11+
* listIndexes command and, for legacy servers, queries on the "system.indexes"
12+
* collection.
13+
*
14+
* @internal
15+
* @see MongoDB\Collection::listIndexes()
16+
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst
17+
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
18+
* @see http://docs.mongodb.org/manual/reference/system-collections/
19+
*/
20+
class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIterator
21+
{
22+
/**
23+
* Return the current element as an IndexInfo instance.
24+
*
25+
* @see IndexInfoIterator::current()
26+
* @see http://php.net/iterator.current
27+
* @return IndexInfo
28+
*/
29+
public function current()
30+
{
31+
return new IndexInfo(parent::current());
32+
}
33+
}

0 commit comments

Comments
 (0)