Commit 72a19c94 authored by Katherine Walker's avatar Katherine Walker

PHPLIB-156: Add readPreference and session options for Explain

parent f6b1b49a
...@@ -18,7 +18,9 @@ ...@@ -18,7 +18,9 @@
namespace MongoDB\Operation; namespace MongoDB\Operation;
use MongoDB\Driver\Command; use MongoDB\Driver\Command;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server; use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Exception\UnsupportedException; use MongoDB\Exception\UnsupportedException;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\BSONDocument; use MongoDB\Model\BSONDocument;
...@@ -49,11 +51,15 @@ class Explain implements Executable ...@@ -49,11 +51,15 @@ class Explain implements Executable
* *
* Supported options: * Supported options:
* *
* * verbosity (string): The mode in which the explain command will be run. * * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * session (MongoDB\Driver\Session): Client session.
* *
* * typeMap (array): Type map for BSON deserialization. This will be used * * typeMap (array): Type map for BSON deserialization. This will be used
* used for the returned command result document. * used for the returned command result document.
* *
* * verbosity (string): The mode in which the explain command will be run.
*
* @param string $databaseName Database name * @param string $databaseName Database name
* @param Explainable $explainable Operation to explain * @param Explainable $explainable Operation to explain
* @param array $options Command options * @param array $options Command options
...@@ -61,14 +67,22 @@ class Explain implements Executable ...@@ -61,14 +67,22 @@ class Explain implements Executable
*/ */
public function __construct($databaseName, Explainable $explainable, array $options = []) public function __construct($databaseName, Explainable $explainable, array $options = [])
{ {
if (isset($options['verbosity']) && ! is_string($options['verbosity'])) { if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
throw InvalidArgumentException::invalidType('"verbosity" option', $options['verbosity'], 'string'); throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
}
if (isset($options['session']) && ! $options['session'] instanceof Session) {
throw InvalidArgumentException::invalidType('"session" option', $options['session'], 'MongoDB\Driver\Session');
} }
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) { if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array'); throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
} }
if (isset($options['verbosity']) && ! is_string($options['verbosity'])) {
throw InvalidArgumentException::invalidType('"verbosity" option', $options['verbosity'], 'string');
}
$this->databaseName = $databaseName; $this->databaseName = $databaseName;
$this->explainable = $explainable; $this->explainable = $explainable;
$this->options = $options; $this->options = $options;
...@@ -94,7 +108,7 @@ class Explain implements Executable ...@@ -94,7 +108,7 @@ class Explain implements Executable
$cmd['verbosity'] = $this->options['verbosity']; $cmd['verbosity'] = $this->options['verbosity'];
} }
$cursor = $server->executeCommand($this->databaseName, new Command($cmd)); $cursor = $server->executeCommand($this->databaseName, new Command($cmd), $this->createOptions());
if (isset($this->options['typeMap'])) { if (isset($this->options['typeMap'])) {
$cursor->setTypeMap($this->options['typeMap']); $cursor->setTypeMap($this->options['typeMap']);
...@@ -103,6 +117,27 @@ class Explain implements Executable ...@@ -103,6 +117,27 @@ class Explain implements Executable
return current($cursor->toArray()); return current($cursor->toArray());
} }
/**
* Create options for executing the command.
*
* @see http://php.net/manual/en/mongodb-driver-server.executecommand.php
* @return array
*/
private function createOptions()
{
$options = [];
if (isset($this->options['readPreference'])) {
$options['readPreference'] = $this->options['readPreference'];
}
if (isset($this->options['session'])) {
$options['session'] = $this->options['session'];
}
return $options;
}
private function isFindAndModify($explainable) private function isFindAndModify($explainable)
{ {
if ($explainable instanceof FindAndModify || $explainable instanceof FindOneAndDelete || $explainable instanceof FindOneAndReplace || $explainable instanceof FindOneAndUpdate) { if ($explainable instanceof FindAndModify || $explainable instanceof FindOneAndDelete || $explainable instanceof FindOneAndReplace || $explainable instanceof FindOneAndUpdate) {
......
...@@ -22,6 +22,14 @@ class ExplainTest extends TestCase ...@@ -22,6 +22,14 @@ class ExplainTest extends TestCase
{ {
$options = []; $options = [];
foreach ($this->getInvalidReadPreferenceValues() as $value) {
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidSessionValues() as $value) {
$options[][] = ['session' => $value];
}
foreach ($this->getInvalidStringValues() as $value) { foreach ($this->getInvalidStringValues() as $value) {
$options[][] = ['verbosity' => $value]; $options[][] = ['verbosity' => $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