Commit 0c8b44f5 authored by Jeremy Mikola's avatar Jeremy Mikola

Validate Aggregation $pipeline before $options

parent 913c2a39
...@@ -59,6 +59,24 @@ class Aggregate implements Executable ...@@ -59,6 +59,24 @@ class Aggregate implements Executable
*/ */
public function __construct($databaseName, $collectionName, array $pipeline, array $options = array()) public function __construct($databaseName, $collectionName, array $pipeline, array $options = array())
{ {
if (empty($pipeline)) {
throw new InvalidArgumentException('$pipeline is empty');
}
$expectedIndex = 0;
foreach ($pipeline as $i => $operation) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
}
if ( ! is_array($operation) && ! is_object($operation)) {
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
}
$expectedIndex += 1;
}
$options += array( $options += array(
'allowDiskUse' => false, 'allowDiskUse' => false,
'useCursor' => true, 'useCursor' => true,
...@@ -84,20 +102,6 @@ class Aggregate implements Executable ...@@ -84,20 +102,6 @@ class Aggregate implements Executable
throw new InvalidArgumentException('"batchSize" option should not be used if "useCursor" is false'); throw new InvalidArgumentException('"batchSize" option should not be used if "useCursor" is false');
} }
$expectedIndex = 0;
foreach ($pipeline as $i => $op) {
if ($i !== $expectedIndex) {
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
}
if ( ! is_array($op) && ! is_object($op)) {
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $op, 'array or object');
}
$expectedIndex += 1;
}
$this->databaseName = (string) $databaseName; $this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName; $this->collectionName = (string) $collectionName;
$this->pipeline = $pipeline; $this->pipeline = $pipeline;
......
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