Skip to content

Commit 294e4b6

Browse files
Merge v1.x into v2.x (#1518)
2 parents 9def5c5 + 4c7b1cb commit 294e4b6

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/Client.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function __debugInfo(): array
168168
*/
169169
public function __get(string $databaseName): Database
170170
{
171-
return $this->selectDatabase($databaseName);
171+
return $this->getDatabase($databaseName);
172172
}
173173

174174
/**
@@ -230,6 +230,37 @@ public function dropDatabase(string $databaseName, array $options = []): void
230230
$operation->execute($server);
231231
}
232232

233+
/**
234+
* Returns a collection instance.
235+
*
236+
* If the collection does not exist in the database, it is not created when
237+
* invoking this method.
238+
*
239+
* @see Collection::__construct() for supported options
240+
* @throws InvalidArgumentException for parameter/option parsing errors
241+
*/
242+
public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection
243+
{
244+
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
245+
246+
return new Collection($this->manager, $databaseName, $collectionName, $options);
247+
}
248+
249+
/**
250+
* Returns a database instance.
251+
*
252+
* If the database does not exist on the server, it is not created when
253+
* invoking this method.
254+
*
255+
* @see Database::__construct() for supported options
256+
*/
257+
public function getDatabase(string $databaseName, array $options = []): Database
258+
{
259+
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
260+
261+
return new Database($this->manager, $databaseName, $options);
262+
}
263+
233264
/**
234265
* Return the Manager.
235266
*/
@@ -329,9 +360,7 @@ final public function removeSubscriber(Subscriber $subscriber): void
329360
*/
330361
public function selectCollection(string $databaseName, string $collectionName, array $options = []): Collection
331362
{
332-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
333-
334-
return new Collection($this->manager, $databaseName, $collectionName, $options);
363+
return $this->getCollection($databaseName, $collectionName, $options);
335364
}
336365

337366
/**
@@ -344,9 +373,7 @@ public function selectCollection(string $databaseName, string $collectionName, a
344373
*/
345374
public function selectDatabase(string $databaseName, array $options = []): Database
346375
{
347-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
348-
349-
return new Database($this->manager, $databaseName, $options);
376+
return $this->getDatabase($databaseName, $options);
350377
}
351378

352379
/**

src/Database.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function __debugInfo(): array
171171
*/
172172
public function __get(string $collectionName): Collection
173173
{
174-
return $this->selectCollection($collectionName);
174+
return $this->getCollection($collectionName);
175175
}
176176

177177
/**
@@ -384,6 +384,28 @@ public function dropCollection(string $collectionName, array $options = []): voi
384384
$operation->execute($server);
385385
}
386386

387+
/**
388+
* Returns a collection instance.
389+
*
390+
* If the collection does not exist in the database, it is not created when
391+
* invoking this method.
392+
*
393+
* @see Collection::__construct() for supported options
394+
* @throws InvalidArgumentException for parameter/option parsing errors
395+
*/
396+
public function getCollection(string $collectionName, array $options = []): Collection
397+
{
398+
$options += [
399+
'builderEncoder' => $this->builderEncoder,
400+
'readConcern' => $this->readConcern,
401+
'readPreference' => $this->readPreference,
402+
'typeMap' => $this->typeMap,
403+
'writeConcern' => $this->writeConcern,
404+
];
405+
406+
return new Collection($this->manager, $this->databaseName, $collectionName, $options);
407+
}
408+
387409
/**
388410
* Returns the database name.
389411
*/
@@ -534,15 +556,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
534556
*/
535557
public function selectCollection(string $collectionName, array $options = []): Collection
536558
{
537-
$options += [
538-
'builderEncoder' => $this->builderEncoder,
539-
'readConcern' => $this->readConcern,
540-
'readPreference' => $this->readPreference,
541-
'typeMap' => $this->typeMap,
542-
'writeConcern' => $this->writeConcern,
543-
];
544-
545-
return new Collection($this->manager, $this->databaseName, $collectionName, $options);
559+
return $this->getCollection($collectionName, $options);
546560
}
547561

548562
/**

0 commit comments

Comments
 (0)