Skip to content

Commit 4f70901

Browse files
committed
Consolidate validation of aggregation pipeline options
1 parent 339b8a2 commit 4f70901

File tree

4 files changed

+8
-30
lines changed

4 files changed

+8
-30
lines changed

src/Operation/Aggregate.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
use function is_string;
4040
use function MongoDB\create_field_path_type_map;
4141
use function MongoDB\is_last_pipeline_operator_write;
42-
use function sprintf;
42+
use function MongoDB\is_pipeline;
4343

4444
/**
4545
* Operation for the aggregate command.
@@ -138,18 +138,8 @@ class Aggregate implements Executable, Explainable
138138
*/
139139
public function __construct(string $databaseName, ?string $collectionName, array $pipeline, array $options = [])
140140
{
141-
$expectedIndex = 0;
142-
143-
foreach ($pipeline as $i => $operation) {
144-
if ($i !== $expectedIndex) {
145-
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
146-
}
147-
148-
if (! is_array($operation) && ! is_object($operation)) {
149-
throw InvalidArgumentException::invalidType(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
150-
}
151-
152-
$expectedIndex += 1;
141+
if (! is_pipeline($pipeline)) {
142+
throw new InvalidArgumentException('$pipeline is not a valid list of pipeline operations');
153143
}
154144

155145
$options += ['useCursor' => true];

src/Operation/CreateCollection.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use function is_integer;
3131
use function is_object;
3232
use function is_string;
33-
use function sprintf;
33+
use function MongoDB\is_pipeline;
3434
use function trigger_error;
3535

3636
use const E_USER_DEPRECATED;
@@ -241,20 +241,8 @@ public function __construct(string $databaseName, string $collectionName, array
241241
trigger_error('The "autoIndexId" option is deprecated and will be removed in a future release', E_USER_DEPRECATED);
242242
}
243243

244-
if (isset($options['pipeline'])) {
245-
$expectedIndex = 0;
246-
247-
foreach ($options['pipeline'] as $i => $operation) {
248-
if ($i !== $expectedIndex) {
249-
throw new InvalidArgumentException(sprintf('The "pipeline" option is not a list (unexpected index: "%s")', $i));
250-
}
251-
252-
if (! is_array($operation) && ! is_object($operation)) {
253-
throw InvalidArgumentException::invalidType(sprintf('$options["pipeline"][%d]', $i), $operation, 'array or object');
254-
}
255-
256-
$expectedIndex += 1;
257-
}
244+
if (isset($options['pipeline']) && ! is_pipeline($options['pipeline'])) {
245+
throw new InvalidArgumentException('"pipeline" option is not a valid list of pipeline operations');
258246
}
259247

260248
$this->databaseName = $databaseName;

tests/Operation/AggregateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AggregateTest extends TestCase
1010
public function testConstructorPipelineArgumentMustBeAList(): void
1111
{
1212
$this->expectException(InvalidArgumentException::class);
13-
$this->expectExceptionMessage('$pipeline is not a list (unexpected index: "1")');
13+
$this->expectExceptionMessage('$pipeline is not a valid list of pipeline operations');
1414
new Aggregate($this->getDatabaseName(), $this->getCollectionName(), [1 => ['$match' => ['x' => 1]]]);
1515
}
1616

tests/Operation/CreateCollectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CreateCollectionTest extends TestCase
1010
public function testConstructorPipelineOptionMustBeAList(): void
1111
{
1212
$this->expectException(InvalidArgumentException::class);
13-
$this->expectExceptionMessage('The "pipeline" option is not a list (unexpected index: "1")');
13+
$this->expectExceptionMessage('"pipeline" option is not a valid list of pipeline operations');
1414
new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), ['pipeline' => [1 => ['$match' => ['x' => 1]]]]);
1515
}
1616

0 commit comments

Comments
 (0)