Skip to content

Commit 733d834

Browse files
committed
Merge pull request #91
2 parents 43aa6b5 + 7c17b34 commit 733d834

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+360
-241
lines changed

src/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Driver\Manager;
88
use MongoDB\Driver\ReadPreference;
99
use MongoDB\Driver\WriteConcern;
10+
use MongoDB\Exception\InvalidArgumentException;
1011
use MongoDB\Model\DatabaseInfoIterator;
1112
use MongoDB\Operation\DropDatabase;
1213
use MongoDB\Operation\ListDatabases;
@@ -49,7 +50,7 @@ public function __construct($uri = 'mongodb://localhost:27017', array $uriOption
4950
];
5051

5152
if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
52-
throw new InvalidArgumentTypeException('"typeMap" driver option', $driverOptions['typeMap'], 'array');
53+
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
5354
}
5455

5556
$this->manager = new Manager($uri, $uriOptions, $driverOptions);

src/Collection.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use MongoDB\Driver\Server;
1111
use MongoDB\Driver\WriteConcern;
1212
use MongoDB\Exception\InvalidArgumentException;
13-
use MongoDB\Exception\InvalidArgumentTypeException;
14-
use MongoDB\Exception\UnexpectedTypeException;
1513
use MongoDB\Model\IndexInfoIterator;
1614
use MongoDB\Model\IndexInput;
1715
use MongoDB\Operation\Aggregate;
@@ -86,19 +84,19 @@ public function __construct(Manager $manager, $namespace, array $options = [])
8684
$this->collectionName = $parts[1];
8785

8886
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
89-
throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
87+
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
9088
}
9189

9290
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
93-
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
91+
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
9492
}
9593

9694
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
97-
throw new InvalidArgumentTypeException('"typeMap" option', $options['typeMap'], 'array');
95+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
9896
}
9997

10098
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
101-
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
99+
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
102100
}
103101

104102
$this->manager = $manager;

src/Database.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use MongoDB\Driver\Server;
1212
use MongoDB\Driver\WriteConcern;
1313
use MongoDB\Exception\InvalidArgumentException;
14-
use MongoDB\Exception\InvalidArgumentTypeException;
1514
use MongoDB\Model\CollectionInfoIterator;
1615
use MongoDB\Operation\CreateCollection;
1716
use MongoDB\Operation\DatabaseCommand;
@@ -62,19 +61,19 @@ public function __construct(Manager $manager, $databaseName, array $options = []
6261
}
6362

6463
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
65-
throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
64+
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
6665
}
6766

6867
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
69-
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
68+
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
7069
}
7170

7271
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
73-
throw new InvalidArgumentTypeException('"typeMap" option', $options['typeMap'], 'array');
72+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
7473
}
7574

7675
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
77-
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
76+
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
7877
}
7978

8079
$this->manager = $manager;

src/Exception/BadMethodCallException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,22 @@
44

55
class BadMethodCallException extends \BadMethodCallException implements Exception
66
{
7+
/**
8+
* Thrown when a mutable method is invoked on an immutable object.
9+
*
10+
* @param string $class Class name
11+
* @return self
12+
*/
13+
public static function classIsImmutable($class)
14+
{
15+
return new static(sprintf('%s is immutable', $class));
16+
}
17+
718
/**
819
* Thrown when accessing a result field on an unacknowledged write result.
20+
*
21+
* @param string $method Method name
22+
* @return self
923
*/
1024
public static function unacknowledgedWriteResultAccess($method)
1125
{

src/Exception/InvalidArgumentException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,16 @@
44

55
class InvalidArgumentException extends \MongoDB\Driver\Exception\InvalidArgumentException implements Exception
66
{
7+
/**
8+
* Thrown when an argument or option has an invalid type.
9+
*
10+
* @param string $name Name of the argument or option
11+
* @param mixed $value Actual value (used to derive the type)
12+
* @param string $expectedType Expected type
13+
* @return self
14+
*/
15+
public static function invalidType($name, $value, $expectedType)
16+
{
17+
return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, is_object($value) ? get_class($value) : gettype($value)));
18+
}
719
}

src/Exception/InvalidArgumentTypeException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Exception/UnexpectedTypeException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Exception/UnexpectedValueTypeException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Model/IndexInfo.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,21 @@ public function offsetGet($key)
151151
* Not supported.
152152
*
153153
* @see http://php.net/arrayaccess.offsetset
154-
* @throws BadMethodCallException IndexInfo is immutable
154+
* @throws BadMethodCallException
155155
*/
156156
public function offsetSet($key, $value)
157157
{
158-
throw new BadMethodCallException('IndexInfo is immutable');
158+
throw BadMethodCallException::classIsImmutable(__CLASS__);
159159
}
160160

161161
/**
162162
* Not supported.
163163
*
164164
* @see http://php.net/arrayaccess.offsetunset
165-
* @throws BadMethodCallException IndexInfo is immutable
165+
* @throws BadMethodCallException
166166
*/
167167
public function offsetUnset($key)
168168
{
169-
throw new BadMethodCallException('IndexInfo is immutable');
169+
throw BadMethodCallException::classIsImmutable(__CLASS__);
170170
}
171171
}

src/Model/IndexInput.php

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

55
use MongoDB\BSON\Serializable;
66
use MongoDB\Exception\InvalidArgumentException;
7-
use MongoDB\Exception\InvalidArgumentTypeException;
87

98
/**
109
* Index input model class.
@@ -24,6 +23,7 @@ class IndexInput implements Serializable
2423
* Constructor.
2524
*
2625
* @param array $index Index specification
26+
* @throws InvalidArgumentException
2727
*/
2828
public function __construct(array $index)
2929
{
@@ -32,12 +32,12 @@ public function __construct(array $index)
3232
}
3333

3434
if ( ! is_array($index['key']) && ! is_object($index['key'])) {
35-
throw new InvalidArgumentTypeException('"key" option', $index['key'], 'array or object');
35+
throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'array or object');
3636
}
3737

3838
foreach ($index['key'] as $fieldName => $order) {
3939
if ( ! is_int($order) && ! is_float($order) && ! is_string($order)) {
40-
throw new InvalidArgumentTypeException(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
40+
throw InvalidArgumentException::invalidType(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
4141
}
4242
}
4343

@@ -46,15 +46,15 @@ public function __construct(array $index)
4646
}
4747

4848
if ( ! is_string($index['ns'])) {
49-
throw new InvalidArgumentTypeException('"ns" option', $index['ns'], 'string');
49+
throw InvalidArgumentException::invalidType('"ns" option', $index['ns'], 'string');
5050
}
5151

5252
if ( ! isset($index['name'])) {
5353
$index['name'] = \MongoDB\generate_index_name($index['key']);
5454
}
5555

5656
if ( ! is_string($index['name'])) {
57-
throw new InvalidArgumentTypeException('"name" option', $index['name'], 'string');
57+
throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string');
5858
}
5959

6060
$this->index = $index;

0 commit comments

Comments
 (0)