Skip to content

Commit 3b98d88

Browse files
committed
PHPLIB-675 Expose additional methods to access collection info
1 parent bf6da6c commit 3b98d88

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

src/Model/CollectionInfo.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function __debugInfo()
5959
/**
6060
* Return the maximum number of documents to keep in the capped collection.
6161
*
62+
* @deprecated Deprecated in favor of using getOptions
63+
*
6264
* @return integer|null
6365
*/
6466
public function getCappedMax()
@@ -70,6 +72,8 @@ public function getCappedMax()
7072
/**
7173
* Return the maximum size (in bytes) of the capped collection.
7274
*
75+
* @deprecated Deprecated in favor of using getOptions
76+
*
7377
* @return integer|null
7478
*/
7579
public function getCappedSize()
@@ -78,9 +82,31 @@ public function getCappedSize()
7882
return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null;
7983
}
8084

85+
/**
86+
* Return information about the _id index for the collection.
87+
*
88+
* @return array
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://docs.mongodb.com/manual/reference/command/listCollections/#output
99+
* @return array
100+
*/
101+
public function getInfo() : array
102+
{
103+
return (array) ($this->info['info'] ?? []);
104+
}
105+
81106
/**
82107
* Return the collection name.
83108
*
109+
* @see https://docs.mongodb.com/manual/reference/command/listCollections/#output
84110
* @return string
85111
*/
86112
public function getName()
@@ -91,16 +117,30 @@ public function getName()
91117
/**
92118
* Return the collection options.
93119
*
120+
* @see https://docs.mongodb.com/manual/reference/command/listCollections/#output
94121
* @return array
95122
*/
96123
public function getOptions()
97124
{
98-
return isset($this->info['options']) ? (array) $this->info['options'] : [];
125+
return (array) ($this->info['options'] ?? []);
126+
}
127+
128+
/**
129+
* Return the collection type.
130+
*
131+
* @see https://docs.mongodb.com/manual/reference/command/listCollections/#output
132+
* @return string
133+
*/
134+
public function getType() : string
135+
{
136+
return (string) $this->info['type'];
99137
}
100138

101139
/**
102140
* Return whether the collection is a capped collection.
103141
*
142+
* @deprecated Deprecated in favor of using getOptions
143+
*
104144
* @return boolean
105145
*/
106146
public function isCapped()

tests/Model/CollectionInfoTest.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,47 @@
88

99
class CollectionInfoTest extends TestCase
1010
{
11-
public function testGetName()
11+
public function testGetBasicInformation() : void
1212
{
13-
$info = new CollectionInfo(['name' => 'foo']);
13+
$info = new CollectionInfo([
14+
'name' => 'foo',
15+
'type' => 'view',
16+
'options' => ['capped' => true, 'size' => 1048576],
17+
'info' => ['readOnly' => true],
18+
'idIndex' => ['idIndex' => true], // Dummy option
19+
]);
20+
1421
$this->assertSame('foo', $info->getName());
22+
$this->assertSame('foo', $info['name']);
23+
24+
$this->assertSame('view', $info->getType());
25+
$this->assertSame('view', $info['type']);
26+
27+
$this->assertSame(['capped' => true, 'size' => 1048576], $info->getOptions());
28+
$this->assertSame(['capped' => true, 'size' => 1048576], $info['options']);
29+
30+
$this->assertSame(['readOnly' => true], $info->getInfo());
31+
$this->assertSame(['readOnly' => true], $info['info']);
32+
33+
$this->assertSame(['idIndex' => true], $info->getIdIndex());
34+
$this->assertSame(['idIndex' => true], $info['idIndex']);
1535
}
1636

17-
public function testGetOptions()
37+
public function testMissingFields() : void
1838
{
19-
$info = new CollectionInfo(['name' => 'foo']);
39+
$info = new CollectionInfo([
40+
'name' => 'foo',
41+
'type' => 'view',
42+
]);
43+
2044
$this->assertSame([], $info->getOptions());
45+
$this->assertFalse(isset($info['options']));
2146

22-
$info = new CollectionInfo(['name' => 'foo', 'options' => ['capped' => true, 'size' => 1048576]]);
23-
$this->assertSame(['capped' => true, 'size' => 1048576], $info->getOptions());
47+
$this->assertSame([], $info->getInfo());
48+
$this->assertFalse(isset($info['info']));
49+
50+
$this->assertSame([], $info->getIdIndex());
51+
$this->assertFalse(isset($info['idIndex']));
2452
}
2553

2654
public function testCappedCollectionMethods()

0 commit comments

Comments
 (0)