Skip to content

Commit 28c26c8

Browse files
committed
Support for writeConcern option in Collection write methods and in Collection::bulkWrite()
1 parent b1039b3 commit 28c26c8

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/Collection.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,26 @@ public function aggregate(array $pipeline, array $options = [])
113113
/**
114114
* @param WriteModelInterface[] $requests
115115
* @param array $options
116-
* @param WriteConcern|null $writeConcern
117116
* @return WriteResult
118117
*/
119-
public function bulkWrite(array $requests, array $options = [], WriteConcern $writeConcern = null)
118+
public function bulkWrite(array $requests, array $options = [])
120119
{
121-
$writeConcern = $writeConcern ?: $this->writeConcern;
120+
if (isset($options['writeConcern'])) {
121+
if (!$options['writeConcern'] instanceof WriteConcern) {
122+
throw new InvalidArgumentException(
123+
sprintf(
124+
'Option "writeConcern" must be an instance of "%s", but is of type "%s".',
125+
WriteConcern::class,
126+
getType($options['writeConcern'])
127+
)
128+
);
129+
}
130+
131+
$writeConcern = $options['writeConcern'];
132+
unset($options['writeConcern']);
133+
} else {
134+
$writeConcern = $this->writeConcern;
135+
}
122136

123137
$compiler = new BulkCompiler($options);
124138
$compiler->add($requests);
@@ -475,6 +489,8 @@ public function updateOne($filter, $update, array $options = [])
475489
private static function extractBulkWriteOptions(array $options)
476490
{
477491
$definedOptions = ResolverFactory::get(BulkWriteResolver::class)->getDefinedOptions();
492+
array_push($definedOptions, 'writeConcern');
493+
478494
$bulkWriteOptions = array_intersect_key($options, array_flip($definedOptions));
479495
$operationOptions = array_diff_key($options, $bulkWriteOptions);
480496

0 commit comments

Comments
 (0)