Skip to content

Commit 0c8b44f

Browse files
committed
Validate Aggregation $pipeline before $options
1 parent 913c2a3 commit 0c8b44f

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/Operation/Aggregate.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ class Aggregate implements Executable
5959
*/
6060
public function __construct($databaseName, $collectionName, array $pipeline, array $options = array())
6161
{
62+
if (empty($pipeline)) {
63+
throw new InvalidArgumentException('$pipeline is empty');
64+
}
65+
66+
$expectedIndex = 0;
67+
68+
foreach ($pipeline as $i => $operation) {
69+
if ($i !== $expectedIndex) {
70+
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
71+
}
72+
73+
if ( ! is_array($operation) && ! is_object($operation)) {
74+
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
75+
}
76+
77+
$expectedIndex += 1;
78+
}
79+
6280
$options += array(
6381
'allowDiskUse' => false,
6482
'useCursor' => true,
@@ -84,20 +102,6 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
84102
throw new InvalidArgumentException('"batchSize" option should not be used if "useCursor" is false');
85103
}
86104

87-
$expectedIndex = 0;
88-
89-
foreach ($pipeline as $i => $op) {
90-
if ($i !== $expectedIndex) {
91-
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
92-
}
93-
94-
if ( ! is_array($op) && ! is_object($op)) {
95-
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $op, 'array or object');
96-
}
97-
98-
$expectedIndex += 1;
99-
}
100-
101105
$this->databaseName = (string) $databaseName;
102106
$this->collectionName = (string) $collectionName;
103107
$this->pipeline = $pipeline;

0 commit comments

Comments
 (0)