Skip to content

Commit c1cc2d6

Browse files
committed
Merge pull request #65
2 parents 4327715 + b1bc3c8 commit c1cc2d6

17 files changed

+137
-8
lines changed

src/Operation/Aggregate.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class Aggregate implements Executable
2323
{
2424
private static $wireVersionForCursor = 2;
25+
private static $wireVersionForDocumentLevelValidation = 4;
2526

2627
private $databaseName;
2728
private $collectionName;
@@ -39,6 +40,13 @@ class Aggregate implements Executable
3940
*
4041
* * batchSize (integer): The number of documents to return per batch.
4142
*
43+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
44+
* out of document level validation. This only applies when the $out
45+
* stage is specified.
46+
*
47+
* For servers < 3.2, this option is ignored as document level validation
48+
* is not available.
49+
*
4250
* * maxTimeMS (integer): The maximum amount of time to allow the query to
4351
* run.
4452
*
@@ -92,6 +100,10 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
92100
throw new InvalidArgumentTypeException('"batchSize" option', $options['batchSize'], 'integer');
93101
}
94102

103+
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
104+
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
105+
}
106+
95107
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
96108
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
97109
}
@@ -163,6 +175,10 @@ private function createCommand(Server $server, $isCursorSupported)
163175

164176
$cmd['allowDiskUse'] = $this->options['allowDiskUse'];
165177

178+
if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
179+
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
180+
}
181+
166182
if (isset($this->options['maxTimeMS'])) {
167183
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
168184
}

src/Operation/BulkWrite.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class BulkWrite implements Executable
2424
const UPDATE_MANY = 'updateMany';
2525
const UPDATE_ONE = 'updateOne';
2626

27+
private static $wireVersionForDocumentLevelValidation = 4;
28+
2729
private $databaseName;
2830
private $collectionName;
2931
private $operations;
@@ -54,6 +56,9 @@ class BulkWrite implements Executable
5456
*
5557
* Supported options for the bulk write operation:
5658
*
59+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
60+
* out of document level validation.
61+
*
5762
* * ordered (boolean): If true, when an insert fails, return without
5863
* performing the remaining writes. If false, when a write fails,
5964
* continue with the remaining writes, if any. The default is true.
@@ -182,6 +187,10 @@ public function __construct($databaseName, $collectionName, array $operations, a
182187

183188
$options += ['ordered' => true];
184189

190+
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
191+
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
192+
}
193+
185194
if ( ! is_bool($options['ordered'])) {
186195
throw new InvalidArgumentTypeException('"ordered" option', $options['ordered'], 'boolean');
187196
}
@@ -205,7 +214,13 @@ public function __construct($databaseName, $collectionName, array $operations, a
205214
*/
206215
public function execute(Server $server)
207216
{
208-
$bulk = new Bulk(['ordered' => $this->options['ordered']]);
217+
$options = ['ordered' => $this->options['ordered']];
218+
219+
if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
220+
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
221+
}
222+
223+
$bulk = new Bulk($options);
209224
$insertedIds = [];
210225

211226
foreach ($this->operations as $i => $operation) {

src/Operation/FindAndModify.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
class FindAndModify implements Executable
2121
{
22+
private static $wireVersionForDocumentLevelValidation = 4;
23+
2224
private $databaseName;
2325
private $collectionName;
2426
private $options;
@@ -28,6 +30,9 @@ class FindAndModify implements Executable
2830
*
2931
* Supported options:
3032
*
33+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
34+
* out of document level validation.
35+
*
3136
* * fields (document): Limits the fields to return for the matching
3237
* document.
3338
*
@@ -66,6 +71,10 @@ public function __construct($databaseName, $collectionName, array $options)
6671
'upsert' => false,
6772
];
6873

74+
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
75+
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
76+
}
77+
6978
if (isset($options['fields']) && ! is_array($options['fields']) && ! is_object($options['fields'])) {
7079
throw new InvalidArgumentTypeException('"fields" option', $options['fields'], 'array or object');
7180
}
@@ -116,7 +125,7 @@ public function __construct($databaseName, $collectionName, array $options)
116125
*/
117126
public function execute(Server $server)
118127
{
119-
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
128+
$cursor = $server->executeCommand($this->databaseName, $this->createCommand($server));
120129
$result = current($cursor->toArray());
121130

122131
if ( ! isset($result->value)) {
@@ -144,9 +153,10 @@ public function execute(Server $server)
144153
/**
145154
* Create the findAndModify command.
146155
*
156+
* @param Server $server
147157
* @return Command
148158
*/
149-
private function createCommand()
159+
private function createCommand(Server $server)
150160
{
151161
$cmd = ['findAndModify' => $this->collectionName];
152162

@@ -167,6 +177,10 @@ private function createCommand()
167177
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
168178
}
169179

180+
if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
181+
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
182+
}
183+
170184
return new Command($cmd);
171185
}
172186
}

src/Operation/FindOneAndReplace.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class FindOneAndReplace implements Executable
2626
*
2727
* Supported options:
2828
*
29+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
30+
* out of document level validation.
31+
*
2932
* * maxTimeMS (integer): The maximum amount of time to allow the query to
3033
* run.
3134
*

src/Operation/FindOneAndUpdate.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class FindOneAndUpdate implements Executable
2626
*
2727
* Supported options:
2828
*
29+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
30+
* out of document level validation.
31+
*
2932
* * maxTimeMS (integer): The maximum amount of time to allow the query to
3033
* run.
3134
*

src/Operation/InsertMany.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
class InsertMany implements Executable
2020
{
21+
private static $wireVersionForDocumentLevelValidation = 4;
22+
2123
private $databaseName;
2224
private $collectionName;
2325
private $documents;
@@ -28,6 +30,9 @@ class InsertMany implements Executable
2830
*
2931
* Supported options:
3032
*
33+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
34+
* out of document level validation.
35+
*
3136
* * ordered (boolean): If true, when an insert fails, return without
3237
* performing the remaining writes. If false, when a write fails,
3338
* continue with the remaining writes, if any. The default is true.
@@ -62,6 +67,10 @@ public function __construct($databaseName, $collectionName, array $documents, ar
6267

6368
$options += ['ordered' => true];
6469

70+
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
71+
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
72+
}
73+
6574
if ( ! is_bool($options['ordered'])) {
6675
throw new InvalidArgumentTypeException('"ordered" option', $options['ordered'], 'boolean');
6776
}
@@ -85,7 +94,13 @@ public function __construct($databaseName, $collectionName, array $documents, ar
8594
*/
8695
public function execute(Server $server)
8796
{
88-
$bulk = new Bulk(['ordered' => $this->options['ordered']]);
97+
$options = ['ordered' => $this->options['ordered']];
98+
99+
if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
100+
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
101+
}
102+
103+
$bulk = new Bulk($options);
89104
$insertedIds = [];
90105

91106
foreach ($this->documents as $i => $document) {

src/Operation/InsertOne.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class InsertOne implements Executable
1919
{
20+
private static $wireVersionForDocumentLevelValidation = 4;
21+
2022
private $databaseName;
2123
private $collectionName;
2224
private $document;
@@ -27,6 +29,9 @@ class InsertOne implements Executable
2729
*
2830
* Supported options:
2931
*
32+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
33+
* out of document level validation.
34+
*
3035
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
3136
*
3237
* @param string $databaseName Database name
@@ -41,6 +46,10 @@ public function __construct($databaseName, $collectionName, $document, array $op
4146
throw new InvalidArgumentTypeException('$document', $document, 'array or object');
4247
}
4348

49+
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
50+
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
51+
}
52+
4453
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
4554
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
4655
}
@@ -60,7 +69,13 @@ public function __construct($databaseName, $collectionName, $document, array $op
6069
*/
6170
public function execute(Server $server)
6271
{
63-
$bulk = new Bulk();
72+
$options = [];
73+
74+
if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
75+
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
76+
}
77+
78+
$bulk = new Bulk($options);
6479
$insertedId = $bulk->insert($this->document);
6580

6681
if ($insertedId === null) {

src/Operation/ReplaceOne.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class ReplaceOne implements Executable
2323
*
2424
* Supported options:
2525
*
26+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
27+
* out of document level validation.
28+
*
2629
* * upsert (boolean): When true, a new document is created if no document
2730
* matches the query. The default is false.
2831
*

src/Operation/Update.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
class Update implements Executable
2222
{
23+
private static $wireVersionForDocumentLevelValidation = 4;
24+
2325
private $databaseName;
2426
private $collectionName;
2527
private $filter;
@@ -31,6 +33,9 @@ class Update implements Executable
3133
*
3234
* Supported options:
3335
*
36+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
37+
* out of document level validation.
38+
*
3439
* * multi (boolean): When true, updates all documents matching the query.
3540
* This option cannot be true if the $update argument is a replacement
3641
* document (i.e. contains no update operators). The default is false.
@@ -63,6 +68,10 @@ public function __construct($databaseName, $collectionName, $filter, $update, ar
6368
'upsert' => false,
6469
];
6570

71+
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
72+
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
73+
}
74+
6675
if ( ! is_bool($options['multi'])) {
6776
throw new InvalidArgumentTypeException('"multi" option', $options['multi'], 'boolean');
6877
}
@@ -95,13 +104,19 @@ public function __construct($databaseName, $collectionName, $filter, $update, ar
95104
*/
96105
public function execute(Server $server)
97106
{
98-
$options = [
107+
$updateOptions = [
99108
'multi' => $this->options['multi'],
100109
'upsert' => $this->options['upsert'],
101110
];
102111

103-
$bulk = new Bulk();
104-
$bulk->update($this->filter, $this->update, $options);
112+
$bulkOptions = [];
113+
114+
if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
115+
$bulkOptions['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
116+
}
117+
118+
$bulk = new Bulk($bulkOptions);
119+
$bulk->update($this->filter, $this->update, $updateOptions);
105120

106121
$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
107122
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);

src/Operation/UpdateMany.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class UpdateMany implements Executable
2323
*
2424
* Supported options:
2525
*
26+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
27+
* out of document level validation.
28+
*
2629
* * upsert (boolean): When true, a new document is created if no document
2730
* matches the query. The default is false.
2831
*

src/Operation/UpdateOne.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class UpdateOne implements Executable
2323
*
2424
* Supported options:
2525
*
26+
* * bypassDocumentValidation (boolean): If true, allows the write to opt
27+
* out of document level validation.
28+
*
2629
* * upsert (boolean): When true, a new document is created if no document
2730
* matches the query. The default is false.
2831
*

tests/Operation/AggregateTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public function provideInvalidConstructorOptions()
4545
$options[][] = ['batchSize' => $value];
4646
}
4747

48+
foreach ($this->getInvalidBooleanValues() as $value) {
49+
$options[][] = ['bypassDocumentValidation' => $value];
50+
}
51+
4852
foreach ($this->getInvalidIntegerValues() as $value) {
4953
$options[][] = ['maxTimeMS' => $value];
5054
}

tests/Operation/BulkWriteTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ public function provideInvalidConstructorOptions()
350350
{
351351
$options = [];
352352

353+
foreach ($this->getInvalidBooleanValues() as $value) {
354+
$options[][] = ['bypassDocumentValidation' => $value];
355+
}
356+
353357
foreach ($this->getInvalidBooleanValues() as $value) {
354358
$options[][] = ['ordered' => $value];
355359
}

tests/Operation/FindAndModifyTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function provideInvalidConstructorOptions()
1919
{
2020
$options = [];
2121

22+
foreach ($this->getInvalidBooleanValues() as $value) {
23+
$options[][] = ['bypassDocumentValidation' => $value];
24+
}
25+
2226
foreach ($this->getInvalidDocumentValues() as $value) {
2327
$options[][] = ['fields' => $value];
2428
}

tests/Operation/InsertManyTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function provideInvalidConstructorOptions()
4747
{
4848
$options = [];
4949

50+
foreach ($this->getInvalidBooleanValues() as $value) {
51+
$options[][] = ['bypassDocumentValidation' => $value];
52+
}
53+
5054
foreach ($this->getInvalidBooleanValues() as $value) {
5155
$options[][] = ['ordered' => $value];
5256
}

tests/Operation/InsertOneTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function provideInvalidConstructorOptions()
2828
{
2929
$options = [];
3030

31+
foreach ($this->getInvalidBooleanValues() as $value) {
32+
$options[][] = ['bypassDocumentValidation' => $value];
33+
}
34+
3135
foreach ($this->getInvalidWriteConcernValues() as $value) {
3236
$options[][] = ['writeConcern' => $value];
3337
}

0 commit comments

Comments
 (0)