Skip to content

Commit 4c7b1cb

Browse files
authored
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
1 parent ca9f9a1 commit 4c7b1cb

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/Client.php

+34-7
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,37 @@ 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+
* @throws InvalidArgumentException for parameter/option parsing errors
258+
*/
259+
public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection
260+
{
261+
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
262+
263+
return new Collection($this->manager, $databaseName, $collectionName, $options);
264+
}
265+
266+
/**
267+
* Returns a database instance.
268+
*
269+
* If the database does not exist on the server, it is not created when
270+
* invoking this method.
271+
*
272+
* @see Database::__construct() for supported options
273+
*/
274+
public function getDatabase(string $databaseName, array $options = []): Database
275+
{
276+
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
277+
278+
return new Database($this->manager, $databaseName, $options);
279+
}
280+
250281
/**
251282
* Return the Manager.
252283
*
@@ -354,9 +385,7 @@ final public function removeSubscriber(Subscriber $subscriber): void
354385
*/
355386
public function selectCollection(string $databaseName, string $collectionName, array $options = [])
356387
{
357-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
358-
359-
return new Collection($this->manager, $databaseName, $collectionName, $options);
388+
return $this->getCollection($databaseName, $collectionName, $options);
360389
}
361390

362391
/**
@@ -370,9 +399,7 @@ public function selectCollection(string $databaseName, string $collectionName, a
370399
*/
371400
public function selectDatabase(string $databaseName, array $options = [])
372401
{
373-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
374-
375-
return new Database($this->manager, $databaseName, $options);
402+
return $this->getDatabase($databaseName, $options);
376403
}
377404

378405
/**

src/Database.php

+24-10
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,28 @@ 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+
* @see Collection::__construct() for supported options
432+
* @throws InvalidArgumentException for parameter/option parsing errors
433+
*/
434+
public function getCollection(string $collectionName, array $options = []): Collection
435+
{
436+
$options += [
437+
'builderEncoder' => $this->builderEncoder,
438+
'readConcern' => $this->readConcern,
439+
'readPreference' => $this->readPreference,
440+
'typeMap' => $this->typeMap,
441+
'writeConcern' => $this->writeConcern,
442+
];
443+
444+
return new Collection($this->manager, $this->databaseName, $collectionName, $options);
445+
}
446+
425447
/**
426448
* Returns the database name.
427449
*
@@ -588,15 +610,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
588610
*/
589611
public function selectCollection(string $collectionName, array $options = [])
590612
{
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);
613+
return $this->getCollection($collectionName, $options);
600614
}
601615

602616
/**

0 commit comments

Comments
 (0)