Skip to content

Commit 5da5c15

Browse files
authored
PHPLIB-1420 Integrate query builder for non-aggregation APIs (#1385)
1 parent 5ebc5dc commit 5da5c15

File tree

4 files changed

+303
-4
lines changed

4 files changed

+303
-4
lines changed

psalm-baseline.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,9 @@
417417
<code><![CDATA[$args[0]]]></code>
418418
<code><![CDATA[$args[0]]]></code>
419419
<code><![CDATA[$args[0]]]></code>
420-
<code><![CDATA[$args[1]]]></code>
421-
<code><![CDATA[$args[1]]]></code>
420+
<code><![CDATA[$args[0]]]></code>
421+
<code><![CDATA[$args[0]]]></code>
422+
<code><![CDATA[$args[0]]]></code>
422423
<code><![CDATA[$args[1]]]></code>
423424
<code><![CDATA[$args[1]]]></code>
424425
<code><![CDATA[$args[1]]]></code>
@@ -448,6 +449,7 @@
448449
<code><![CDATA[$args[1]]]></code>
449450
<code><![CDATA[$args[1]]]></code>
450451
<code><![CDATA[$args[1]]]></code>
452+
<code><![CDATA[$args[1]]]></code>
451453
<code><![CDATA[$args[1]['limit']]]></code>
452454
<code><![CDATA[$args[2]]]></code>
453455
<code><![CDATA[$args[2]]]></code>
@@ -458,6 +460,10 @@
458460
<code><![CDATA[$args[2]['multi']]]></code>
459461
<code><![CDATA[$args[2]['multi']]]></code>
460462
<code><![CDATA[$operations[$i][$type][0]]]></code>
463+
<code><![CDATA[$operations[$i][$type][0]]]></code>
464+
<code><![CDATA[$operations[$i][$type][0]]]></code>
465+
<code><![CDATA[$operations[$i][$type][0]]]></code>
466+
<code><![CDATA[$operations[$i][$type][1]]]></code>
461467
<code><![CDATA[$operations[$i][$type][1]]]></code>
462468
<code><![CDATA[$operations[$i][$type][1]]]></code>
463469
<code><![CDATA[$operations[$i][$type][2]]]></code>
@@ -466,9 +472,14 @@
466472
<MixedAssignment>
467473
<code><![CDATA[$args]]></code>
468474
<code><![CDATA[$args]]></code>
475+
<code><![CDATA[$args[1]]]></code>
469476
<code><![CDATA[$args[2]]]></code>
470477
<code><![CDATA[$args[2]]]></code>
471478
<code><![CDATA[$insertedIds[$i]]]></code>
479+
<code><![CDATA[$operations[$i][$type][0]]]></code>
480+
<code><![CDATA[$operations[$i][$type][0]]]></code>
481+
<code><![CDATA[$operations[$i][$type][0]]]></code>
482+
<code><![CDATA[$operations[$i][$type][1]]]></code>
472483
<code><![CDATA[$operations[$i][$type][1]]]></code>
473484
<code><![CDATA[$operations[$i][$type][2]]]></code>
474485
<code><![CDATA[$operations[$i][$type][2]]]></code>

src/Collection.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public function aggregate(array $pipeline, array $options = [])
262262
*/
263263
public function bulkWrite(array $operations, array $options = [])
264264
{
265+
$options = $this->inheritBuilderEncoder($options);
265266
$options = $this->inheritWriteOptions($options);
266267
$options = $this->inheritCodec($options);
267268

@@ -286,6 +287,7 @@ public function bulkWrite(array $operations, array $options = [])
286287
*/
287288
public function count(array|object $filter = [], array $options = [])
288289
{
290+
$filter = $this->builderEncoder->encodeIfSupported($filter);
289291
$options = $this->inheritReadOptions($options);
290292

291293
$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
@@ -307,6 +309,7 @@ public function count(array|object $filter = [], array $options = [])
307309
*/
308310
public function countDocuments(array|object $filter = [], array $options = [])
309311
{
312+
$filter = $this->builderEncoder->encodeIfSupported($filter);
310313
$options = $this->inheritReadOptions($options);
311314

312315
$operation = new CountDocuments($this->databaseName, $this->collectionName, $filter, $options);
@@ -444,6 +447,7 @@ public function createSearchIndexes(array $indexes, array $options = []): array
444447
*/
445448
public function deleteMany(array|object $filter, array $options = [])
446449
{
450+
$filter = $this->builderEncoder->encodeIfSupported($filter);
447451
$options = $this->inheritWriteOptions($options);
448452

449453
$operation = new DeleteMany($this->databaseName, $this->collectionName, $filter, $options);
@@ -465,6 +469,7 @@ public function deleteMany(array|object $filter, array $options = [])
465469
*/
466470
public function deleteOne(array|object $filter, array $options = [])
467471
{
472+
$filter = $this->builderEncoder->encodeIfSupported($filter);
468473
$options = $this->inheritWriteOptions($options);
469474

470475
$operation = new DeleteOne($this->databaseName, $this->collectionName, $filter, $options);
@@ -487,6 +492,7 @@ public function deleteOne(array|object $filter, array $options = [])
487492
*/
488493
public function distinct(string $fieldName, array|object $filter = [], array $options = [])
489494
{
495+
$filter = $this->builderEncoder->encodeIfSupported($filter);
490496
$options = $this->inheritReadOptions($options);
491497
$options = $this->inheritTypeMap($options);
492498

@@ -645,6 +651,7 @@ public function explain(Explainable $explainable, array $options = [])
645651
*/
646652
public function find(array|object $filter = [], array $options = [])
647653
{
654+
$filter = $this->builderEncoder->encodeIfSupported($filter);
648655
$options = $this->inheritReadOptions($options);
649656
$options = $this->inheritCodecOrTypeMap($options);
650657

@@ -667,6 +674,7 @@ public function find(array|object $filter = [], array $options = [])
667674
*/
668675
public function findOne(array|object $filter = [], array $options = [])
669676
{
677+
$filter = $this->builderEncoder->encodeIfSupported($filter);
670678
$options = $this->inheritReadOptions($options);
671679
$options = $this->inheritCodecOrTypeMap($options);
672680

@@ -692,6 +700,7 @@ public function findOne(array|object $filter = [], array $options = [])
692700
*/
693701
public function findOneAndDelete(array|object $filter, array $options = [])
694702
{
703+
$filter = $this->builderEncoder->encodeIfSupported($filter);
695704
$options = $this->inheritWriteOptions($options);
696705
$options = $this->inheritCodecOrTypeMap($options);
697706

@@ -722,6 +731,7 @@ public function findOneAndDelete(array|object $filter, array $options = [])
722731
*/
723732
public function findOneAndReplace(array|object $filter, array|object $replacement, array $options = [])
724733
{
734+
$filter = $this->builderEncoder->encodeIfSupported($filter);
725735
$options = $this->inheritWriteOptions($options);
726736
$options = $this->inheritCodecOrTypeMap($options);
727737

@@ -752,6 +762,7 @@ public function findOneAndReplace(array|object $filter, array|object $replacemen
752762
*/
753763
public function findOneAndUpdate(array|object $filter, array|object $update, array $options = [])
754764
{
765+
$filter = $this->builderEncoder->encodeIfSupported($filter);
755766
$options = $this->inheritWriteOptions($options);
756767
$options = $this->inheritCodecOrTypeMap($options);
757768

@@ -1000,6 +1011,7 @@ public function rename(string $toCollectionName, ?string $toDatabaseName = null,
10001011
*/
10011012
public function replaceOne(array|object $filter, array|object $replacement, array $options = [])
10021013
{
1014+
$filter = $this->builderEncoder->encodeIfSupported($filter);
10031015
$options = $this->inheritWriteOptions($options);
10041016
$options = $this->inheritCodec($options);
10051017

@@ -1023,6 +1035,8 @@ public function replaceOne(array|object $filter, array|object $replacement, arra
10231035
*/
10241036
public function updateMany(array|object $filter, array|object $update, array $options = [])
10251037
{
1038+
$filter = $this->builderEncoder->encodeIfSupported($filter);
1039+
$update = $this->builderEncoder->encodeIfSupported($update);
10261040
$options = $this->inheritWriteOptions($options);
10271041

10281042
$operation = new UpdateMany($this->databaseName, $this->collectionName, $filter, $update, $options);
@@ -1045,6 +1059,8 @@ public function updateMany(array|object $filter, array|object $update, array $op
10451059
*/
10461060
public function updateOne(array|object $filter, array|object $update, array $options = [])
10471061
{
1062+
$filter = $this->builderEncoder->encodeIfSupported($filter);
1063+
$update = $this->builderEncoder->encodeIfSupported($update);
10481064
$options = $this->inheritWriteOptions($options);
10491065

10501066
$operation = new UpdateOne($this->databaseName, $this->collectionName, $filter, $update, $options);
@@ -1112,6 +1128,11 @@ public function withOptions(array $options = [])
11121128
return new Collection($this->manager, $this->databaseName, $this->collectionName, $options);
11131129
}
11141130

1131+
private function inheritBuilderEncoder(array $options): array
1132+
{
1133+
return ['builderEncoder' => $this->builderEncoder] + $options;
1134+
}
1135+
11151136
private function inheritCodec(array $options): array
11161137
{
11171138
// If the options contain a type map, don't inherit anything

src/Operation/BulkWrite.php

Lines changed: 19 additions & 2 deletions
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
}
@@ -169,7 +178,7 @@ public function __construct(private string $databaseName, private string $collec
169178
unset($options['writeConcern']);
170179
}
171180

172-
$this->operations = $this->validateOperations($operations, $options['codec'] ?? null);
181+
$this->operations = $this->validateOperations($operations, $options['codec'] ?? null, $options['builderEncoder'] ?? new BuilderEncoder());
173182
$this->options = $options;
174183
}
175184

@@ -264,7 +273,7 @@ private function createExecuteOptions(): array
264273
* @param array[] $operations
265274
* @return array[]
266275
*/
267-
private function validateOperations(array $operations, ?DocumentCodec $codec): array
276+
private function validateOperations(array $operations, ?DocumentCodec $codec, Encoder $builderEncoder): array
268277
{
269278
foreach ($operations as $i => $operation) {
270279
if (! is_array($operation)) {
@@ -298,6 +307,8 @@ private function validateOperations(array $operations, ?DocumentCodec $codec): a
298307

299308
case self::DELETE_MANY:
300309
case self::DELETE_ONE:
310+
$operations[$i][$type][0] = $builderEncoder->encodeIfSupported($args[0]);
311+
301312
if (! isset($args[1])) {
302313
$args[1] = [];
303314
}
@@ -317,6 +328,8 @@ private function validateOperations(array $operations, ?DocumentCodec $codec): a
317328
break;
318329

319330
case self::REPLACE_ONE:
331+
$operations[$i][$type][0] = $builderEncoder->encodeIfSupported($args[0]);
332+
320333
if (! isset($args[1]) && ! array_key_exists(1, $args)) {
321334
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
322335
}
@@ -367,10 +380,14 @@ private function validateOperations(array $operations, ?DocumentCodec $codec): a
367380

368381
case self::UPDATE_MANY:
369382
case self::UPDATE_ONE:
383+
$operations[$i][$type][0] = $builderEncoder->encodeIfSupported($args[0]);
384+
370385
if (! isset($args[1]) && ! array_key_exists(1, $args)) {
371386
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
372387
}
373388

389+
$operations[$i][$type][1] = $args[1] = $builderEncoder->encodeIfSupported($args[1]);
390+
374391
if ((! is_document($args[1]) || ! is_first_key_operator($args[1])) && ! is_pipeline($args[1])) {
375392
throw new InvalidArgumentException(sprintf('Expected update operator(s) or non-empty pipeline for $operations[%d]["%s"][1]', $i, $type));
376393
}

0 commit comments

Comments
 (0)