From 398a2a8d21b9d376f7083f2dcce0c038989adb40 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 23 Oct 2024 10:57:35 +0200 Subject: [PATCH 1/2] PHPLIB-1185: Add new methods to get database and collection instances --- src/Client.php | 47 ++++++++++++++++++++++++++++++++++++++++------- src/Database.php | 36 ++++++++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/src/Client.php b/src/Client.php index f21d09d72..a3ce0eaf1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -174,7 +174,7 @@ public function __debugInfo() */ public function __get(string $databaseName) { - return $this->selectDatabase($databaseName); + return $this->getDatabase($databaseName); } /** @@ -247,6 +247,43 @@ public function dropDatabase(string $databaseName, array $options = []) return $operation->execute($server); } + /** + * Returns a collection instance. + * + * If the collection does not exist in the database, it is not created when + * invoking this method. + * + * @see Collection::__construct() for supported options + * @param string $databaseName Name of the database containing the collection + * @param string $collectionName Name of the collection to select + * @param array $options Collection constructor options + * @throws InvalidArgumentException for parameter/option parsing errors + */ + public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection + { + $options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder]; + + return new Collection($this->manager, $databaseName, $collectionName, $options); + } + + /** + * Returns a database instance. + * + * If the database does not exist on the server, it is not created when + * invoking this method. + * + * @param string $databaseName Name of the database to select + * @param array $options Database constructor options + * @throws InvalidArgumentException for parameter/option parsing errors + * @see Database::__construct() for supported options + */ + public function getDatabase(string $databaseName, array $options = []): Database + { + $options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder]; + + return new Database($this->manager, $databaseName, $options); + } + /** * Return the Manager. * @@ -354,9 +391,7 @@ final public function removeSubscriber(Subscriber $subscriber): void */ public function selectCollection(string $databaseName, string $collectionName, array $options = []) { - $options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder]; - - return new Collection($this->manager, $databaseName, $collectionName, $options); + return $this->getCollection($databaseName, $collectionName, $options); } /** @@ -370,9 +405,7 @@ public function selectCollection(string $databaseName, string $collectionName, a */ public function selectDatabase(string $databaseName, array $options = []) { - $options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder]; - - return new Database($this->manager, $databaseName, $options); + return $this->getDatabase($databaseName, $options); } /** diff --git a/src/Database.php b/src/Database.php index 0561ea943..087fe7ef4 100644 --- a/src/Database.php +++ b/src/Database.php @@ -178,7 +178,7 @@ public function __debugInfo() */ public function __get(string $collectionName) { - return $this->selectCollection($collectionName); + return $this->getCollection($collectionName); } /** @@ -422,6 +422,30 @@ public function dropCollection(string $collectionName, array $options = []) return $operation->execute($server); } + /** + * Returns a collection instance. + * + * If the collection does not exist in the database, it is not created when + * invoking this method. + * + * @param string $collectionName Name of the collection to select + * @param array $options Collection constructor options + * @throws InvalidArgumentException for parameter/option parsing errors + * @see Collection::__construct() for supported options + */ + public function getCollection(string $collectionName, array $options = []): Collection + { + $options += [ + 'builderEncoder' => $this->builderEncoder, + 'readConcern' => $this->readConcern, + 'readPreference' => $this->readPreference, + 'typeMap' => $this->typeMap, + 'writeConcern' => $this->writeConcern, + ]; + + return new Collection($this->manager, $this->databaseName, $collectionName, $options); + } + /** * Returns the database name. * @@ -588,15 +612,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio */ public function selectCollection(string $collectionName, array $options = []) { - $options += [ - 'builderEncoder' => $this->builderEncoder, - 'readConcern' => $this->readConcern, - 'readPreference' => $this->readPreference, - 'typeMap' => $this->typeMap, - 'writeConcern' => $this->writeConcern, - ]; - - return new Collection($this->manager, $this->databaseName, $collectionName, $options); + return $this->getCollection($collectionName, $options); } /** From ef20376b620232b3af1eb1aef956945545f7806b Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 4 Nov 2024 13:29:42 +0100 Subject: [PATCH 2/2] Remove unnecessary phpdoc tags --- src/Client.php | 6 ------ src/Database.php | 4 +--- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Client.php b/src/Client.php index a3ce0eaf1..ae0433f62 100644 --- a/src/Client.php +++ b/src/Client.php @@ -254,9 +254,6 @@ public function dropDatabase(string $databaseName, array $options = []) * invoking this method. * * @see Collection::__construct() for supported options - * @param string $databaseName Name of the database containing the collection - * @param string $collectionName Name of the collection to select - * @param array $options Collection constructor options * @throws InvalidArgumentException for parameter/option parsing errors */ public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection @@ -272,9 +269,6 @@ public function getCollection(string $databaseName, string $collectionName, arra * If the database does not exist on the server, it is not created when * invoking this method. * - * @param string $databaseName Name of the database to select - * @param array $options Database constructor options - * @throws InvalidArgumentException for parameter/option parsing errors * @see Database::__construct() for supported options */ public function getDatabase(string $databaseName, array $options = []): Database diff --git a/src/Database.php b/src/Database.php index 087fe7ef4..36154cfa5 100644 --- a/src/Database.php +++ b/src/Database.php @@ -428,10 +428,8 @@ public function dropCollection(string $collectionName, array $options = []) * If the collection does not exist in the database, it is not created when * invoking this method. * - * @param string $collectionName Name of the collection to select - * @param array $options Collection constructor options - * @throws InvalidArgumentException for parameter/option parsing errors * @see Collection::__construct() for supported options + * @throws InvalidArgumentException for parameter/option parsing errors */ public function getCollection(string $collectionName, array $options = []): Collection {