Commit 7191886d authored by Jeremy Mikola's avatar Jeremy Mikola

Use type map for database and collection enumeration

This obviates the need to handle stdClass instances within the results.
parent 982889cb
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
{ "name": "Derick Rethans", "email": "github@derickrethans.nl" } { "name": "Derick Rethans", "email": "github@derickrethans.nl" }
], ],
"require": { "require": {
"ext-mongodb": "*" "ext-mongodb": ">=0.5.0"
}, },
"require-dev": { "require-dev": {
"fzaninotto/faker": "~1.0" "fzaninotto/faker": "~1.0"
......
...@@ -63,24 +63,20 @@ class Client ...@@ -63,24 +63,20 @@ class Client
$command = new Command(array('listDatabases' => 1)); $command = new Command(array('listDatabases' => 1));
$cursor = $this->manager->executeCommand('admin', $command); $cursor = $this->manager->executeCommand('admin', $command);
$cursor->setTypeMap(array('document' => 'array'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if ( ! isset($result['databases']) || ! is_array($result['databases'])) { if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
throw new UnexpectedValueException('listDatabases command did not return a "databases" array'); throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
} }
$databases = array_map(
function(stdClass $database) { return (array) $database; },
$result['databases']
);
/* Return an Iterator instead of an array in case listDatabases is /* Return an Iterator instead of an array in case listDatabases is
* eventually changed to return a command cursor, like the collection * eventually changed to return a command cursor, like the collection
* and index enumeration commands. This makes the "totalSize" command * and index enumeration commands. This makes the "totalSize" command
* field inaccessible, but users can manually invoke the command if they * field inaccessible, but users can manually invoke the command if they
* need that value. * need that value.
*/ */
return new DatabaseInfoLegacyIterator($databases); return new DatabaseInfoLegacyIterator($result['databases']);
} }
/** /**
......
...@@ -141,6 +141,7 @@ class Database ...@@ -141,6 +141,7 @@ class Database
{ {
$command = new Command(array('listCollections' => 1) + $options); $command = new Command(array('listCollections' => 1) + $options);
$cursor = $server->executeCommand($this->databaseName, $command); $cursor = $server->executeCommand($this->databaseName, $command);
$cursor->setTypeMap(array('document' => 'array'));
return new CollectionInfoCommandIterator($cursor); return new CollectionInfoCommandIterator($cursor);
} }
...@@ -177,6 +178,7 @@ class Database ...@@ -177,6 +178,7 @@ class Database
$namespace = $this->databaseName . '.system.namespaces'; $namespace = $this->databaseName . '.system.namespaces';
$query = new Query($filter); $query = new Query($filter);
$cursor = $server->executeQuery($namespace, $query); $cursor = $server->executeQuery($namespace, $query);
$cursor->setTypeMap(array('document' => 'array'));
return new CollectionInfoLegacyIterator($cursor); return new CollectionInfoLegacyIterator($cursor);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment