@@ -72,21 +72,21 @@ class Collection
7272 * to use for collection operations. Defaults to the Manager's write
7373 * concern.
7474 *
75- * @param Manager $manager Manager instance from the driver
76- * @param string $namespace Collection namespace (e.g. "db.collection")
77- * @param array $options Collection options
75+ * @param Manager $manager Manager instance from the driver
76+ * @param string $databaseName Database name
77+ * @param string $collectionName Collection name
78+ * @param array $options Collection options
7879 * @throws InvalidArgumentException
7980 */
80- public function __construct (Manager $ manager , $ namespace , array $ options = [])
81+ public function __construct (Manager $ manager , $ databaseName , $ collectionName , array $ options = [])
8182 {
82- $ parts = explode ('. ' , $ namespace , 2 );
83-
84- if (count ($ parts ) != 2 || strlen ($ parts [0 ]) == 0 || strlen ($ parts [1 ]) == 0 ) {
85- throw new InvalidArgumentException ('$namespace is invalid: ' . $ namespace );
83+ if (strlen ($ databaseName ) < 1 ) {
84+ throw new InvalidArgumentException ('$databaseName is invalid: ' . $ databaseName );
8685 }
8786
88- $ this ->databaseName = $ parts [0 ];
89- $ this ->collectionName = $ parts [1 ];
87+ if (strlen ($ collectionName ) < 1 ) {
88+ throw new InvalidArgumentException ('$collectionName is invalid: ' . $ collectionName );
89+ }
9090
9191 if (isset ($ options ['readConcern ' ]) && ! $ options ['readConcern ' ] instanceof ReadConcern) {
9292 throw InvalidArgumentException::invalidType ('"readConcern" option ' , $ options ['readConcern ' ], 'MongoDB\Driver\ReadConcern ' );
@@ -105,6 +105,8 @@ public function __construct(Manager $manager, $namespace, array $options = [])
105105 }
106106
107107 $ this ->manager = $ manager ;
108+ $ this ->databaseName = (string ) $ databaseName ;
109+ $ this ->collectionName = (string ) $ collectionName ;
108110 $ this ->readConcern = isset ($ options ['readConcern ' ]) ? $ options ['readConcern ' ] : $ this ->manager ->getReadConcern ();
109111 $ this ->readPreference = isset ($ options ['readPreference ' ]) ? $ options ['readPreference ' ] : $ this ->manager ->getReadPreference ();
110112 $ this ->typeMap = isset ($ options ['typeMap ' ]) ? $ options ['typeMap ' ] : self ::$ defaultTypeMap ;
@@ -133,6 +135,7 @@ public function __debugInfo()
133135 /**
134136 * Return the collection namespace (e.g. "db.collection").
135137 *
138+ * @see https://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace
136139 * @param string
137140 */
138141 public function __toString ()
@@ -723,6 +726,6 @@ public function withOptions(array $options = [])
723726 'writeConcern ' => $ this ->writeConcern ,
724727 ];
725728
726- return new Collection ($ this ->manager , $ this ->databaseName . ' . ' . $ this ->collectionName , $ options );
729+ return new Collection ($ this ->manager , $ this ->databaseName , $ this ->collectionName , $ options );
727730 }
728731}
0 commit comments