Skip to content

Commit 0ffbae8

Browse files
committed
Handle new Cursor and toArray() API in extension
PHPC-224 consolidated the Result and Cursor classes into one. Additionally, PHPC-203 changed toArray() to return iterator_to_array($this) instead of only the first result document. For commands, this means we need to extract the first element in toArray()'s return value.
1 parent 92ee3c5 commit 0ffbae8

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed

src/Client.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace MongoDB;
44

55
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Cursor;
67
use MongoDB\Driver\Manager;
78
use MongoDB\Driver\ReadPreference;
8-
use MongoDB\Driver\Result;
99
use MongoDB\Driver\WriteConcern;
1010
use ArrayIterator;
1111
use stdClass;
@@ -39,7 +39,7 @@ public function __construct($uri, array $options = array(), array $driverOptions
3939
*
4040
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
4141
* @param string $databaseName
42-
* @return Result
42+
* @return Cursor
4343
*/
4444
public function dropDatabase($databaseName)
4545
{
@@ -61,9 +61,8 @@ public function listDatabases()
6161
{
6262
$command = new Command(array('listDatabases' => 1));
6363

64-
$result = $this->manager->executeCommand('admin', $command);
65-
$result = iterator_to_array($result);
66-
$result = current($result);
64+
$cursor = $this->manager->executeCommand('admin', $command);
65+
$result = current($cursor->toArray());
6766

6867
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
6968
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');

src/Collection.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace MongoDB;
44

55
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Cursor;
67
use MongoDB\Driver\Manager;
78
use MongoDB\Driver\Query;
89
use MongoDB\Driver\ReadPreference;
9-
use MongoDB\Driver\Result;
1010
use MongoDB\Driver\BulkWrite;
1111
use MongoDB\Driver\WriteConcern;
1212

@@ -86,14 +86,16 @@ public function aggregate(array $pipeline, array $options = array())
8686
"pipeline" => $pipeline,
8787
) + $options;
8888

89-
$result = $this->_runCommand($this->dbname, $cmd);
90-
$doc = $result->toArray();
89+
$cursor = $this->_runCommand($this->dbname, $cmd);
90+
9191
if (isset($cmd["cursor"]) && $cmd["cursor"]) {
92-
return $result;
93-
} else {
94-
if ($doc["ok"]) {
95-
return new \ArrayIterator($doc["result"]);
96-
}
92+
return $cursor;
93+
}
94+
95+
$doc = current($cursor->toArray());
96+
97+
if ($doc["ok"]) {
98+
return new \ArrayIterator($doc["result"]);
9799
}
98100

99101
throw $this->_generateCommandException($doc);
@@ -234,7 +236,7 @@ public function count(array $filter = array(), array $options = array())
234236
"query" => $filter,
235237
) + $options;
236238

237-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
239+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
238240
if ($doc["ok"]) {
239241
return $doc["n"];
240242
}
@@ -324,7 +326,7 @@ public function distinct($fieldName, array $filter = array(), array $options = a
324326
"query" => $filter,
325327
) + $options;
326328

327-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
329+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
328330
if ($doc["ok"]) {
329331
return $doc["values"];
330332
}
@@ -335,7 +337,7 @@ public function distinct($fieldName, array $filter = array(), array $options = a
335337
* Drop this collection.
336338
*
337339
* @see http://docs.mongodb.org/manual/reference/command/drop/
338-
* @return Result
340+
* @return Cursor
339341
*/
340342
public function drop()
341343
{
@@ -351,7 +353,7 @@ public function drop()
351353
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
352354
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
353355
* @param string $indexName
354-
* @return Result
356+
* @return Cursor
355357
* @throws InvalidArgumentException if "*" is specified
356358
*/
357359
public function dropIndex($indexName)
@@ -364,7 +366,7 @@ public function dropIndex($indexName)
364366
*
365367
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
366368
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
367-
* @return Result
369+
* @return Cursor
368370
*/
369371
public function dropIndexes()
370372
{
@@ -379,7 +381,7 @@ public function dropIndexes()
379381
*
380382
* @param array $filter The find query to execute
381383
* @param array $options Additional options
382-
* @return Result
384+
* @return Cursor
383385
*/
384386
public function find(array $filter = array(), array $options = array())
385387
{
@@ -437,7 +439,7 @@ public function findOneAndDelete(array $filter, array $options = array())
437439
"query" => $filter,
438440
) + $options;
439441

440-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
442+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
441443
if ($doc["ok"]) {
442444
return $doc["value"];
443445
}
@@ -474,7 +476,7 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
474476
"query" => $filter,
475477
) + $options;
476478

477-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
479+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
478480
if ($doc["ok"]) {
479481
return $doc["value"];
480482
}
@@ -512,7 +514,7 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
512514
"query" => $filter,
513515
) + $options;
514516

515-
$doc = $this->_runCommand($this->dbname, $cmd)->toArray();
517+
$doc = current($this->_runCommand($this->dbname, $cmd)->toArray());
516518
if ($doc["ok"]) {
517519
return $doc["value"];
518520
}
@@ -951,7 +953,7 @@ public function insertOne(array $document)
951953
*
952954
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
953955
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
954-
* @return Result
956+
* @return Cursor
955957
*/
956958
public function listIndexes()
957959
{

src/Database.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use MongoDB\Collection;
66
use MongoDB\Driver\Command;
7+
use MongoDB\Driver\Cursor;
78
use MongoDB\Driver\Manager;
89
use MongoDB\Driver\Query;
910
use MongoDB\Driver\ReadPreference;
10-
use MongoDB\Driver\Result;
1111
use MongoDB\Driver\WriteConcern;
1212
use MongoDB\Model\CollectionInfoIterator;
1313
use MongoDB\Model\CollectionInfoCommandIterator;
@@ -47,7 +47,7 @@ public function __construct(Manager $manager, $databaseName, WriteConcern $write
4747
* @see http://docs.mongodb.org/manual/reference/method/db.createCollection/
4848
* @param string $collectionName
4949
* @param array $options
50-
* @return Result
50+
* @return Cursor
5151
*/
5252
public function createCollection($collectionName, array $options = array())
5353
{
@@ -58,7 +58,7 @@ public function createCollection($collectionName, array $options = array())
5858
* Drop this database.
5959
*
6060
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
61-
* @return Result
61+
* @return Cursor
6262
*/
6363
public function drop()
6464
{
@@ -73,7 +73,7 @@ public function drop()
7373
*
7474
* @see http://docs.mongodb.org/manual/reference/command/drop/
7575
* @param string $collectionName
76-
* @return Result
76+
* @return Cursor
7777
*/
7878
public function dropCollection($collectionName)
7979
{
@@ -130,9 +130,9 @@ private function listCollectionsCommand(array $options = array())
130130
// TODO: Relax RP if connected to a secondary node in standalone mode
131131
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
132132

133-
$result = $this->manager->executeCommand($this->databaseName, $command, $readPreference);
133+
$cursor = $this->manager->executeCommand($this->databaseName, $command, $readPreference);
134134

135-
return new CollectionInfoCommandIterator($result);
135+
return new CollectionInfoCommandIterator($cursor);
136136
}
137137

138138
/**
@@ -166,8 +166,8 @@ private function listCollectionsLegacy(array $options = array())
166166
// TODO: Relax RP if connected to a secondary node in standalone mode
167167
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
168168

169-
$result = $this->manager->executeQuery($namespace, $query, $readPreference);
169+
$cursor = $this->manager->executeQuery($namespace, $query, $readPreference);
170170

171-
return new CollectionInfoLegacyIterator($result);
171+
return new CollectionInfoLegacyIterator($cursor);
172172
}
173173
}

tests/CollectionFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function testInsertAndRetrieve()
4949
$count = $this->collection->count($query);
5050
$this->assertEquals(1, $count);
5151
$cursor = $this->collection->find($query);
52-
$this->assertInstanceOf('MongoDB\Driver\Result', $cursor);
52+
$this->assertInstanceOf('MongoDB\Driver\Cursor', $cursor);
5353

5454
foreach($cursor as $n => $person) {
5555
$this->assertInternalType("array", $person);

tests/FunctionalTestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Manager;
7-
use MongoDB\Driver\Result;
7+
use MongoDB\Driver\Cursor;
88

99
abstract class FunctionalTestCase extends TestCase
1010
{
@@ -19,16 +19,16 @@ public function assertCollectionCount($namespace, $count)
1919
{
2020
list($databaseName, $collectionName) = explode('.', $namespace, 2);
2121

22-
$result = $this->manager->executeCommand($databaseName, new Command(array('count' => $collectionName)));
22+
$cursor = $this->manager->executeCommand($databaseName, new Command(array('count' => $collectionName)));
2323

24-
$document = $result->toArray();
24+
$document = current($cursor->toArray());
2525
$this->assertArrayHasKey('n', $document);
2626
$this->assertEquals($count, $document['n']);
2727
}
2828

29-
public function assertCommandSucceeded(Result $result)
29+
public function assertCommandSucceeded(Cursor $cursor)
3030
{
31-
$document = $result->toArray();
31+
$document = current($cursor->toArray());
3232
$this->assertArrayHasKey('ok', $document);
3333
$this->assertEquals(1, $document['ok']);
3434
}

0 commit comments

Comments
 (0)