14
14
use MongoDB \Exception \UnexpectedTypeException ;
15
15
use MongoDB \Model \IndexInfoIterator ;
16
16
use MongoDB \Model \IndexInfoIteratorIterator ;
17
- use stdClass ;
17
+ use MongoDB \ Model \ IndexInput ;
18
18
19
19
class Collection
20
20
{
@@ -286,33 +286,20 @@ public function createIndex($key, array $options = array())
286
286
* @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
287
287
* @param array $indexes List of index specifications
288
288
* @return string[] The names of the created indexes
289
- * @throws InvalidArgumentException if an index specification does not
290
- * contain a "key" document
291
- * @throws UnexpectedTypeException if an index specification is not an array
292
- * or a "key" document is not an array or
293
- * object
289
+ * @throws InvalidArgumentException if an index specification is invalid
294
290
*/
295
291
public function createIndexes (array $ indexes )
296
292
{
297
- foreach ($ indexes as & $ index ) {
293
+ foreach ($ indexes as $ i => $ index ) {
298
294
if ( ! is_array ($ index )) {
299
295
throw new UnexpectedTypeException ($ index , 'array ' );
300
296
}
301
297
302
- if ( ! isset ($ index ['key ' ])) {
303
- throw new InvalidArgumentException ( ' Required "key" document is missing from index specification ' ) ;
298
+ if ( ! isset ($ index ['ns ' ])) {
299
+ $ index [ ' ns ' ] = $ this -> ns ;
304
300
}
305
301
306
- if ( ! is_array ($ index ['key ' ]) && ! is_object ($ index ['key ' ])) {
307
- throw new UnexpectedTypeException ($ index ['key ' ], 'array or object ' );
308
- }
309
-
310
- $ index ['key ' ] = (object ) $ index ['key ' ];
311
- $ index ['ns ' ] = $ this ->ns ;
312
-
313
- if ( ! isset ($ index ['name ' ])) {
314
- $ index ['name ' ] = $ this ->generateIndexName ($ index ['key ' ]);
315
- }
302
+ $ indexes [$ i ] = new IndexInput ($ index );
316
303
}
317
304
318
305
$ readPreference = new ReadPreference (ReadPreference::RP_PRIMARY );
@@ -327,7 +314,7 @@ public function createIndexes(array $indexes)
327
314
$ this ->createIndexesLegacy ($ server , $ indexes );
328
315
}
329
316
330
- return array_map (function (array $ index ) { return $ index[ ' name ' ] ; }, $ indexes );
317
+ return array_map (function (IndexInput $ index ) { return ( string ) $ index ; }, $ indexes );
331
318
}
332
319
333
320
/**
@@ -1249,23 +1236,6 @@ private function createIndexesLegacy(Server $server, array $indexes)
1249
1236
$ server ->executeBulkWrite ($ this ->dbname . '.system.indexes ' , $ bulk );
1250
1237
}
1251
1238
1252
- /**
1253
- * Generates an index name from its key specification.
1254
- *
1255
- * @param object $key
1256
- * @return string
1257
- */
1258
- private function generateIndexName (stdClass $ key )
1259
- {
1260
- $ name = '' ;
1261
-
1262
- foreach ($ key as $ field => $ type ) {
1263
- $ name .= ($ name != '' ? '_ ' : '' ) . $ field . '_ ' . $ type ;
1264
- }
1265
-
1266
- return $ name ;
1267
- }
1268
-
1269
1239
/**
1270
1240
* Returns information for all indexes for this collection using the
1271
1241
* listIndexes command.
0 commit comments