1111use MongoDB \Driver \Server ;
1212use MongoDB \Driver \WriteConcern ;
1313use MongoDB \Exception \InvalidArgumentException ;
14+ use MongoDB \Exception \InvalidArgumentTypeException ;
1415use MongoDB \Model \CollectionInfoIterator ;
1516use MongoDB \Operation \CreateCollection ;
1617use MongoDB \Operation \DropCollection ;
@@ -30,22 +31,55 @@ class Database
3031 * This class provides methods for database-specific operations and serves
3132 * as a gateway for accessing collections.
3233 *
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
3848 */
39- public function __construct (Manager $ manager , $ databaseName , WriteConcern $ writeConcern = null , ReadPreference $ readPreference = null )
49+ public function __construct (Manager $ manager , $ databaseName , array $ options = [] )
4050 {
4151 if (strlen ($ databaseName ) < 1 ) {
4252 throw new InvalidArgumentException ('$databaseName is invalid: ' . $ databaseName );
4353 }
4454
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+
4563 $ this ->manager = $ manager ;
4664 $ 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+ ];
4983 }
5084
5185 /**
@@ -129,20 +163,59 @@ public function listCollections(array $options = [])
129163 /**
130164 * Select a collection within this database.
131165 *
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.
134171 *
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
138178 * @return Collection
139179 */
140- public function selectCollection ($ collectionName , WriteConcern $ writeConcern = null , ReadPreference $ readPreference = null )
180+ public function selectCollection ($ collectionName , array $ options = [] )
141181 {
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+ }
145218
146- return new Collection ($ this ->manager , $ namespace , $ writeConcern , $ readPreference );
219+ return new Database ($ this ->manager , $ this -> databaseName , $ options );
147220 }
148221}
0 commit comments