Skip to content

Commit 5a44dc7

Browse files
committed
Merge pull request #99
2 parents af9bc65 + e61ce5d commit 5a44dc7

11 files changed

+209
-43
lines changed

src/Client.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
class Client
1616
{
17+
private static $defaultTypeMap = [
18+
'array' => 'MongoDB\Model\BSONArray',
19+
'document' => 'MongoDB\Model\BSONDocument',
20+
'root' => 'MongoDB\Model\BSONDocument',
21+
];
22+
1723
private $manager;
1824
private $uri;
1925
private $typeMap;
@@ -41,13 +47,7 @@ class Client
4147
*/
4248
public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
4349
{
44-
$driverOptions += [
45-
'typeMap' => [
46-
'array' => 'MongoDB\Model\BSONArray',
47-
'document' => 'MongoDB\Model\BSONDocument',
48-
'root' => 'MongoDB\Model\BSONDocument',
49-
],
50-
];
50+
$driverOptions += ['typeMap' => self::$defaultTypeMap];
5151

5252
if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
5353
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
@@ -103,12 +103,18 @@ public function __toString()
103103
/**
104104
* Drop a database.
105105
*
106-
* @param string $databaseName
107-
* @return object Command result document
106+
* @see DropDatabase::__construct() for supported options
107+
* @param string $databaseName Database name
108+
* @param array $options Additional options
109+
* @return array|object Command result document
108110
*/
109-
public function dropDatabase($databaseName)
111+
public function dropDatabase($databaseName, array $options = [])
110112
{
111-
$operation = new DropDatabase($databaseName);
113+
if ( ! isset($options['typeMap'])) {
114+
$options['typeMap'] = $this->typeMap;
115+
}
116+
117+
$operation = new DropDatabase($databaseName, $options);
112118
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
113119

114120
return $operation->execute($server);

src/Collection.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636

3737
class Collection
3838
{
39+
private static $defaultTypeMap = [
40+
'array' => 'MongoDB\Model\BSONArray',
41+
'document' => 'MongoDB\Model\BSONDocument',
42+
'root' => 'MongoDB\Model\BSONDocument',
43+
];
3944
private static $wireVersionForFindAndModifyWriteConcern = 4;
4045

4146
private $collectionName;
@@ -102,7 +107,7 @@ public function __construct(Manager $manager, $namespace, array $options = [])
102107
$this->manager = $manager;
103108
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
104109
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
105-
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : null;
110+
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : self::$defaultTypeMap;
106111
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
107112
}
108113

@@ -340,11 +345,17 @@ public function distinct($fieldName, $filter = [], array $options = [])
340345
/**
341346
* Drop this collection.
342347
*
343-
* @return object Command result document
348+
* @see DropCollection::__construct() for supported options
349+
* @param array $options Additional options
350+
* @return array|object Command result document
344351
*/
345-
public function drop()
352+
public function drop(array $options = [])
346353
{
347-
$operation = new DropCollection($this->databaseName, $this->collectionName);
354+
if ( ! isset($options['typeMap'])) {
355+
$options['typeMap'] = $this->typeMap;
356+
}
357+
358+
$operation = new DropCollection($this->databaseName, $this->collectionName, $options);
348359
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
349360

350361
return $operation->execute($server);
@@ -353,19 +364,25 @@ public function drop()
353364
/**
354365
* Drop a single index in the collection.
355366
*
367+
* @see DropIndexes::__construct() for supported options
356368
* @param string $indexName Index name
357-
* @return object Command result document
369+
* @param array $options Additional options
370+
* @return array|object Command result document
358371
* @throws InvalidArgumentException if $indexName is an empty string or "*"
359372
*/
360-
public function dropIndex($indexName)
373+
public function dropIndex($indexName, array $options = [])
361374
{
362375
$indexName = (string) $indexName;
363376

364377
if ($indexName === '*') {
365378
throw new InvalidArgumentException('dropIndexes() must be used to drop multiple indexes');
366379
}
367380

368-
$operation = new DropIndexes($this->databaseName, $this->collectionName, $indexName);
381+
if ( ! isset($options['typeMap'])) {
382+
$options['typeMap'] = $this->typeMap;
383+
}
384+
385+
$operation = new DropIndexes($this->databaseName, $this->collectionName, $indexName, $options);
369386
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
370387

371388
return $operation->execute($server);
@@ -374,11 +391,17 @@ public function dropIndex($indexName)
374391
/**
375392
* Drop all indexes in the collection.
376393
*
377-
* @return object Command result document
394+
* @see DropIndexes::__construct() for supported options
395+
* @param array $options Additional options
396+
* @return array|object Command result document
378397
*/
379-
public function dropIndexes()
398+
public function dropIndexes(array $options = [])
380399
{
381-
$operation = new DropIndexes($this->databaseName, $this->collectionName, '*');
400+
if ( ! isset($options['typeMap'])) {
401+
$options['typeMap'] = $this->typeMap;
402+
}
403+
404+
$operation = new DropIndexes($this->databaseName, $this->collectionName, '*', $options);
382405
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
383406

384407
return $operation->execute($server);

src/Database.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020

2121
class Database
2222
{
23+
private static $defaultTypeMap = [
24+
'array' => 'MongoDB\Model\BSONArray',
25+
'document' => 'MongoDB\Model\BSONDocument',
26+
'root' => 'MongoDB\Model\BSONDocument',
27+
];
28+
2329
private $databaseName;
2430
private $manager;
2531
private $readConcern;
@@ -80,7 +86,7 @@ public function __construct(Manager $manager, $databaseName, array $options = []
8086
$this->databaseName = (string) $databaseName;
8187
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
8288
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
83-
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : null;
89+
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : self::$defaultTypeMap;
8490
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
8591
}
8692

@@ -144,6 +150,10 @@ public function command($command, array $options = [])
144150
$options['readPreference'] = $this->readPreference;
145151
}
146152

153+
if ( ! isset($options['typeMap'])) {
154+
$options['typeMap'] = $this->typeMap;
155+
}
156+
147157
$operation = new DatabaseCommand($this->databaseName, $command, $options);
148158
$server = $this->manager->selectServer($options['readPreference']);
149159

@@ -169,11 +179,17 @@ public function createCollection($collectionName, array $options = [])
169179
/**
170180
* Drop this database.
171181
*
172-
* @return object Command result document
182+
* @see DropDatabase::__construct() for supported options
183+
* @param array $options Additional options
184+
* @return array|object Command result document
173185
*/
174-
public function drop()
186+
public function drop(array $options = [])
175187
{
176-
$operation = new DropDatabase($this->databaseName);
188+
if ( ! isset($options['typeMap'])) {
189+
$options['typeMap'] = $this->typeMap;
190+
}
191+
192+
$operation = new DropDatabase($this->databaseName, $options);
177193
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
178194

179195
return $operation->execute($server);
@@ -182,12 +198,18 @@ public function drop()
182198
/**
183199
* Drop a collection within this database.
184200
*
185-
* @param string $collectionName
186-
* @return object Command result document
201+
* @see DropCollection::__construct() for supported options
202+
* @param string $collectionName Collection name
203+
* @param array $options Additional options
204+
* @return array|object Command result document
187205
*/
188-
public function dropCollection($collectionName)
206+
public function dropCollection($collectionName, array $options = [])
189207
{
190-
$operation = new DropCollection($this->databaseName, $collectionName);
208+
if ( ! isset($options['typeMap'])) {
209+
$options['typeMap'] = $this->typeMap;
210+
}
211+
212+
$operation = new DropCollection($this->databaseName, $collectionName, $options);
191213
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
192214

193215
return $operation->execute($server);

src/Operation/DropCollection.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
77
use MongoDB\Driver\Exception\RuntimeException;
8+
use MongoDB\Exception\InvalidArgumentException;
89

910
/**
1011
* Operation for the drop command.
@@ -17,27 +18,40 @@
1718
class DropCollection implements Executable
1819
{
1920
private static $errorMessageNamespaceNotFound = 'ns not found';
21+
2022
private $databaseName;
2123
private $collectionName;
24+
private $options;
2225

2326
/**
2427
* Constructs a drop command.
2528
*
29+
* Supported options:
30+
*
31+
* * typeMap (array): Type map for BSON deserialization. This will be used
32+
* for the returned command result document.
33+
*
2634
* @param string $databaseName Database name
2735
* @param string $collectionName Collection name
36+
* @param array $options Command options
2837
*/
29-
public function __construct($databaseName, $collectionName)
38+
public function __construct($databaseName, $collectionName, array $options = [])
3039
{
40+
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
41+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
42+
}
43+
3144
$this->databaseName = (string) $databaseName;
3245
$this->collectionName = (string) $collectionName;
46+
$this->options = $options;
3347
}
3448

3549
/**
3650
* Execute the operation.
3751
*
3852
* @see Executable::execute()
3953
* @param Server $server
40-
* @return object Command result document
54+
* @return array|object Command result document
4155
*/
4256
public function execute(Server $server)
4357
{
@@ -55,6 +69,10 @@ public function execute(Server $server)
5569
throw $e;
5670
}
5771

72+
if (isset($this->options['typeMap'])) {
73+
$cursor->setTypeMap($this->options['typeMap']);
74+
}
75+
5876
return current($cursor->toArray());
5977
}
6078
}

src/Operation/DropDatabase.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
7+
use MongoDB\Exception\InvalidArgumentException;
78

89
/**
910
* Operation for the dropDatabase command.
@@ -16,28 +17,39 @@
1617
class DropDatabase implements Executable
1718
{
1819
private $databaseName;
20+
private $options;
1921

2022
/**
2123
* Constructs a dropDatabase command.
2224
*
2325
* @param string $databaseName Database name
26+
* @param array $options Command options
2427
*/
25-
public function __construct($databaseName)
28+
public function __construct($databaseName, array $options = [])
2629
{
30+
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
31+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
32+
}
33+
2734
$this->databaseName = (string) $databaseName;
35+
$this->options = $options;
2836
}
2937

3038
/**
3139
* Execute the operation.
3240
*
3341
* @see Executable::execute()
3442
* @param Server $server
35-
* @return object Command result document
43+
* @return array|object Command result document
3644
*/
3745
public function execute(Server $server)
3846
{
3947
$cursor = $server->executeCommand($this->databaseName, new Command(['dropDatabase' => 1]));
4048

49+
if (isset($this->options['typeMap'])) {
50+
$cursor->setTypeMap($this->options['typeMap']);
51+
}
52+
4153
return current($cursor->toArray());
4254
}
4355
}

src/Operation/DropIndexes.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,41 @@ class DropIndexes implements Executable
1818
private $databaseName;
1919
private $collectionName;
2020
private $indexName;
21+
private $options;
2122

2223
/**
2324
* Constructs a dropIndexes command.
2425
*
2526
* @param string $databaseName Database name
2627
* @param string $collectionName Collection name
2728
* @param string $indexName Index name (use "*" to drop all indexes)
29+
* @param array $options Command options
2830
* @throws InvalidArgumentException
2931
*/
30-
public function __construct($databaseName, $collectionName, $indexName)
32+
public function __construct($databaseName, $collectionName, $indexName, array $options = [])
3133
{
3234
$indexName = (string) $indexName;
3335

3436
if ($indexName === '') {
3537
throw new InvalidArgumentException('$indexName cannot be empty');
3638
}
3739

40+
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
41+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
42+
}
43+
3844
$this->databaseName = (string) $databaseName;
3945
$this->collectionName = (string) $collectionName;
4046
$this->indexName = $indexName;
47+
$this->options = $options;
4148
}
4249

4350
/**
4451
* Execute the operation.
4552
*
4653
* @see Executable::execute()
4754
* @param Server $server
48-
* @return object Command result document
55+
* @return array|object Command result document
4956
*/
5057
public function execute(Server $server)
5158
{
@@ -56,6 +63,10 @@ public function execute(Server $server)
5663

5764
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
5865

66+
if (isset($this->options['typeMap'])) {
67+
$cursor->setTypeMap($this->options['typeMap']);
68+
}
69+
5970
return current($cursor->toArray());
6071
}
6172
}

tests/Collection/CollectionFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ public function testFindOne()
115115
'sort' => ['x' => -1],
116116
];
117117

118-
$expected = (object) ['_id' => 3, 'x' => 33];
118+
$expected = ['_id' => 3, 'x' => 33];
119119

120-
$this->assertEquals($expected, $this->collection->findOne($filter, $options));
120+
$this->assertSameDocument($expected, $this->collection->findOne($filter, $options));
121121
}
122122

123123
public function testWithOptionsInheritsReadPreferenceAndWriteConcern()

0 commit comments

Comments
 (0)