Skip to content

Commit

Permalink
PHPLIB-1185: Add new methods to get database and collection instances (
Browse files Browse the repository at this point in the history
…#1494)

* PHPLIB-1185: Add new methods to get database and collection instances

* Remove unnecessary phpdoc tags
  • Loading branch information
alcaeus authored Nov 4, 2024
1 parent ca9f9a1 commit 4c7b1cb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
41 changes: 34 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function __debugInfo()
*/
public function __get(string $databaseName)
{
return $this->selectDatabase($databaseName);
return $this->getDatabase($databaseName);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
34 changes: 24 additions & 10 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function __debugInfo()
*/
public function __get(string $collectionName)
{
return $this->selectCollection($collectionName);
return $this->getCollection($collectionName);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 4c7b1cb

Please sign in to comment.