@@ -45,6 +45,7 @@ class Collection
4545 private $ manager ;
4646 private $ readConcern ;
4747 private $ readPreference ;
48+ private $ typeMap ;
4849 private $ writeConcern ;
4950
5051 /**
@@ -62,6 +63,8 @@ class Collection
6263 * preference to use for collection operations. Defaults to the Manager's
6364 * read preference.
6465 *
66+ * * typeMap (array): Default type map for cursors and BSON documents.
67+ *
6568 * * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
6669 * to use for collection operations. Defaults to the Manager's write
6770 * concern.
@@ -90,13 +93,18 @@ public function __construct(Manager $manager, $namespace, array $options = [])
9093 throw new InvalidArgumentTypeException ('"readPreference" option ' , $ options ['readPreference ' ], 'MongoDB\Driver\ReadPreference ' );
9194 }
9295
96+ if (isset ($ options ['typeMap ' ]) && ! is_array ($ options ['typeMap ' ])) {
97+ throw new InvalidArgumentTypeException ('"typeMap" option ' , $ options ['typeMap ' ], 'array ' );
98+ }
99+
93100 if (isset ($ options ['writeConcern ' ]) && ! $ options ['writeConcern ' ] instanceof WriteConcern) {
94101 throw new InvalidArgumentTypeException ('"writeConcern" option ' , $ options ['writeConcern ' ], 'MongoDB\Driver\WriteConcern ' );
95102 }
96103
97104 $ this ->manager = $ manager ;
98105 $ this ->readConcern = isset ($ options ['readConcern ' ]) ? $ options ['readConcern ' ] : $ this ->manager ->getReadConcern ();
99106 $ this ->readPreference = isset ($ options ['readPreference ' ]) ? $ options ['readPreference ' ] : $ this ->manager ->getReadPreference ();
107+ $ this ->typeMap = isset ($ options ['typeMap ' ]) ? $ options ['typeMap ' ] : null ;
100108 $ this ->writeConcern = isset ($ options ['writeConcern ' ]) ? $ options ['writeConcern ' ] : $ this ->manager ->getWriteConcern ();
101109 }
102110
@@ -114,6 +122,7 @@ public function __debugInfo()
114122 'manager ' => $ this ->manager ,
115123 'readConcern ' => $ this ->readConcern ,
116124 'readPreference ' => $ this ->readPreference ,
125+ 'typeMap ' => $ this ->typeMap ,
117126 'writeConcern ' => $ this ->writeConcern ,
118127 ];
119128 }
@@ -136,6 +145,10 @@ public function __toString()
136145 * returned; otherwise, an ArrayIterator is returned, which wraps the
137146 * "result" array from the command response document.
138147 *
148+ * Note: BSON deserialization of inline aggregation results (i.e. not using
149+ * a command cursor) does not yet support a custom type map
150+ * (depends on: https://jira.mongodb.org/browse/PHPC-314).
151+ *
139152 * @see Aggregate::__construct() for supported options
140153 * @param array $pipeline List of pipeline operations
141154 * @param array $options Command options
@@ -160,6 +173,10 @@ public function aggregate(array $pipeline, array $options = [])
160173 $ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
161174 }
162175
176+ if ( ! isset ($ options ['typeMap ' ])) {
177+ $ options ['typeMap ' ] = $ this ->typeMap ;
178+ }
179+
163180 $ operation = new Aggregate ($ this ->databaseName , $ this ->collectionName , $ pipeline , $ options );
164181 $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
165182
@@ -388,6 +405,10 @@ public function find($filter = [], array $options = [])
388405 $ options ['readPreference ' ] = $ this ->readPreference ;
389406 }
390407
408+ if ( ! isset ($ options ['typeMap ' ])) {
409+ $ options ['typeMap ' ] = $ this ->typeMap ;
410+ }
411+
391412 $ operation = new Find ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
392413 $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
393414
@@ -413,6 +434,10 @@ public function findOne($filter = [], array $options = [])
413434 $ options ['readPreference ' ] = $ this ->readPreference ;
414435 }
415436
437+ if ( ! isset ($ options ['typeMap ' ])) {
438+ $ options ['typeMap ' ] = $ this ->typeMap ;
439+ }
440+
416441 $ operation = new FindOne ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
417442 $ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
418443
@@ -424,6 +449,9 @@ public function findOne($filter = [], array $options = [])
424449 *
425450 * The document to return may be null.
426451 *
452+ * Note: BSON deserialization of the returned document does not yet support
453+ * a custom type map (depends on: https://jira.mongodb.org/browse/PHPC-314).
454+ *
427455 * @see FindOneAndDelete::__construct() for supported options
428456 * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
429457 * @param array|object $filter Query by which to filter documents
@@ -451,6 +479,9 @@ public function findOneAndDelete($filter, array $options = [])
451479 * returned. Specify FindOneAndReplace::RETURN_DOCUMENT_AFTER for the
452480 * "returnDocument" option to return the updated document.
453481 *
482+ * Note: BSON deserialization of the returned document does not yet support
483+ * a custom type map (depends on: https://jira.mongodb.org/browse/PHPC-314).
484+ *
454485 * @see FindOneAndReplace::__construct() for supported options
455486 * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
456487 * @param array|object $filter Query by which to filter documents
@@ -479,6 +510,9 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
479510 * returned. Specify FindOneAndUpdate::RETURN_DOCUMENT_AFTER for the
480511 * "returnDocument" option to return the updated document.
481512 *
513+ * Note: BSON deserialization of the returned document does not yet support
514+ * a custom type map (depends on: https://jira.mongodb.org/browse/PHPC-314).
515+ *
482516 * @see FindOneAndReplace::__construct() for supported options
483517 * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
484518 * @param array|object $filter Query by which to filter documents
@@ -613,9 +647,9 @@ public function replaceOne($filter, $replacement, array $options = [])
613647 *
614648 * @see UpdateMany::__construct() for supported options
615649 * @see http://docs.mongodb.org/manual/reference/command/update/
616- * @param array|object $filter Query by which to filter documents
617- * @param array|object $replacement Update to apply to the matched documents
618- * @param array $options Command options
650+ * @param array|object $filter Query by which to filter documents
651+ * @param array|object $update Update to apply to the matched documents
652+ * @param array $options Command options
619653 * @return UpdateResult
620654 */
621655 public function updateMany ($ filter , $ update , array $ options = [])
@@ -635,9 +669,9 @@ public function updateMany($filter, $update, array $options = [])
635669 *
636670 * @see ReplaceOne::__construct() for supported options
637671 * @see http://docs.mongodb.org/manual/reference/command/update/
638- * @param array|object $filter Query by which to filter documents
639- * @param array|object $replacement Update to apply to the matched document
640- * @param array $options Command options
672+ * @param array|object $filter Query by which to filter documents
673+ * @param array|object $update Update to apply to the matched document
674+ * @param array $options Command options
641675 * @return UpdateResult
642676 */
643677 public function updateOne ($ filter , $ update , array $ options = [])
@@ -655,36 +689,18 @@ public function updateOne($filter, $update, array $options = [])
655689 /**
656690 * Get a clone of this collection with different options.
657691 *
658- * Supported options:
659- *
660- * * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
661- * use for collection operations. Defaults to this Collection's read
662- * concern.
663- *
664- * * readPreference (MongoDB\Driver\ReadPreference): The default read
665- * preference to use for collection operations. Defaults to this
666- * Collection's read preference.
667- *
668- * * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
669- * to use for collection operations. Defaults to this Collection's write
670- * concern.
671- *
692+ * @see Collection::__construct() for supported options
672693 * @param array $options Collection constructor options
673694 * @return Collection
674695 */
675696 public function withOptions (array $ options = [])
676697 {
677- if ( ! isset ($ options ['readConcern ' ])) {
678- $ options ['readConcern ' ] = $ this ->readConcern ;
679- }
680-
681- if ( ! isset ($ options ['readPreference ' ])) {
682- $ options ['readPreference ' ] = $ this ->readPreference ;
683- }
684-
685- if ( ! isset ($ options ['writeConcern ' ])) {
686- $ options ['writeConcern ' ] = $ this ->writeConcern ;
687- }
698+ $ options += [
699+ 'readConcern ' => $ this ->readConcern ,
700+ 'readPreference ' => $ this ->readPreference ,
701+ 'typeMap ' => $ this ->typeMap ,
702+ 'writeConcern ' => $ this ->writeConcern ,
703+ ];
688704
689705 return new Collection ($ this ->manager , $ this ->databaseName . '. ' . $ this ->collectionName , $ options );
690706 }
0 commit comments