@@ -178,7 +178,7 @@ public function __construct(private string $databaseName, private string $collec
178
178
unset($ options ['writeConcern ' ]);
179
179
}
180
180
181
- $ this ->operations = $ this ->validateOperations ($ operations , $ options ['codec ' ] ?? null );
181
+ $ this ->operations = $ this ->validateOperations ($ operations , $ options ['codec ' ] ?? null , $ options [ ' builderEncoder ' ] ?? new BuilderEncoder () );
182
182
$ this ->options = $ options ;
183
183
}
184
184
@@ -197,8 +197,6 @@ public function execute(Server $server)
197
197
throw UnsupportedException::writeConcernNotSupportedInTransaction ();
198
198
}
199
199
200
- $ builderEncoder = $ operation ['builderEncoder ' ] ?? new BuilderEncoder ();
201
-
202
200
$ bulk = new Bulk ($ this ->createBulkWriteOptions ());
203
201
$ insertedIds = [];
204
202
@@ -209,7 +207,6 @@ public function execute(Server $server)
209
207
switch ($ type ) {
210
208
case self ::DELETE_MANY :
211
209
case self ::DELETE_ONE :
212
- $ args [0 ] = $ builderEncoder ->encodeIfSupported ($ args [0 ]);
213
210
$ bulk ->delete ($ args [0 ], $ args [1 ]);
214
211
break ;
215
212
@@ -220,8 +217,6 @@ public function execute(Server $server)
220
217
case self ::UPDATE_MANY :
221
218
case self ::UPDATE_ONE :
222
219
case self ::REPLACE_ONE :
223
- $ args [0 ] = $ builderEncoder ->encodeIfSupported ($ args [0 ]);
224
- $ args [1 ] = $ builderEncoder ->encodeIfSupported ($ args [1 ]);
225
220
$ bulk ->update ($ args [0 ], $ args [1 ], $ args [2 ]);
226
221
break ;
227
222
}
@@ -278,7 +273,7 @@ private function createExecuteOptions(): array
278
273
* @param array[] $operations
279
274
* @return array[]
280
275
*/
281
- private function validateOperations (array $ operations , ?DocumentCodec $ codec ): array
276
+ private function validateOperations (array $ operations , ?DocumentCodec $ codec, Encoder $ builderEncoder ): array
282
277
{
283
278
foreach ($ operations as $ i => $ operation ) {
284
279
if (! is_array ($ operation )) {
@@ -312,6 +307,8 @@ private function validateOperations(array $operations, ?DocumentCodec $codec): a
312
307
313
308
case self ::DELETE_MANY :
314
309
case self ::DELETE_ONE :
310
+ $ operations [$ i ][$ type ][0 ] = $ builderEncoder ->encodeIfSupported ($ args [0 ]);
311
+
315
312
if (! isset ($ args [1 ])) {
316
313
$ args [1 ] = [];
317
314
}
@@ -331,6 +328,8 @@ private function validateOperations(array $operations, ?DocumentCodec $codec): a
331
328
break ;
332
329
333
330
case self ::REPLACE_ONE :
331
+ $ operations [$ i ][$ type ][0 ] = $ builderEncoder ->encodeIfSupported ($ args [0 ]);
332
+
334
333
if (! isset ($ args [1 ]) && ! array_key_exists (1 , $ args )) {
335
334
throw new InvalidArgumentException (sprintf ('Missing second argument for $operations[%d]["%s"] ' , $ i , $ type ));
336
335
}
@@ -381,10 +380,14 @@ private function validateOperations(array $operations, ?DocumentCodec $codec): a
381
380
382
381
case self ::UPDATE_MANY :
383
382
case self ::UPDATE_ONE :
383
+ $ operations [$ i ][$ type ][0 ] = $ builderEncoder ->encodeIfSupported ($ args [0 ]);
384
+
384
385
if (! isset ($ args [1 ]) && ! array_key_exists (1 , $ args )) {
385
386
throw new InvalidArgumentException (sprintf ('Missing second argument for $operations[%d]["%s"] ' , $ i , $ type ));
386
387
}
387
388
389
+ $ operations [$ i ][$ type ][1 ] = $ args [1 ] = $ builderEncoder ->encodeIfSupported ($ args [1 ]);
390
+
388
391
if ((! is_document ($ args [1 ]) || ! is_first_key_operator ($ args [1 ])) && ! is_pipeline ($ args [1 ])) {
389
392
throw new InvalidArgumentException (sprintf ('Expected update operator(s) or non-empty pipeline for $operations[%d]["%s"][1] ' , $ i , $ type ));
390
393
}
0 commit comments