3030use function is_integer ;
3131use function is_object ;
3232use function is_string ;
33+ use function sprintf ;
3334use function trigger_error ;
3435
3536use const E_USER_DEPRECATED ;
@@ -100,6 +101,10 @@ class CreateCollection implements Executable
100101 * * maxTimeMS (integer): The maximum amount of time to allow the query to
101102 * run.
102103 *
104+ * * pipeline (array): An array that consists of the aggregation pipeline
105+ * stage(s), which will be applied to the collection or view specified by
106+ * viewOn.
107+ *
103108 * * session (MongoDB\Driver\Session): Client session.
104109 *
105110 * * size (integer): The maximum number of bytes for a capped collection.
@@ -119,6 +124,9 @@ class CreateCollection implements Executable
119124 *
120125 * * validator (document): Validation rules or expressions.
121126 *
127+ * * viewOn (string): The name of the source collection or view from which
128+ * to create the view.
129+ *
122130 * * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
123131 *
124132 * @see https://source.wiredtiger.com/2.4.1/struct_w_t___s_e_s_s_i_o_n.html#a358ca4141d59c345f401c58501276bbb
@@ -170,6 +178,10 @@ public function __construct($databaseName, $collectionName, array $options = [])
170178 throw InvalidArgumentException::invalidType ('"maxTimeMS" option ' , $ options ['maxTimeMS ' ], 'integer ' );
171179 }
172180
181+ if (isset ($ options ['pipeline ' ]) && ! is_array ($ options ['pipeline ' ])) {
182+ throw InvalidArgumentException::invalidType ('"pipeline" option ' , $ options ['pipeline ' ], 'array ' );
183+ }
184+
173185 if (isset ($ options ['session ' ]) && ! $ options ['session ' ] instanceof Session) {
174186 throw InvalidArgumentException::invalidType ('"session" option ' , $ options ['session ' ], Session::class);
175187 }
@@ -202,6 +214,10 @@ public function __construct($databaseName, $collectionName, array $options = [])
202214 throw InvalidArgumentException::invalidType ('"validator" option ' , $ options ['validator ' ], 'array or object ' );
203215 }
204216
217+ if (isset ($ options ['viewOn ' ]) && ! is_string ($ options ['viewOn ' ])) {
218+ throw InvalidArgumentException::invalidType ('"viewOn" option ' , $ options ['viewOn ' ], 'string ' );
219+ }
220+
205221 if (isset ($ options ['writeConcern ' ]) && ! $ options ['writeConcern ' ] instanceof WriteConcern) {
206222 throw InvalidArgumentException::invalidType ('"writeConcern" option ' , $ options ['writeConcern ' ], WriteConcern::class);
207223 }
@@ -214,6 +230,22 @@ public function __construct($databaseName, $collectionName, array $options = [])
214230 trigger_error ('The "autoIndexId" option is deprecated and will be removed in a future release ' , E_USER_DEPRECATED );
215231 }
216232
233+ if (isset ($ options ['pipeline ' ])) {
234+ $ expectedIndex = 0 ;
235+
236+ foreach ($ options ['pipeline ' ] as $ i => $ operation ) {
237+ if ($ i !== $ expectedIndex ) {
238+ throw new InvalidArgumentException (sprintf ('The "pipeline" option is not a list (unexpected index: "%s") ' , $ i ));
239+ }
240+
241+ if (! is_array ($ operation ) && ! is_object ($ operation )) {
242+ throw InvalidArgumentException::invalidType (sprintf ('$options["pipeline"][%d] ' , $ i ), $ operation , 'array or object ' );
243+ }
244+
245+ $ expectedIndex += 1 ;
246+ }
247+ }
248+
217249 $ this ->databaseName = (string ) $ databaseName ;
218250 $ this ->collectionName = (string ) $ collectionName ;
219251 $ this ->options = $ options ;
@@ -247,7 +279,7 @@ private function createCommand()
247279 {
248280 $ cmd = ['create ' => $ this ->collectionName ];
249281
250- foreach (['autoIndexId ' , 'capped ' , 'expireAfterSeconds ' , 'flags ' , 'max ' , 'maxTimeMS ' , 'size ' , 'validationAction ' , 'validationLevel ' ] as $ option ) {
282+ foreach (['autoIndexId ' , 'capped ' , 'expireAfterSeconds ' , 'flags ' , 'max ' , 'maxTimeMS ' , 'pipeline ' , ' size ' , 'validationAction ' , 'validationLevel ' , ' viewOn ' ] as $ option ) {
251283 if (isset ($ this ->options [$ option ])) {
252284 $ cmd [$ option ] = $ this ->options [$ option ];
253285 }
0 commit comments