1919 */
2020class 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}
0 commit comments