Skip to content

Commit 28e0574

Browse files
committed
Merge pull request #73
2 parents 2c6a776 + 2f3a455 commit 28e0574

18 files changed

+513
-167
lines changed

src/Client.php

+25-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Client
1515
{
1616
private $manager;
1717
private $uri;
18+
private $typeMap;
1819

1920
/**
2021
* Constructs a new Client instance.
@@ -23,15 +24,29 @@ class Client
2324
* cluster of servers. It serves as a gateway for accessing individual
2425
* databases and collections.
2526
*
27+
* Supported driver-specific options:
28+
*
29+
* * typeMap (array): Default type map for cursors and BSON documents.
30+
*
31+
* Other options are documented in MongoDB\Driver\Manager::__construct().
32+
*
2633
* @see http://docs.mongodb.org/manual/reference/connection-string/
34+
* @see http://php.net/manual/en/mongodb-driver-manager.construct.php
35+
* @see http://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps
2736
* @param string $uri MongoDB connection string
28-
* @param array $options Additional connection string options
37+
* @param array $uriOptions Additional connection string options
2938
* @param array $driverOptions Driver-specific options
39+
* @throws InvalidArgumentException
3040
*/
31-
public function __construct($uri = 'mongodb://localhost:27017', array $options = [], array $driverOptions = [])
41+
public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
3242
{
33-
$this->manager = new Manager($uri, $options, $driverOptions);
43+
if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
44+
throw new InvalidArgumentTypeException('"typeMap" driver option', $driverOptions['typeMap'], 'array');
45+
}
46+
47+
$this->manager = new Manager($uri, $uriOptions, $driverOptions);
3448
$this->uri = (string) $uri;
49+
$this->typeMap = isset($driverOptions['typeMap']) ? $driverOptions['typeMap'] : null;
3550
}
3651

3752
/**
@@ -45,6 +60,7 @@ public function __debugInfo()
4560
return [
4661
'manager' => $this->manager,
4762
'uri' => $this->uri,
63+
'typeMap' => $this->typeMap,
4864
];
4965
}
5066

@@ -89,45 +105,31 @@ public function listDatabases(array $options = [])
89105
/**
90106
* Select a collection.
91107
*
92-
* Supported options:
93-
*
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-
*
108+
* @see Collection::__construct() for supported options
102109
* @param string $databaseName Name of the database containing the collection
103110
* @param string $collectionName Name of the collection to select
104111
* @param array $options Collection constructor options
105112
* @return Collection
106113
*/
107114
public function selectCollection($databaseName, $collectionName, array $options = [])
108115
{
116+
$options += ['typeMap' => $this->typeMap];
117+
109118
return new Collection($this->manager, $databaseName . '.' . $collectionName, $options);
110119
}
111120

112121
/**
113122
* Select a database.
114123
*
115-
* Supported options:
116-
*
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-
*
124+
* @see Database::__construct() for supported options
125125
* @param string $databaseName Name of the database to select
126126
* @param array $options Database constructor options
127127
* @return Database
128128
*/
129129
public function selectDatabase($databaseName, array $options = [])
130130
{
131+
$options += ['typeMap' => $this->typeMap];
132+
131133
return new Database($this->manager, $databaseName, $options);
132134
}
133135
}

src/Collection.php

+47-31
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Collection
4545
private $manager;
4646
private $readConcern;
4747
private $readPreference;
48+
private $typeMap;
4849
private $writeConcern;
4950

5051
/**
@@ -62,6 +63,8 @@ class Collection
6263
* preference to use for collection operations. Defaults to the Manager's
6364
* read preference.
6465
*
66+
* * typeMap (array): Default type map for cursors and BSON documents.
67+
*
6568
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
6669
* to use for collection operations. Defaults to the Manager's write
6770
* concern.
@@ -90,13 +93,18 @@ public function __construct(Manager $manager, $namespace, array $options = [])
9093
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
9194
}
9295

96+
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
97+
throw new InvalidArgumentTypeException('"typeMap" option', $options['typeMap'], 'array');
98+
}
99+
93100
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
94101
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
95102
}
96103

97104
$this->manager = $manager;
98105
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
99106
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
107+
$this->typeMap = isset($options['typeMap']) ? $options['typeMap'] : null;
100108
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
101109
}
102110

@@ -114,6 +122,7 @@ public function __debugInfo()
114122
'manager' => $this->manager,
115123
'readConcern' => $this->readConcern,
116124
'readPreference' => $this->readPreference,
125+
'typeMap' => $this->typeMap,
117126
'writeConcern' => $this->writeConcern,
118127
];
119128
}
@@ -136,6 +145,10 @@ public function __toString()
136145
* returned; otherwise, an ArrayIterator is returned, which wraps the
137146
* "result" array from the command response document.
138147
*
148+
* Note: BSON deserialization of inline aggregation results (i.e. not using
149+
* a command cursor) does not yet support a custom type map
150+
* (depends on: https://jira.mongodb.org/browse/PHPC-314).
151+
*
139152
* @see Aggregate::__construct() for supported options
140153
* @param array $pipeline List of pipeline operations
141154
* @param array $options Command options
@@ -160,6 +173,10 @@ public function aggregate(array $pipeline, array $options = [])
160173
$options['readPreference'] = new ReadPreference(ReadPreference::RP_PRIMARY);
161174
}
162175

176+
if ( ! isset($options['typeMap'])) {
177+
$options['typeMap'] = $this->typeMap;
178+
}
179+
163180
$operation = new Aggregate($this->databaseName, $this->collectionName, $pipeline, $options);
164181
$server = $this->manager->selectServer($options['readPreference']);
165182

@@ -388,6 +405,10 @@ public function find($filter = [], array $options = [])
388405
$options['readPreference'] = $this->readPreference;
389406
}
390407

408+
if ( ! isset($options['typeMap'])) {
409+
$options['typeMap'] = $this->typeMap;
410+
}
411+
391412
$operation = new Find($this->databaseName, $this->collectionName, $filter, $options);
392413
$server = $this->manager->selectServer($options['readPreference']);
393414

@@ -413,6 +434,10 @@ public function findOne($filter = [], array $options = [])
413434
$options['readPreference'] = $this->readPreference;
414435
}
415436

437+
if ( ! isset($options['typeMap'])) {
438+
$options['typeMap'] = $this->typeMap;
439+
}
440+
416441
$operation = new FindOne($this->databaseName, $this->collectionName, $filter, $options);
417442
$server = $this->manager->selectServer($options['readPreference']);
418443

@@ -424,6 +449,9 @@ public function findOne($filter = [], array $options = [])
424449
*
425450
* The document to return may be null.
426451
*
452+
* Note: BSON deserialization of the returned document does not yet support
453+
* a custom type map (depends on: https://jira.mongodb.org/browse/PHPC-314).
454+
*
427455
* @see FindOneAndDelete::__construct() for supported options
428456
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
429457
* @param array|object $filter Query by which to filter documents
@@ -451,6 +479,9 @@ public function findOneAndDelete($filter, array $options = [])
451479
* returned. Specify FindOneAndReplace::RETURN_DOCUMENT_AFTER for the
452480
* "returnDocument" option to return the updated document.
453481
*
482+
* Note: BSON deserialization of the returned document does not yet support
483+
* a custom type map (depends on: https://jira.mongodb.org/browse/PHPC-314).
484+
*
454485
* @see FindOneAndReplace::__construct() for supported options
455486
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
456487
* @param array|object $filter Query by which to filter documents
@@ -479,6 +510,9 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
479510
* returned. Specify FindOneAndUpdate::RETURN_DOCUMENT_AFTER for the
480511
* "returnDocument" option to return the updated document.
481512
*
513+
* Note: BSON deserialization of the returned document does not yet support
514+
* a custom type map (depends on: https://jira.mongodb.org/browse/PHPC-314).
515+
*
482516
* @see FindOneAndReplace::__construct() for supported options
483517
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
484518
* @param array|object $filter Query by which to filter documents
@@ -613,9 +647,9 @@ public function replaceOne($filter, $replacement, array $options = [])
613647
*
614648
* @see UpdateMany::__construct() for supported options
615649
* @see http://docs.mongodb.org/manual/reference/command/update/
616-
* @param array|object $filter Query by which to filter documents
617-
* @param array|object $replacement Update to apply to the matched documents
618-
* @param array $options Command options
650+
* @param array|object $filter Query by which to filter documents
651+
* @param array|object $update Update to apply to the matched documents
652+
* @param array $options Command options
619653
* @return UpdateResult
620654
*/
621655
public function updateMany($filter, $update, array $options = [])
@@ -635,9 +669,9 @@ public function updateMany($filter, $update, array $options = [])
635669
*
636670
* @see ReplaceOne::__construct() for supported options
637671
* @see http://docs.mongodb.org/manual/reference/command/update/
638-
* @param array|object $filter Query by which to filter documents
639-
* @param array|object $replacement Update to apply to the matched document
640-
* @param array $options Command options
672+
* @param array|object $filter Query by which to filter documents
673+
* @param array|object $update Update to apply to the matched document
674+
* @param array $options Command options
641675
* @return UpdateResult
642676
*/
643677
public function updateOne($filter, $update, array $options = [])
@@ -655,36 +689,18 @@ public function updateOne($filter, $update, array $options = [])
655689
/**
656690
* Get a clone of this collection with different options.
657691
*
658-
* Supported options:
659-
*
660-
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
661-
* use for collection operations. Defaults to this Collection's read
662-
* concern.
663-
*
664-
* * readPreference (MongoDB\Driver\ReadPreference): The default read
665-
* preference to use for collection operations. Defaults to this
666-
* Collection's read preference.
667-
*
668-
* * writeConcern (MongoDB\Driver\WriteConcern): The default write concern
669-
* to use for collection operations. Defaults to this Collection's write
670-
* concern.
671-
*
692+
* @see Collection::__construct() for supported options
672693
* @param array $options Collection constructor options
673694
* @return Collection
674695
*/
675696
public function withOptions(array $options = [])
676697
{
677-
if ( ! isset($options['readConcern'])) {
678-
$options['readConcern'] = $this->readConcern;
679-
}
680-
681-
if ( ! isset($options['readPreference'])) {
682-
$options['readPreference'] = $this->readPreference;
683-
}
684-
685-
if ( ! isset($options['writeConcern'])) {
686-
$options['writeConcern'] = $this->writeConcern;
687-
}
698+
$options += [
699+
'readConcern' => $this->readConcern,
700+
'readPreference' => $this->readPreference,
701+
'typeMap' => $this->typeMap,
702+
'writeConcern' => $this->writeConcern,
703+
];
688704

689705
return new Collection($this->manager, $this->databaseName . '.' . $this->collectionName, $options);
690706
}

0 commit comments

Comments
 (0)