Commit 82de6ec1 authored by Jeremy Mikola's avatar Jeremy Mikola

Ensure operations return documents as objects by default

Unless the operation returns a scalar or custom value (e.g. Count, CreateIndexes), we should explicitly convert documents to stdClass instances to return. This will ultimately be made configurable once PHPLIB-112 is implemented.

A manual cast (of arrays to objects) was also necessary until PHPC-318 is implemented.
parent 771316b1
...@@ -122,20 +122,21 @@ class Aggregate implements Executable ...@@ -122,20 +122,21 @@ class Aggregate implements Executable
return $cursor; return $cursor;
} }
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
if ( ! isset($result['result']) || ! is_array($result['result'])) { if ( ! isset($result->result) || ! is_array($result->result)) {
throw new UnexpectedValueException('aggregate command did not return a "result" array'); throw new UnexpectedValueException('aggregate command did not return a "result" array');
} }
return new ArrayIterator(array_map( return new ArrayIterator($result->result);
function (stdClass $document) { return (array) $document; },
$result['result']
));
} }
/** /**
......
...@@ -106,11 +106,14 @@ class CreateCollection implements Executable ...@@ -106,11 +106,14 @@ class CreateCollection implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, $this->createCommand()); $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'array')); $cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
return $result; return $result;
......
...@@ -91,6 +91,7 @@ class CreateIndexes implements Executable ...@@ -91,6 +91,7 @@ class CreateIndexes implements Executable
)); ));
$cursor = $server->executeCommand($this->databaseName, $command); $cursor = $server->executeCommand($this->databaseName, $command);
$cursor->setTypeMap(array('document' => 'array'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { if (empty($result['ok'])) {
......
...@@ -62,18 +62,21 @@ class Distinct implements Executable ...@@ -62,18 +62,21 @@ class Distinct implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, $this->createCommand()); $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'array')); $cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
if ( ! isset($result['values']) || ! is_array($result['values'])) { if ( ! isset($result->values) || ! is_array($result->values)) {
throw new UnexpectedValueException('distinct command did not return a "values" array'); throw new UnexpectedValueException('distinct command did not return a "values" array');
} }
return $result['values']; return $result->values;
} }
/** /**
......
...@@ -41,11 +41,14 @@ class DropCollection implements Executable ...@@ -41,11 +41,14 @@ class DropCollection implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName))); $cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
$cursor->setTypeMap(array('document' => 'array')); $cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
return $result; return $result;
......
...@@ -39,11 +39,14 @@ class DropDatabase implements Executable ...@@ -39,11 +39,14 @@ class DropDatabase implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1))); $cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1)));
$cursor->setTypeMap(array('document' => 'array')); $cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
return $result; return $result;
......
...@@ -56,10 +56,14 @@ class DropIndexes implements Executable ...@@ -56,10 +56,14 @@ class DropIndexes implements Executable
); );
$cursor = $server->executeCommand($this->databaseName, new Command($cmd)); $cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
return $result; return $result;
......
...@@ -118,14 +118,17 @@ class FindAndModify implements Executable ...@@ -118,14 +118,17 @@ class FindAndModify implements Executable
public function execute(Server $server) public function execute(Server $server)
{ {
$cursor = $server->executeCommand($this->databaseName, $this->createCommand()); $cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'array')); $cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { // TODO: Remove this once PHPC-318 is implemented
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); is_array($result) and $result = (object) $result;
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
if ( ! isset($result['value'])) { if ( ! isset($result->value)) {
return null; return null;
} }
...@@ -134,17 +137,17 @@ class FindAndModify implements Executable ...@@ -134,17 +137,17 @@ class FindAndModify implements Executable
* requested. * requested.
*/ */
if ($this->options['upsert'] && ! $this->options['new'] && if ($this->options['upsert'] && ! $this->options['new'] &&
isset($result['lastErrorObject']->updatedExisting) && isset($result->lastErrorObject->updatedExisting) &&
! $result['lastErrorObject']->updatedExisting) { ! $result->lastErrorObject->updatedExisting) {
return null; return null;
} }
if ( ! is_array($result['value'])) { if ( ! is_object($result->value)) {
throw new UnexpectedValueException('findAndModify command did not return a "value" document'); throw new UnexpectedValueException('findAndModify command did not return a "value" document');
} }
return (object) $result['value']; return $result->value;
} }
/** /**
......
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