From 4c7b1cbe903fbb3aff77158692097d54d8e79f93 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 4 Nov 2024 13:55:06 +0100 Subject: [PATCH] PHPLIB-1185: Add new methods to get database and collection instances (#1494) * PHPLIB-1185: Add new methods to get database and collection instances * Remove unnecessary phpdoc tags --- src/Client.php | 41 ++++++++++++++++++++++++++++++++++------- src/Database.php | 34 ++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/Client.php b/src/Client.php index f21d09d72..ae0433f62 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,37 @@ 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 + * @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. + * + * @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 +385,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 +399,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..36154cfa5 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,28 @@ 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. + * + * @see Collection::__construct() for supported options + * @throws InvalidArgumentException for parameter/option parsing errors + */ + 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 +610,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); } /**