30
30
use function is_integer ;
31
31
use function is_object ;
32
32
use function is_string ;
33
+ use function sprintf ;
33
34
use function trigger_error ;
34
35
35
36
use const E_USER_DEPRECATED ;
@@ -100,6 +101,10 @@ class CreateCollection implements Executable
100
101
* * maxTimeMS (integer): The maximum amount of time to allow the query to
101
102
* run.
102
103
*
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
+ *
103
108
* * session (MongoDB\Driver\Session): Client session.
104
109
*
105
110
* * size (integer): The maximum number of bytes for a capped collection.
@@ -119,6 +124,9 @@ class CreateCollection implements Executable
119
124
*
120
125
* * validator (document): Validation rules or expressions.
121
126
*
127
+ * * viewOn (string): The name of the source collection or view from which
128
+ * to create the view.
129
+ *
122
130
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
123
131
*
124
132
* @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 = [])
170
178
throw InvalidArgumentException::invalidType ('"maxTimeMS" option ' , $ options ['maxTimeMS ' ], 'integer ' );
171
179
}
172
180
181
+ if (isset ($ options ['pipeline ' ]) && ! is_array ($ options ['pipeline ' ])) {
182
+ throw InvalidArgumentException::invalidType ('"pipeline" option ' , $ options ['pipeline ' ], 'array ' );
183
+ }
184
+
173
185
if (isset ($ options ['session ' ]) && ! $ options ['session ' ] instanceof Session) {
174
186
throw InvalidArgumentException::invalidType ('"session" option ' , $ options ['session ' ], Session::class);
175
187
}
@@ -202,6 +214,10 @@ public function __construct($databaseName, $collectionName, array $options = [])
202
214
throw InvalidArgumentException::invalidType ('"validator" option ' , $ options ['validator ' ], 'array or object ' );
203
215
}
204
216
217
+ if (isset ($ options ['viewOn ' ]) && ! is_string ($ options ['viewOn ' ])) {
218
+ throw InvalidArgumentException::invalidType ('"viewOn" option ' , $ options ['viewOn ' ], 'string ' );
219
+ }
220
+
205
221
if (isset ($ options ['writeConcern ' ]) && ! $ options ['writeConcern ' ] instanceof WriteConcern) {
206
222
throw InvalidArgumentException::invalidType ('"writeConcern" option ' , $ options ['writeConcern ' ], WriteConcern::class);
207
223
}
@@ -214,6 +230,22 @@ public function __construct($databaseName, $collectionName, array $options = [])
214
230
trigger_error ('The "autoIndexId" option is deprecated and will be removed in a future release ' , E_USER_DEPRECATED );
215
231
}
216
232
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
+
217
249
$ this ->databaseName = (string ) $ databaseName ;
218
250
$ this ->collectionName = (string ) $ collectionName ;
219
251
$ this ->options = $ options ;
@@ -247,7 +279,7 @@ private function createCommand()
247
279
{
248
280
$ cmd = ['create ' => $ this ->collectionName ];
249
281
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 ) {
251
283
if (isset ($ this ->options [$ option ])) {
252
284
$ cmd [$ option ] = $ this ->options [$ option ];
253
285
}
0 commit comments