From 8fdf974c55693c69884812805d71fe5c7830e6cf Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Fri, 2 Feb 2018 10:05:14 +0200 Subject: [PATCH] Fetch database from mongo dsn. --- src/CollectionFactory.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/CollectionFactory.php diff --git a/src/CollectionFactory.php b/src/CollectionFactory.php new file mode 100644 index 0000000..216ecdd --- /dev/null +++ b/src/CollectionFactory.php @@ -0,0 +1,37 @@ +mongodb = $mongodb; + $this->mongoDsn = $mongoDsn; + } + + public function create(string $collectionName, string $databaseName = null, array $options = []): Collection + { + if (false == $databaseName) { + $databaseName = parse_url($this->mongoDsn, PHP_URL_PATH); + } + + if (false == $databaseName) { + throw new \LogicException('Failed to guess database name, neither mongo DSN nor argument have it.'); + } + + $this->mongodb->selectCollection($databaseName, $collectionName, $options); + } +}