Skip to content

Commit 573f1e7

Browse files
committed
PHPLIB-164: Drop operations should use type map
1 parent 1ea44f9 commit 573f1e7

File tree

9 files changed

+179
-26
lines changed

9 files changed

+179
-26
lines changed

src/Client.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,17 @@ public function distinct($fieldName, $filter = [], array $options = [])
340340
/**
341341
* Drop this collection.
342342
*
343-
* @return object Command result document
343+
* @see DropCollection::__construct() for supported options
344+
* @param array $options Additional options
345+
* @return array|object Command result document
344346
*/
345-
public function drop()
347+
public function drop(array $options = [])
346348
{
347-
$operation = new DropCollection($this->databaseName, $this->collectionName);
349+
if ( ! isset($options['typeMap'])) {
350+
$options['typeMap'] = $this->typeMap;
351+
}
352+
353+
$operation = new DropCollection($this->databaseName, $this->collectionName, $options);
348354
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
349355

350356
return $operation->execute($server);
@@ -353,19 +359,25 @@ public function drop()
353359
/**
354360
* Drop a single index in the collection.
355361
*
362+
* @see DropIndexes::__construct() for supported options
356363
* @param string $indexName Index name
357-
* @return object Command result document
364+
* @param array $options Additional options
365+
* @return array|object Command result document
358366
* @throws InvalidArgumentException if $indexName is an empty string or "*"
359367
*/
360-
public function dropIndex($indexName)
368+
public function dropIndex($indexName, array $options = [])
361369
{
362370
$indexName = (string) $indexName;
363371

364372
if ($indexName === '*') {
365373
throw new InvalidArgumentException('dropIndexes() must be used to drop multiple indexes');
366374
}
367375

368-
$operation = new DropIndexes($this->databaseName, $this->collectionName, $indexName);
376+
if ( ! isset($options['typeMap'])) {
377+
$options['typeMap'] = $this->typeMap;
378+
}
379+
380+
$operation = new DropIndexes($this->databaseName, $this->collectionName, $indexName, $options);
369381
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
370382

371383
return $operation->execute($server);
@@ -374,11 +386,17 @@ public function dropIndex($indexName)
374386
/**
375387
* Drop all indexes in the collection.
376388
*
377-
* @return object Command result document
389+
* @see DropIndexes::__construct() for supported options
390+
* @param array $options Additional options
391+
* @return array|object Command result document
378392
*/
379-
public function dropIndexes()
393+
public function dropIndexes(array $options = [])
380394
{
381-
$operation = new DropIndexes($this->databaseName, $this->collectionName, '*');
395+
if ( ! isset($options['typeMap'])) {
396+
$options['typeMap'] = $this->typeMap;
397+
}
398+
399+
$operation = new DropIndexes($this->databaseName, $this->collectionName, '*', $options);
382400
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
383401

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

src/Database.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,17 @@ public function createCollection($collectionName, array $options = [])
173173
/**
174174
* Drop this database.
175175
*
176-
* @return object Command result document
176+
* @see DropDatabase::__construct() for supported options
177+
* @param array $options Additional options
178+
* @return array|object Command result document
177179
*/
178-
public function drop()
180+
public function drop(array $options = [])
179181
{
180-
$operation = new DropDatabase($this->databaseName);
182+
if ( ! isset($options['typeMap'])) {
183+
$options['typeMap'] = $this->typeMap;
184+
}
185+
186+
$operation = new DropDatabase($this->databaseName, $options);
181187
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
182188

183189
return $operation->execute($server);
@@ -186,12 +192,18 @@ public function drop()
186192
/**
187193
* Drop a collection within this database.
188194
*
189-
* @param string $collectionName
190-
* @return object Command result document
195+
* @see DropCollection::__construct() for supported options
196+
* @param string $collectionName Collection name
197+
* @param array $options Additional options
198+
* @return array|object Command result document
191199
*/
192-
public function dropCollection($collectionName)
200+
public function dropCollection($collectionName, array $options = [])
193201
{
194-
$operation = new DropCollection($this->databaseName, $collectionName);
202+
if ( ! isset($options['typeMap'])) {
203+
$options['typeMap'] = $this->typeMap;
204+
}
205+
206+
$operation = new DropCollection($this->databaseName, $collectionName, $options);
195207
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
196208

197209
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
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\DropCollection;
6+
7+
class DropCollectionTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentException
11+
* @dataProvider provideInvalidConstructorOptions
12+
*/
13+
public function testConstructorOptionTypeChecks(array $options)
14+
{
15+
new DropCollection($this->getDatabaseName(), $this->getCollectionName(), $options);
16+
}
17+
18+
public function provideInvalidConstructorOptions()
19+
{
20+
$options = [];
21+
22+
foreach ($this->getInvalidArrayValues() as $value) {
23+
$options[][] = ['typeMap' => $value];
24+
}
25+
26+
return $options;
27+
}
28+
}

tests/Operation/DropDatabaseTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Operation\DropDatabase;
6+
7+
class DropDatabaseTest extends TestCase
8+
{
9+
/**
10+
* @expectedException MongoDB\Exception\InvalidArgumentException
11+
* @dataProvider provideInvalidConstructorOptions
12+
*/
13+
public function testConstructorOptionTypeChecks(array $options)
14+
{
15+
new DropDatabase($this->getDatabaseName(), $options);
16+
}
17+
18+
public function provideInvalidConstructorOptions()
19+
{
20+
$options = [];
21+
22+
foreach ($this->getInvalidArrayValues() as $value) {
23+
$options[][] = ['typeMap' => $value];
24+
}
25+
26+
return $options;
27+
}
28+
}

tests/Operation/DropIndexesTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,24 @@ public function testDropIndexShouldNotAllowEmptyIndexName()
1313
{
1414
new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), '');
1515
}
16+
17+
/**
18+
* @expectedException MongoDB\Exception\InvalidArgumentException
19+
* @dataProvider provideInvalidConstructorOptions
20+
*/
21+
public function testConstructorOptionTypeChecks(array $options)
22+
{
23+
new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), '*', $options);
24+
}
25+
26+
public function provideInvalidConstructorOptions()
27+
{
28+
$options = [];
29+
30+
foreach ($this->getInvalidArrayValues() as $value) {
31+
$options[][] = ['typeMap' => $value];
32+
}
33+
34+
return $options;
35+
}
1636
}

0 commit comments

Comments
 (0)