Skip to content

Commit e33b967

Browse files
committed
PHPLIB-1185: Add new methods to get database and collection instances
1 parent 04857b1 commit e33b967

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

src/Client.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function __debugInfo()
174174
*/
175175
public function __get(string $databaseName)
176176
{
177-
return $this->selectDatabase($databaseName);
177+
return $this->getDatabase($databaseName);
178178
}
179179

180180
/**
@@ -247,6 +247,43 @@ public function dropDatabase(string $databaseName, array $options = [])
247247
return $operation->execute($server);
248248
}
249249

250+
/**
251+
* Returns a collection instance.
252+
*
253+
* If the collection does not exist in the database, it is not created when
254+
* invoking this method.
255+
*
256+
* @see Collection::__construct() for supported options
257+
* @param string $databaseName Name of the database containing the collection
258+
* @param string $collectionName Name of the collection to select
259+
* @param array $options Collection constructor options
260+
* @throws InvalidArgumentException for parameter/option parsing errors
261+
*/
262+
public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection
263+
{
264+
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
265+
266+
return new Collection($this->manager, $databaseName, $collectionName, $options);
267+
}
268+
269+
/**
270+
* Returns a database instance.
271+
*
272+
* If the database does not exist on the server, it is not created when
273+
* invoking this method.
274+
*
275+
* @param string $databaseName Name of the database to select
276+
* @param array $options Database constructor options
277+
* @throws InvalidArgumentException for parameter/option parsing errors
278+
* @see Database::__construct() for supported options
279+
*/
280+
public function getDatabase(string $databaseName, array $options = []): Database
281+
{
282+
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
283+
284+
return new Database($this->manager, $databaseName, $options);
285+
}
286+
250287
/**
251288
* Return the Manager.
252289
*
@@ -354,9 +391,7 @@ final public function removeSubscriber(Subscriber $subscriber): void
354391
*/
355392
public function selectCollection(string $databaseName, string $collectionName, array $options = [])
356393
{
357-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
358-
359-
return new Collection($this->manager, $databaseName, $collectionName, $options);
394+
return $this->getCollection($databaseName, $collectionName, $options);
360395
}
361396

362397
/**
@@ -370,9 +405,7 @@ public function selectCollection(string $databaseName, string $collectionName, a
370405
*/
371406
public function selectDatabase(string $databaseName, array $options = [])
372407
{
373-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
374-
375-
return new Database($this->manager, $databaseName, $options);
408+
return $this->getDatabase($databaseName, $options);
376409
}
377410

378411
/**

src/Database.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function __debugInfo()
178178
*/
179179
public function __get(string $collectionName)
180180
{
181-
return $this->selectCollection($collectionName);
181+
return $this->getCollection($collectionName);
182182
}
183183

184184
/**
@@ -422,6 +422,30 @@ public function dropCollection(string $collectionName, array $options = [])
422422
return $operation->execute($server);
423423
}
424424

425+
/**
426+
* Returns a collection instance.
427+
*
428+
* If the collection does not exist in the database, it is not created when
429+
* invoking this method.
430+
*
431+
* @param string $collectionName Name of the collection to select
432+
* @param array $options Collection constructor options
433+
* @throws InvalidArgumentException for parameter/option parsing errors
434+
* @see Collection::__construct() for supported options
435+
*/
436+
public function getCollection(string $collectionName, array $options = []): Collection
437+
{
438+
$options += [
439+
'builderEncoder' => $this->builderEncoder,
440+
'readConcern' => $this->readConcern,
441+
'readPreference' => $this->readPreference,
442+
'typeMap' => $this->typeMap,
443+
'writeConcern' => $this->writeConcern,
444+
];
445+
446+
return new Collection($this->manager, $this->databaseName, $collectionName, $options);
447+
}
448+
425449
/**
426450
* Returns the database name.
427451
*
@@ -588,15 +612,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
588612
*/
589613
public function selectCollection(string $collectionName, array $options = [])
590614
{
591-
$options += [
592-
'builderEncoder' => $this->builderEncoder,
593-
'readConcern' => $this->readConcern,
594-
'readPreference' => $this->readPreference,
595-
'typeMap' => $this->typeMap,
596-
'writeConcern' => $this->writeConcern,
597-
];
598-
599-
return new Collection($this->manager, $this->databaseName, $collectionName, $options);
615+
return $this->getCollection($collectionName, $options);
600616
}
601617

602618
/**

0 commit comments

Comments
 (0)