Commit 694c2117 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #27

parents ced71457 e223a19c
...@@ -14,8 +14,6 @@ use MongoDB\Operation\ListDatabases; ...@@ -14,8 +14,6 @@ use MongoDB\Operation\ListDatabases;
class Client class Client
{ {
private $manager; private $manager;
private $readPreference;
private $writeConcern;
/** /**
* Constructs a new Client instance. * Constructs a new Client instance.
...@@ -77,9 +75,8 @@ class Client ...@@ -77,9 +75,8 @@ class Client
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null) public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{ {
$namespace = $databaseName . '.' . $collectionName; $namespace = $databaseName . '.' . $collectionName;
// TODO: inherit from Manager options once PHPC-196 is implemented $writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
$writeConcern = $writeConcern ?: $this->writeConcern; $readPreference = $readPreference ?: $this->manager->getReadPreference();
$readPreference = $readPreference ?: $this->readPreference;
return new Collection($this->manager, $namespace, $writeConcern, $readPreference); return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
} }
...@@ -97,9 +94,8 @@ class Client ...@@ -97,9 +94,8 @@ class Client
*/ */
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null) public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{ {
// TODO: inherit from Manager options once PHPC-196 is implemented $writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
$writeConcern = $writeConcern ?: $this->writeConcern; $readPreference = $readPreference ?: $this->manager->getReadPreference();
$readPreference = $readPreference ?: $this->readPreference;
return new Database($this->manager, $databaseName, $writeConcern, $readPreference); return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
} }
......
...@@ -122,12 +122,8 @@ class Aggregate implements Executable ...@@ -122,12 +122,8 @@ class Aggregate implements Executable
return $cursor; return $cursor;
} }
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;
if (empty($result->ok)) { if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
......
...@@ -85,19 +85,18 @@ class Count implements Executable ...@@ -85,19 +85,18 @@ class Count 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('root' => 'array', 'document' => 'array'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { if (empty($result->ok)) {
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
// Older server versions may return a float // Older server versions may return a float
if ( ! isset($result['n']) || ! (is_integer($result['n']) || is_float($result['n']))) { if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
throw new UnexpectedValueException('count command did not return an "n" value'); throw new UnexpectedValueException('count command did not return a numeric "n" value');
} }
return (integer) $result['n']; return (integer) $result->n;
} }
/** /**
......
...@@ -102,12 +102,8 @@ class CreateCollection implements Executable ...@@ -102,12 +102,8 @@ 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' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;
if (empty($result->ok)) { if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
......
...@@ -91,11 +91,10 @@ class CreateIndexes implements Executable ...@@ -91,11 +91,10 @@ class CreateIndexes implements Executable
)); ));
$cursor = $server->executeCommand($this->databaseName, $command); $cursor = $server->executeCommand($this->databaseName, $command);
$cursor->setTypeMap(array('root' => 'array', 'document' => 'array'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
if (empty($result['ok'])) { if (empty($result->ok)) {
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
} }
......
...@@ -62,12 +62,8 @@ class Distinct implements Executable ...@@ -62,12 +62,8 @@ 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' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;
if (empty($result->ok)) { if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
......
...@@ -41,12 +41,8 @@ class DropCollection implements Executable ...@@ -41,12 +41,8 @@ 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' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;
if (empty($result->ok)) { if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
......
...@@ -39,12 +39,8 @@ class DropDatabase implements Executable ...@@ -39,12 +39,8 @@ 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' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;
if (empty($result->ok)) { if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
......
...@@ -56,12 +56,8 @@ class DropIndexes implements Executable ...@@ -56,12 +56,8 @@ 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());
// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;
if (empty($result->ok)) { if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
......
...@@ -118,12 +118,8 @@ class FindAndModify implements Executable ...@@ -118,12 +118,8 @@ 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' => 'stdClass'));
$result = current($cursor->toArray()); $result = current($cursor->toArray());
// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;
if (empty($result->ok)) { if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error'); throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
} }
......
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