@@ -45,6 +45,7 @@ class Collection
45
45
private $ manager ;
46
46
private $ readConcern ;
47
47
private $ readPreference ;
48
+ private $ typeMap ;
48
49
private $ writeConcern ;
49
50
50
51
/**
@@ -62,6 +63,8 @@ class Collection
62
63
* preference to use for collection operations. Defaults to the Manager's
63
64
* read preference.
64
65
*
66
+ * * typeMap (array): Default type map for cursors and BSON documents.
67
+ *
65
68
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
66
69
* to use for collection operations. Defaults to the Manager's write
67
70
* concern.
@@ -90,13 +93,18 @@ public function __construct(Manager $manager, $namespace, array $options = [])
90
93
throw new InvalidArgumentTypeException ('"readPreference" option ' , $ options ['readPreference ' ], 'MongoDB\Driver\ReadPreference ' );
91
94
}
92
95
96
+ if (isset ($ options ['typeMap ' ]) && ! is_array ($ options ['typeMap ' ])) {
97
+ throw new InvalidArgumentTypeException ('"typeMap" option ' , $ options ['typeMap ' ], 'array ' );
98
+ }
99
+
93
100
if (isset ($ options ['writeConcern ' ]) && ! $ options ['writeConcern ' ] instanceof WriteConcern) {
94
101
throw new InvalidArgumentTypeException ('"writeConcern" option ' , $ options ['writeConcern ' ], 'MongoDB\Driver\WriteConcern ' );
95
102
}
96
103
97
104
$ this ->manager = $ manager ;
98
105
$ this ->readConcern = isset ($ options ['readConcern ' ]) ? $ options ['readConcern ' ] : $ this ->manager ->getReadConcern ();
99
106
$ this ->readPreference = isset ($ options ['readPreference ' ]) ? $ options ['readPreference ' ] : $ this ->manager ->getReadPreference ();
107
+ $ this ->typeMap = isset ($ options ['typeMap ' ]) ? $ options ['typeMap ' ] : null ;
100
108
$ this ->writeConcern = isset ($ options ['writeConcern ' ]) ? $ options ['writeConcern ' ] : $ this ->manager ->getWriteConcern ();
101
109
}
102
110
@@ -114,6 +122,7 @@ public function __debugInfo()
114
122
'manager ' => $ this ->manager ,
115
123
'readConcern ' => $ this ->readConcern ,
116
124
'readPreference ' => $ this ->readPreference ,
125
+ 'typeMap ' => $ this ->typeMap ,
117
126
'writeConcern ' => $ this ->writeConcern ,
118
127
];
119
128
}
@@ -136,6 +145,10 @@ public function __toString()
136
145
* returned; otherwise, an ArrayIterator is returned, which wraps the
137
146
* "result" array from the command response document.
138
147
*
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
+ *
139
152
* @see Aggregate::__construct() for supported options
140
153
* @param array $pipeline List of pipeline operations
141
154
* @param array $options Command options
@@ -160,6 +173,10 @@ public function aggregate(array $pipeline, array $options = [])
160
173
$ options ['readPreference ' ] = new ReadPreference (ReadPreference::RP_PRIMARY );
161
174
}
162
175
176
+ if ( ! isset ($ options ['typeMap ' ])) {
177
+ $ options ['typeMap ' ] = $ this ->typeMap ;
178
+ }
179
+
163
180
$ operation = new Aggregate ($ this ->databaseName , $ this ->collectionName , $ pipeline , $ options );
164
181
$ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
165
182
@@ -388,6 +405,10 @@ public function find($filter = [], array $options = [])
388
405
$ options ['readPreference ' ] = $ this ->readPreference ;
389
406
}
390
407
408
+ if ( ! isset ($ options ['typeMap ' ])) {
409
+ $ options ['typeMap ' ] = $ this ->typeMap ;
410
+ }
411
+
391
412
$ operation = new Find ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
392
413
$ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
393
414
@@ -413,6 +434,10 @@ public function findOne($filter = [], array $options = [])
413
434
$ options ['readPreference ' ] = $ this ->readPreference ;
414
435
}
415
436
437
+ if ( ! isset ($ options ['typeMap ' ])) {
438
+ $ options ['typeMap ' ] = $ this ->typeMap ;
439
+ }
440
+
416
441
$ operation = new FindOne ($ this ->databaseName , $ this ->collectionName , $ filter , $ options );
417
442
$ server = $ this ->manager ->selectServer ($ options ['readPreference ' ]);
418
443
@@ -424,6 +449,9 @@ public function findOne($filter = [], array $options = [])
424
449
*
425
450
* The document to return may be null.
426
451
*
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
+ *
427
455
* @see FindOneAndDelete::__construct() for supported options
428
456
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
429
457
* @param array|object $filter Query by which to filter documents
@@ -451,6 +479,9 @@ public function findOneAndDelete($filter, array $options = [])
451
479
* returned. Specify FindOneAndReplace::RETURN_DOCUMENT_AFTER for the
452
480
* "returnDocument" option to return the updated document.
453
481
*
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
+ *
454
485
* @see FindOneAndReplace::__construct() for supported options
455
486
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
456
487
* @param array|object $filter Query by which to filter documents
@@ -479,6 +510,9 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
479
510
* returned. Specify FindOneAndUpdate::RETURN_DOCUMENT_AFTER for the
480
511
* "returnDocument" option to return the updated document.
481
512
*
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
+ *
482
516
* @see FindOneAndReplace::__construct() for supported options
483
517
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
484
518
* @param array|object $filter Query by which to filter documents
@@ -613,9 +647,9 @@ public function replaceOne($filter, $replacement, array $options = [])
613
647
*
614
648
* @see UpdateMany::__construct() for supported options
615
649
* @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
619
653
* @return UpdateResult
620
654
*/
621
655
public function updateMany ($ filter , $ update , array $ options = [])
@@ -635,9 +669,9 @@ public function updateMany($filter, $update, array $options = [])
635
669
*
636
670
* @see ReplaceOne::__construct() for supported options
637
671
* @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
641
675
* @return UpdateResult
642
676
*/
643
677
public function updateOne ($ filter , $ update , array $ options = [])
@@ -655,36 +689,18 @@ public function updateOne($filter, $update, array $options = [])
655
689
/**
656
690
* Get a clone of this collection with different options.
657
691
*
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
672
693
* @param array $options Collection constructor options
673
694
* @return Collection
674
695
*/
675
696
public function withOptions (array $ options = [])
676
697
{
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
+ ];
688
704
689
705
return new Collection ($ this ->manager , $ this ->databaseName . '. ' . $ this ->collectionName , $ options );
690
706
}
0 commit comments