11
11
use MongoDB \Driver \Server ;
12
12
use MongoDB \Driver \WriteConcern ;
13
13
use MongoDB \Exception \InvalidArgumentException ;
14
+ use MongoDB \Exception \InvalidArgumentTypeException ;
14
15
use MongoDB \Model \CollectionInfoIterator ;
15
16
use MongoDB \Operation \CreateCollection ;
16
17
use MongoDB \Operation \DropCollection ;
@@ -30,22 +31,55 @@ class Database
30
31
* This class provides methods for database-specific operations and serves
31
32
* as a gateway for accessing collections.
32
33
*
33
- * @param Manager $manager Manager instance from the driver
34
- * @param string $databaseName Database name
35
- * @param WriteConcern $writeConcern Default write concern to apply
36
- * @param ReadPreference $readPreference Default read preference to apply
37
- * @throws InvalidArgumentException if $databaseName is invalid
34
+ * Supported options:
35
+ *
36
+ * * readPreference (MongoDB\Driver\ReadPreference): The default read
37
+ * preference to use for database operations and selected collections.
38
+ * Defaults to the Manager's read preference.
39
+ *
40
+ * * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
41
+ * to use for database operations and selected collections. Defaults to
42
+ * the Manager's write concern.
43
+ *
44
+ * @param Manager $manager Manager instance from the driver
45
+ * @param string $databaseName Database name
46
+ * @param array $options Database options
47
+ * @throws InvalidArgumentException
38
48
*/
39
- public function __construct (Manager $ manager , $ databaseName , WriteConcern $ writeConcern = null , ReadPreference $ readPreference = null )
49
+ public function __construct (Manager $ manager , $ databaseName , array $ options = [] )
40
50
{
41
51
if (strlen ($ databaseName ) < 1 ) {
42
52
throw new InvalidArgumentException ('$databaseName is invalid: ' . $ databaseName );
43
53
}
44
54
55
+ if (isset ($ options ['readPreference ' ]) && ! $ options ['readPreference ' ] instanceof ReadPreference) {
56
+ throw new InvalidArgumentTypeException ('"readPreference" option ' , $ options ['readPreference ' ], 'MongoDB\Driver\ReadPreference ' );
57
+ }
58
+
59
+ if (isset ($ options ['writeConcern ' ]) && ! $ options ['writeConcern ' ] instanceof WriteConcern) {
60
+ throw new InvalidArgumentTypeException ('"writeConcern" option ' , $ options ['writeConcern ' ], 'MongoDB\Driver\WriteConcern ' );
61
+ }
62
+
45
63
$ this ->manager = $ manager ;
46
64
$ this ->databaseName = (string ) $ databaseName ;
47
- $ this ->writeConcern = $ writeConcern ?: $ this ->manager ->getWriteConcern ();
48
- $ this ->readPreference = $ readPreference ?: $ this ->manager ->getReadPreference ();
65
+ $ this ->readPreference = isset ($ options ['readPreference ' ]) ? $ options ['readPreference ' ] : $ this ->manager ->getReadPreference ();
66
+ $ this ->writeConcern = isset ($ options ['writeConcern ' ]) ? $ options ['writeConcern ' ] : $ this ->manager ->getWriteConcern ();
67
+ }
68
+
69
+ /**
70
+ * Return internal properties for debugging purposes.
71
+ *
72
+ * @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
73
+ * @param array
74
+ */
75
+ public function __debugInfo ()
76
+ {
77
+ return [
78
+ 'databaseName ' => $ this ->databaseName ,
79
+ 'manager ' => $ this ->manager ,
80
+ 'readPreference ' => $ this ->readPreference ,
81
+ 'writeConcern ' => $ this ->writeConcern ,
82
+ ];
49
83
}
50
84
51
85
/**
@@ -129,20 +163,59 @@ public function listCollections(array $options = [])
129
163
/**
130
164
* Select a collection within this database.
131
165
*
132
- * If a write concern or read preference is not specified, the write concern
133
- * or read preference of the Database will be applied, respectively.
166
+ * Supported options:
167
+ *
168
+ * * readPreference (MongoDB\Driver\ReadPreference): The default read
169
+ * preference to use for collection operations. Defaults to the
170
+ * Database's read preference.
134
171
*
135
- * @param string $collectionName Name of the collection to select
136
- * @param WriteConcern $writeConcern Default write concern to apply
137
- * @param ReadPreference $readPreference Default read preference to apply
172
+ * * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
173
+ * to use for collection operations. Defaults to the Database's write
174
+ * concern.
175
+ *
176
+ * @param string $collectionName Name of the collection to select
177
+ * @param array $options Collection constructor options
138
178
* @return Collection
139
179
*/
140
- public function selectCollection ($ collectionName , WriteConcern $ writeConcern = null , ReadPreference $ readPreference = null )
180
+ public function selectCollection ($ collectionName , array $ options = [] )
141
181
{
142
- $ namespace = $ this ->databaseName . '. ' . $ collectionName ;
143
- $ writeConcern = $ writeConcern ?: $ this ->writeConcern ;
144
- $ readPreference = $ readPreference ?: $ this ->readPreference ;
182
+ if ( ! isset ($ options ['readPreference ' ])) {
183
+ $ options ['readPreference ' ] = $ this ->readPreference ;
184
+ }
185
+
186
+ if ( ! isset ($ options ['writeConcern ' ])) {
187
+ $ options ['writeConcern ' ] = $ this ->writeConcern ;
188
+ }
189
+
190
+ return new Collection ($ this ->manager , $ this ->databaseName . '. ' . $ collectionName , $ options );
191
+ }
192
+
193
+ /**
194
+ * Get a clone of this database with different options.
195
+ *
196
+ * Supported options:
197
+ *
198
+ * * readPreference (MongoDB\Driver\ReadPreference): The default read
199
+ * preference to use for database operations and selected collections.
200
+ * Defaults to this Database's read preference.
201
+ *
202
+ * * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
203
+ * to use for database operations and selected collections. Defaults to
204
+ * this Database's write concern.
205
+ *
206
+ * @param array $options Database constructor options
207
+ * @return Database
208
+ */
209
+ public function withOptions (array $ options = [])
210
+ {
211
+ if ( ! isset ($ options ['readPreference ' ])) {
212
+ $ options ['readPreference ' ] = $ this ->readPreference ;
213
+ }
214
+
215
+ if ( ! isset ($ options ['writeConcern ' ])) {
216
+ $ options ['writeConcern ' ] = $ this ->writeConcern ;
217
+ }
145
218
146
- return new Collection ($ this ->manager , $ namespace , $ writeConcern , $ readPreference );
219
+ return new Database ($ this ->manager , $ this -> databaseName , $ options );
147
220
}
148
221
}
0 commit comments