1515use MongoDB \Driver \ServerApi ;
1616use MongoDB \Driver \WriteConcern ;
1717use MongoDB \Operation \CreateCollection ;
18+ use MongoDB \Operation \CreateEncryptedCollection ;
1819use MongoDB \Operation \DatabaseCommand ;
1920use MongoDB \Operation \ListCollections ;
2021use stdClass ;
@@ -257,6 +258,9 @@ public static function getModuleInfo(string $row): ?string
257258 * dropped again during tearDown(). If the collection already exists, it
258259 * is dropped and recreated.
259260 *
261+ * This method supports creation of encrypted collections (as indicated by
262+ * the "encryptedFields" option).
263+ *
260264 * A majority write concern is applied by default to ensure that the
261265 * transaction can acquire the required locks.
262266 * See: https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-operations
@@ -265,17 +269,14 @@ public static function getModuleInfo(string $row): ?string
265269 */
266270 protected function createCollection (string $ databaseName , string $ collectionName , array $ options = []): Collection
267271 {
268- // See: https://jira.mongodb.org/browse/PHPLIB-1145
269- if (isset ($ options ['encryptedFields ' ])) {
270- throw new InvalidArgumentException ('The "encryptedFields" option is not supported by createCollection(). Time to refactor! ' );
271- }
272-
273272 // Pass only relevant options to drop the collection in case it already exists
274273 $ dropOptions = array_intersect_key ($ options , ['writeConcern ' => 1 , 'encryptedFields ' => 1 ]);
275274 $ collection = $ this ->dropCollection ($ databaseName , $ collectionName , $ dropOptions );
276275
277276 $ options += ['writeConcern ' => new WriteConcern (WriteConcern::MAJORITY )];
278- $ operation = new CreateCollection ($ databaseName , $ collectionName , $ options );
277+ $ operation = isset ($ options ['encryptedFields ' ])
278+ ? new CreateEncryptedCollection ($ databaseName , $ collectionName , $ options )
279+ : new CreateCollection ($ databaseName , $ collectionName , $ options );
279280 $ operation ->execute ($ this ->getPrimaryServer ());
280281
281282 return $ collection ;
0 commit comments