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,39 @@ 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 ();
4967 }
5068
5169 /**
@@ -129,20 +147,30 @@ public function listCollections(array $options = [])
129147 /**
130148 * Select a collection within this database.
131149 *
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.
150+ * Supported options:
151+ *
152+ * * readPreference (MongoDB\Driver\ReadPreference): The default read
153+ * preference to use for collection operations. Defaults to the
154+ * Database's read preference.
155+ *
156+ * * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
157+ * to use for collection operations. Defaults to the Database's write
158+ * concern.
134159 *
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
160+ * @param string $collectionName Name of the collection to select
161+ * @param array $options Collection constructor options
138162 * @return Collection
139163 */
140- public function selectCollection ($ collectionName , WriteConcern $ writeConcern = null , ReadPreference $ readPreference = null )
164+ public function selectCollection ($ collectionName , array $ options = [] )
141165 {
142- $ namespace = $ this ->databaseName . '. ' . $ collectionName ;
143- $ writeConcern = $ writeConcern ?: $ this ->writeConcern ;
144- $ readPreference = $ readPreference ?: $ this ->readPreference ;
166+ if ( ! isset ($ options ['readPreference ' ])) {
167+ $ options ['readPreference ' ] = $ this ->readPreference ;
168+ }
169+
170+ if ( ! isset ($ options ['writeConcern ' ])) {
171+ $ options ['writeConcern ' ] = $ this ->writeConcern ;
172+ }
145173
146- return new Collection ($ this ->manager , $ namespace , $ writeConcern , $ readPreference );
174+ return new Collection ($ this ->manager , $ this -> databaseName . ' . ' . $ collectionName , $ options );
147175 }
148176}
0 commit comments