Skip to content

Commit 26bb3a1

Browse files
committed
PHPLIB-1419 Encode Agg builder objects in Collection methods
1 parent be2df01 commit 26bb3a1

File tree

3 files changed

+312
-2
lines changed

3 files changed

+312
-2
lines changed

src/Collection.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use MongoDB\BSON\JavascriptInterface;
2424
use MongoDB\BSON\PackedArray;
2525
use MongoDB\Builder\BuilderEncoder;
26+
use MongoDB\Builder\Pipeline;
2627
use MongoDB\Codec\DocumentCodec;
2728
use MongoDB\Codec\Encoder;
2829
use MongoDB\Driver\CursorInterface;
@@ -221,8 +222,9 @@ public function __toString()
221222
* @throws InvalidArgumentException for parameter/option parsing errors
222223
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
223224
*/
224-
public function aggregate(array $pipeline, array $options = [])
225+
public function aggregate(array|Pipeline $pipeline, array $options = [])
225226
{
227+
$pipeline = $this->builderEncoder->encodeIfSupported($pipeline);
226228
$hasWriteStage = is_last_pipeline_operator_write($pipeline);
227229

228230
$options = $this->inheritReadPreference($options);
@@ -262,6 +264,7 @@ public function aggregate(array $pipeline, array $options = [])
262264
*/
263265
public function bulkWrite(array $operations, array $options = [])
264266
{
267+
$options = $this->inheritBuilderEncoder($options);
265268
$options = $this->inheritWriteOptions($options);
266269
$options = $this->inheritCodec($options);
267270

@@ -286,6 +289,7 @@ public function bulkWrite(array $operations, array $options = [])
286289
*/
287290
public function count(array|object $filter = [], array $options = [])
288291
{
292+
$filter = $this->builderEncoder->encodeIfSupported($filter);
289293
$options = $this->inheritReadOptions($options);
290294

291295
$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
@@ -307,6 +311,7 @@ public function count(array|object $filter = [], array $options = [])
307311
*/
308312
public function countDocuments(array|object $filter = [], array $options = [])
309313
{
314+
$filter = $this->builderEncoder->encodeIfSupported($filter);
310315
$options = $this->inheritReadOptions($options);
311316

312317
$operation = new CountDocuments($this->databaseName, $this->collectionName, $filter, $options);
@@ -444,6 +449,7 @@ public function createSearchIndexes(array $indexes, array $options = []): array
444449
*/
445450
public function deleteMany(array|object $filter, array $options = [])
446451
{
452+
$filter = $this->builderEncoder->encodeIfSupported($filter);
447453
$options = $this->inheritWriteOptions($options);
448454

449455
$operation = new DeleteMany($this->databaseName, $this->collectionName, $filter, $options);
@@ -465,6 +471,7 @@ public function deleteMany(array|object $filter, array $options = [])
465471
*/
466472
public function deleteOne(array|object $filter, array $options = [])
467473
{
474+
$filter = $this->builderEncoder->encodeIfSupported($filter);
468475
$options = $this->inheritWriteOptions($options);
469476

470477
$operation = new DeleteOne($this->databaseName, $this->collectionName, $filter, $options);
@@ -487,6 +494,7 @@ public function deleteOne(array|object $filter, array $options = [])
487494
*/
488495
public function distinct(string $fieldName, array|object $filter = [], array $options = [])
489496
{
497+
$filter = $this->builderEncoder->encodeIfSupported($filter);
490498
$options = $this->inheritReadOptions($options);
491499
$options = $this->inheritTypeMap($options);
492500

@@ -645,6 +653,7 @@ public function explain(Explainable $explainable, array $options = [])
645653
*/
646654
public function find(array|object $filter = [], array $options = [])
647655
{
656+
$filter = $this->builderEncoder->encodeIfSupported($filter);
648657
$options = $this->inheritReadOptions($options);
649658
$options = $this->inheritCodecOrTypeMap($options);
650659

@@ -667,6 +676,7 @@ public function find(array|object $filter = [], array $options = [])
667676
*/
668677
public function findOne(array|object $filter = [], array $options = [])
669678
{
679+
$filter = $this->builderEncoder->encodeIfSupported($filter);
670680
$options = $this->inheritReadOptions($options);
671681
$options = $this->inheritCodecOrTypeMap($options);
672682

@@ -692,6 +702,7 @@ public function findOne(array|object $filter = [], array $options = [])
692702
*/
693703
public function findOneAndDelete(array|object $filter, array $options = [])
694704
{
705+
$filter = $this->builderEncoder->encodeIfSupported($filter);
695706
$options = $this->inheritWriteOptions($options);
696707
$options = $this->inheritCodecOrTypeMap($options);
697708

@@ -722,6 +733,7 @@ public function findOneAndDelete(array|object $filter, array $options = [])
722733
*/
723734
public function findOneAndReplace(array|object $filter, array|object $replacement, array $options = [])
724735
{
736+
$filter = $this->builderEncoder->encodeIfSupported($filter);
725737
$options = $this->inheritWriteOptions($options);
726738
$options = $this->inheritCodecOrTypeMap($options);
727739

@@ -752,6 +764,7 @@ public function findOneAndReplace(array|object $filter, array|object $replacemen
752764
*/
753765
public function findOneAndUpdate(array|object $filter, array|object $update, array $options = [])
754766
{
767+
$filter = $this->builderEncoder->encodeIfSupported($filter);
755768
$options = $this->inheritWriteOptions($options);
756769
$options = $this->inheritCodecOrTypeMap($options);
757770

@@ -1000,6 +1013,7 @@ public function rename(string $toCollectionName, ?string $toDatabaseName = null,
10001013
*/
10011014
public function replaceOne(array|object $filter, array|object $replacement, array $options = [])
10021015
{
1016+
$filter = $this->builderEncoder->encodeIfSupported($filter);
10031017
$options = $this->inheritWriteOptions($options);
10041018
$options = $this->inheritCodec($options);
10051019

@@ -1023,6 +1037,8 @@ public function replaceOne(array|object $filter, array|object $replacement, arra
10231037
*/
10241038
public function updateMany(array|object $filter, array|object $update, array $options = [])
10251039
{
1040+
$filter = $this->builderEncoder->encodeIfSupported($filter);
1041+
$update = $this->builderEncoder->encodeIfSupported($update);
10261042
$options = $this->inheritWriteOptions($options);
10271043

10281044
$operation = new UpdateMany($this->databaseName, $this->collectionName, $filter, $update, $options);
@@ -1045,6 +1061,8 @@ public function updateMany(array|object $filter, array|object $update, array $op
10451061
*/
10461062
public function updateOne(array|object $filter, array|object $update, array $options = [])
10471063
{
1064+
$filter = $this->builderEncoder->encodeIfSupported($filter);
1065+
$update = $this->builderEncoder->encodeIfSupported($update);
10481066
$options = $this->inheritWriteOptions($options);
10491067

10501068
$operation = new UpdateOne($this->databaseName, $this->collectionName, $filter, $update, $options);
@@ -1080,8 +1098,9 @@ public function updateSearchIndex(string $name, array|object $definition, array
10801098
* @return ChangeStream
10811099
* @throws InvalidArgumentException for parameter/option parsing errors
10821100
*/
1083-
public function watch(array $pipeline = [], array $options = [])
1101+
public function watch(array|Pipeline $pipeline = [], array $options = [])
10841102
{
1103+
$pipeline = $this->builderEncoder->encodeIfSupported($pipeline);
10851104
$options = $this->inheritReadOptions($options);
10861105
$options = $this->inheritCodecOrTypeMap($options);
10871106

@@ -1112,6 +1131,13 @@ public function withOptions(array $options = [])
11121131
return new Collection($this->manager, $this->databaseName, $this->collectionName, $options);
11131132
}
11141133

1134+
private function inheritBuilderEncoder(array $options): array
1135+
{
1136+
$options['builderEncoder'] = $this->builderEncoder;
1137+
1138+
return $options;
1139+
}
1140+
11151141
private function inheritCodec(array $options): array
11161142
{
11171143
// If the options contain a type map, don't inherit anything

src/Operation/BulkWrite.php

+14
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
namespace MongoDB\Operation;
1919

20+
use MongoDB\Builder\BuilderEncoder;
2021
use MongoDB\BulkWriteResult;
2122
use MongoDB\Codec\DocumentCodec;
23+
use MongoDB\Codec\Encoder;
2224
use MongoDB\Driver\BulkWrite as Bulk;
2325
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2426
use MongoDB\Driver\Server;
@@ -94,6 +96,9 @@ class BulkWrite implements Executable
9496
*
9597
* Supported options for the bulk write operation:
9698
*
99+
* * builderEncoder (MongoDB\Builder\Encoder): Encoder for query and
100+
* aggregation builders. If not given, the default encoder will be used.
101+
*
97102
* * bypassDocumentValidation (boolean): If true, allows the write to
98103
* circumvent document level validation. The default is false.
99104
*
@@ -137,6 +142,10 @@ public function __construct(private string $databaseName, private string $collec
137142

138143
$options += ['ordered' => true];
139144

145+
if (isset($options['builderEncoder']) && ! $options['builderEncoder'] instanceof Encoder) {
146+
throw InvalidArgumentException::invalidType('"builderEncoder" option', $options['builderEncoder'], Encoder::class);
147+
}
148+
140149
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
141150
throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
142151
}
@@ -188,6 +197,8 @@ public function execute(Server $server)
188197
throw UnsupportedException::writeConcernNotSupportedInTransaction();
189198
}
190199

200+
$builderEncoder = $operation['builderEncoder'] ?? new BuilderEncoder();
201+
191202
$bulk = new Bulk($this->createBulkWriteOptions());
192203
$insertedIds = [];
193204

@@ -198,6 +209,7 @@ public function execute(Server $server)
198209
switch ($type) {
199210
case self::DELETE_MANY:
200211
case self::DELETE_ONE:
212+
$args[0] = $builderEncoder->encodeIfSupported($args[0]);
201213
$bulk->delete($args[0], $args[1]);
202214
break;
203215

@@ -208,6 +220,8 @@ public function execute(Server $server)
208220
case self::UPDATE_MANY:
209221
case self::UPDATE_ONE:
210222
case self::REPLACE_ONE:
223+
$args[0] = $builderEncoder->encodeIfSupported($args[0]);
224+
$args[1] = $builderEncoder->encodeIfSupported($args[1]);
211225
$bulk->update($args[0], $args[1], $args[2]);
212226
break;
213227
}

0 commit comments

Comments
 (0)