Skip to content

Commit 57e05c1

Browse files
committed
Merge pull request #51
2 parents be188e3 + 52b81e9 commit 57e05c1

File tree

8 files changed

+437
-59
lines changed

8 files changed

+437
-59
lines changed

src/Client.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ public function __construct($uri = 'mongodb://localhost:27017', array $options =
3434
$this->uri = (string) $uri;
3535
}
3636

37+
/**
38+
* Return internal properties for debugging purposes.
39+
*
40+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
41+
* @param array
42+
*/
43+
public function __debugInfo()
44+
{
45+
return [
46+
'manager' => $this->manager,
47+
'uri' => $this->uri,
48+
];
49+
}
50+
3751
/**
3852
* Return the connection string (i.e. URI).
3953
*
@@ -75,40 +89,45 @@ public function listDatabases(array $options = [])
7589
/**
7690
* Select a collection.
7791
*
78-
* If a write concern or read preference is not specified, the write concern
79-
* or read preference of the Client will be applied, respectively.
92+
* Supported options:
8093
*
81-
* @param string $databaseName Name of the database containing the collection
82-
* @param string $collectionName Name of the collection to select
83-
* @param WriteConcern $writeConcern Default write concern to apply
84-
* @param ReadPreference $readPreference Default read preference to apply
94+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
95+
* preference to use for collection operations. Defaults to the Client's
96+
* read preference.
97+
*
98+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
99+
* to use for collection operations. Defaults to the Client's write
100+
* concern.
101+
*
102+
* @param string $databaseName Name of the database containing the collection
103+
* @param string $collectionName Name of the collection to select
104+
* @param array $options Collection constructor options
85105
* @return Collection
86106
*/
87-
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
107+
public function selectCollection($databaseName, $collectionName, array $options = [])
88108
{
89-
$namespace = $databaseName . '.' . $collectionName;
90-
$writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
91-
$readPreference = $readPreference ?: $this->manager->getReadPreference();
92-
93-
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
109+
return new Collection($this->manager, $databaseName . '.' . $collectionName, $options);
94110
}
95111

96112
/**
97113
* Select a database.
98114
*
99-
* If a write concern or read preference is not specified, the write concern
100-
* or read preference of the Client will be applied, respectively.
115+
* Supported options:
101116
*
102-
* @param string $databaseName Name of the database to select
103-
* @param WriteConcern $writeConcern Default write concern to apply
104-
* @param ReadPreference $readPreference Default read preference to apply
117+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
118+
* preference to use for database operations and selected collections.
119+
* Defaults to the Client's read preference.
120+
*
121+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
122+
* to use for database operations and selected collections. Defaults to
123+
* the Client's write concern.
124+
*
125+
* @param string $databaseName Name of the database to select
126+
* @param array $options Database constructor options
105127
* @return Database
106128
*/
107-
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
129+
public function selectDatabase($databaseName, array $options = [])
108130
{
109-
$writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
110-
$readPreference = $readPreference ?: $this->manager->getReadPreference();
111-
112-
return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
131+
return new Database($this->manager, $databaseName, $options);
113132
}
114133
}

src/Collection.php

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MongoDB\Driver\Server;
1010
use MongoDB\Driver\WriteConcern;
1111
use MongoDB\Exception\InvalidArgumentException;
12+
use MongoDB\Exception\InvalidArgumentTypeException;
1213
use MongoDB\Exception\UnexpectedTypeException;
1314
use MongoDB\Model\IndexInfoIterator;
1415
use MongoDB\Model\IndexInput;
@@ -48,13 +49,22 @@ class Collection
4849
* This class provides methods for collection-specific operations, such as
4950
* CRUD (i.e. create, read, update, and delete) and index management.
5051
*
51-
* @param Manager $manager Manager instance from the driver
52-
* @param string $namespace Collection namespace (e.g. "db.collection")
53-
* @param WriteConcern $writeConcern Default write concern to apply
54-
* @param ReadPreference $readPreference Default read preference to apply
55-
* @throws InvalidArgumentException if $namespace is invalid
52+
* Supported options:
53+
*
54+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
55+
* preference to use for collection operations. Defaults to the Manager's
56+
* read preference.
57+
*
58+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
59+
* to use for collection operations. Defaults to the Manager's write
60+
* concern.
61+
*
62+
* @param Manager $manager Manager instance from the driver
63+
* @param string $namespace Collection namespace (e.g. "db.collection")
64+
* @param array $options Collection options
65+
* @throws InvalidArgumentException
5666
*/
57-
public function __construct(Manager $manager, $namespace, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
67+
public function __construct(Manager $manager, $namespace, array $options = [])
5868
{
5969
$parts = explode('.', $namespace, 2);
6070

@@ -65,13 +75,38 @@ public function __construct(Manager $manager, $namespace, WriteConcern $writeCon
6575
$this->databaseName = $parts[0];
6676
$this->collectionName = $parts[1];
6777

78+
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
79+
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
80+
}
81+
82+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
83+
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
84+
}
85+
6886
$this->manager = $manager;
69-
$this->writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
70-
$this->readPreference = $readPreference ?: $this->manager->getReadPreference();
87+
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
88+
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
7189
}
7290

7391
/**
74-
* Return the collection namespace.
92+
* Return internal properties for debugging purposes.
93+
*
94+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
95+
* @param array
96+
*/
97+
public function __debugInfo()
98+
{
99+
return [
100+
'collectionName' => $this->collectionName,
101+
'databaseName' => $this->databaseName,
102+
'manager' => $this->manager,
103+
'readPreference' => $this->readPreference,
104+
'writeConcern' => $this->writeConcern,
105+
];
106+
}
107+
108+
/**
109+
* Return the collection namespace (e.g. "db.collection").
75110
*
76111
* @param string
77112
*/
@@ -563,4 +598,33 @@ public function updateOne($filter, $update, array $options = [])
563598

564599
return $operation->execute($server);
565600
}
601+
602+
/**
603+
* Get a clone of this collection with different options.
604+
*
605+
* Supported options:
606+
*
607+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
608+
* preference to use for collection operations. Defaults to this
609+
* Collection's read preference.
610+
*
611+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
612+
* to use for collection operations. Defaults to this Collection's write
613+
* concern.
614+
*
615+
* @param array $options Collection constructor options
616+
* @return Collection
617+
*/
618+
public function withOptions(array $options = [])
619+
{
620+
if ( ! isset($options['readPreference'])) {
621+
$options['readPreference'] = $this->readPreference;
622+
}
623+
624+
if ( ! isset($options['writeConcern'])) {
625+
$options['writeConcern'] = $this->writeConcern;
626+
}
627+
628+
return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
629+
}
566630
}

src/Database.php

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use MongoDB\Driver\Server;
1212
use MongoDB\Driver\WriteConcern;
1313
use MongoDB\Exception\InvalidArgumentException;
14+
use MongoDB\Exception\InvalidArgumentTypeException;
1415
use MongoDB\Model\CollectionInfoIterator;
1516
use MongoDB\Operation\CreateCollection;
1617
use MongoDB\Operation\DropCollection;
@@ -30,22 +31,55 @@ class Database
3031
* This class provides methods for database-specific operations and serves
3132
* as a gateway for accessing collections.
3233
*
33-
* @param Manager $manager Manager instance from the driver
34-
* @param string $databaseName Database name
35-
* @param WriteConcern $writeConcern Default write concern to apply
36-
* @param ReadPreference $readPreference Default read preference to apply
37-
* @throws InvalidArgumentException if $databaseName is invalid
34+
* Supported options:
35+
*
36+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
37+
* preference to use for database operations and selected collections.
38+
* Defaults to the Manager's read preference.
39+
*
40+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
41+
* to use for database operations and selected collections. Defaults to
42+
* the Manager's write concern.
43+
*
44+
* @param Manager $manager Manager instance from the driver
45+
* @param string $databaseName Database name
46+
* @param array $options Database options
47+
* @throws InvalidArgumentException
3848
*/
39-
public function __construct(Manager $manager, $databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
49+
public function __construct(Manager $manager, $databaseName, array $options = [])
4050
{
4151
if (strlen($databaseName) < 1) {
4252
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
4353
}
4454

55+
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
56+
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
57+
}
58+
59+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
60+
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
61+
}
62+
4563
$this->manager = $manager;
4664
$this->databaseName = (string) $databaseName;
47-
$this->writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
48-
$this->readPreference = $readPreference ?: $this->manager->getReadPreference();
65+
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
66+
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
67+
}
68+
69+
/**
70+
* Return internal properties for debugging purposes.
71+
*
72+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
73+
* @param array
74+
*/
75+
public function __debugInfo()
76+
{
77+
return [
78+
'databaseName' => $this->databaseName,
79+
'manager' => $this->manager,
80+
'readPreference' => $this->readPreference,
81+
'writeConcern' => $this->writeConcern,
82+
];
4983
}
5084

5185
/**
@@ -129,20 +163,59 @@ public function listCollections(array $options = [])
129163
/**
130164
* Select a collection within this database.
131165
*
132-
* If a write concern or read preference is not specified, the write concern
133-
* or read preference of the Database will be applied, respectively.
166+
* Supported options:
167+
*
168+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
169+
* preference to use for collection operations. Defaults to the
170+
* Database's read preference.
134171
*
135-
* @param string $collectionName Name of the collection to select
136-
* @param WriteConcern $writeConcern Default write concern to apply
137-
* @param ReadPreference $readPreference Default read preference to apply
172+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
173+
* to use for collection operations. Defaults to the Database's write
174+
* concern.
175+
*
176+
* @param string $collectionName Name of the collection to select
177+
* @param array $options Collection constructor options
138178
* @return Collection
139179
*/
140-
public function selectCollection($collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
180+
public function selectCollection($collectionName, array $options = [])
141181
{
142-
$namespace = $this->databaseName . '.' . $collectionName;
143-
$writeConcern = $writeConcern ?: $this->writeConcern;
144-
$readPreference = $readPreference ?: $this->readPreference;
182+
if ( ! isset($options['readPreference'])) {
183+
$options['readPreference'] = $this->readPreference;
184+
}
185+
186+
if ( ! isset($options['writeConcern'])) {
187+
$options['writeConcern'] = $this->writeConcern;
188+
}
189+
190+
return new Collection($this->manager, $this->databaseName . '.' . $collectionName, $options);
191+
}
192+
193+
/**
194+
* Get a clone of this database with different options.
195+
*
196+
* Supported options:
197+
*
198+
* * readPreference (MongoDB\Driver\ReadPreference): The default read
199+
* preference to use for database operations and selected collections.
200+
* Defaults to this Database's read preference.
201+
*
202+
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
203+
* to use for database operations and selected collections. Defaults to
204+
* this Database's write concern.
205+
*
206+
* @param array $options Database constructor options
207+
* @return Database
208+
*/
209+
public function withOptions(array $options = [])
210+
{
211+
if ( ! isset($options['readPreference'])) {
212+
$options['readPreference'] = $this->readPreference;
213+
}
214+
215+
if ( ! isset($options['writeConcern'])) {
216+
$options['writeConcern'] = $this->writeConcern;
217+
}
145218

146-
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
219+
return new Database($this->manager, $this->databaseName, $options);
147220
}
148221
}

0 commit comments

Comments
 (0)