Skip to content

PHPLIB-73: WC/RP inheritance and type map cleanup #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
class Client
{
private $manager;
private $readPreference;
private $writeConcern;

/**
* Constructs a new Client instance.
Expand Down Expand Up @@ -77,9 +75,8 @@ public function listDatabases(array $options = array())
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{
$namespace = $databaseName . '.' . $collectionName;
// TODO: inherit from Manager options once PHPC-196 is implemented
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;
$writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
$readPreference = $readPreference ?: $this->manager->getReadPreference();

return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
}
Expand All @@ -97,9 +94,8 @@ public function selectCollection($databaseName, $collectionName, WriteConcern $w
*/
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{
// TODO: inherit from Manager options once PHPC-196 is implemented
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;
$writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
$readPreference = $readPreference ?: $this->manager->getReadPreference();

return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
}
Expand Down
4 changes: 0 additions & 4 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,8 @@ public function execute(Server $server)
return $cursor;
}

$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray());

// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
Expand Down
11 changes: 5 additions & 6 deletions src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,18 @@ public function __construct($databaseName, $collectionName, array $filter = arra
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('root' => 'array', 'document' => 'array'));
$result = current($cursor->toArray());

if (empty($result['ok'])) {
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

// Older server versions may return a float
if ( ! isset($result['n']) || ! (is_integer($result['n']) || is_float($result['n']))) {
throw new UnexpectedValueException('count command did not return an "n" value');
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
throw new UnexpectedValueException('count command did not return a numeric "n" value');
}

return (integer) $result['n'];
return (integer) $result->n;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/Operation/CreateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,8 @@ public function __construct($databaseName, $collectionName, array $options = arr
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray());

// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
Expand Down
5 changes: 2 additions & 3 deletions src/Operation/CreateIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ private function executeCommand(Server $server)
));

$cursor = $server->executeCommand($this->databaseName, $command);
$cursor->setTypeMap(array('root' => 'array', 'document' => 'array'));
$result = current($cursor->toArray());

if (empty($result['ok'])) {
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ public function __construct($databaseName, $collectionName, $fieldName, array $f
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray());

// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
Expand Down
4 changes: 0 additions & 4 deletions src/Operation/DropCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ public function __construct($databaseName, $collectionName)
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray());

// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
Expand Down
4 changes: 0 additions & 4 deletions src/Operation/DropDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ public function __construct($databaseName)
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, new Command(array('dropDatabase' => 1)));
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray());

// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
Expand Down
4 changes: 0 additions & 4 deletions src/Operation/DropIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ public function execute(Server $server)
);

$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray());

// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
Expand Down
4 changes: 0 additions & 4 deletions src/Operation/FindAndModify.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,8 @@ public function __construct($databaseName, $collectionName, array $options)
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor->setTypeMap(array('document' => 'stdClass'));
$result = current($cursor->toArray());

// TODO: Remove this once PHPC-318 is implemented
is_array($result) and $result = (object) $result;

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
Expand Down