Skip to content

Commit 2c24646

Browse files
committed
Use DriverOptions value object in Client
1 parent 220f52a commit 2c24646

File tree

1 file changed

+23
-108
lines changed

1 file changed

+23
-108
lines changed

src/Client.php

Lines changed: 23 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919

2020
use Composer\InstalledVersions;
2121
use Iterator;
22-
use MongoDB\BSON\Document;
23-
use MongoDB\BSON\PackedArray;
24-
use MongoDB\Builder\BuilderEncoder;
2522
use MongoDB\Builder\Pipeline;
26-
use MongoDB\Codec\Encoder;
2723
use MongoDB\Driver\BulkWriteCommand;
2824
use MongoDB\Driver\BulkWriteCommandResult;
2925
use MongoDB\Driver\ClientEncryption;
@@ -38,34 +34,23 @@
3834
use MongoDB\Exception\InvalidArgumentException;
3935
use MongoDB\Exception\UnexpectedValueException;
4036
use MongoDB\Exception\UnsupportedException;
41-
use MongoDB\Model\BSONArray;
42-
use MongoDB\Model\BSONDocument;
4337
use MongoDB\Model\DatabaseInfo;
4438
use MongoDB\Operation\ClientBulkWriteCommand;
4539
use MongoDB\Operation\DropDatabase;
4640
use MongoDB\Operation\ListDatabaseNames;
4741
use MongoDB\Operation\ListDatabases;
4842
use MongoDB\Operation\Watch;
49-
use stdClass;
43+
use MongoDB\ValueObject\AutoEncryptionOptions;
44+
use MongoDB\ValueObject\DriverOptions;
5045
use Stringable;
5146
use Throwable;
5247

5348
use function array_diff_key;
54-
use function is_array;
55-
use function is_string;
5649

5750
class Client implements Stringable
5851
{
5952
public const DEFAULT_URI = 'mongodb://127.0.0.1/';
6053

61-
private const DEFAULT_TYPE_MAP = [
62-
'array' => BSONArray::class,
63-
'document' => BSONDocument::class,
64-
'root' => BSONDocument::class,
65-
];
66-
67-
private const HANDSHAKE_SEPARATOR = '/';
68-
6954
private static ?string $version = null;
7055

7156
private Manager $manager;
@@ -76,14 +61,9 @@ class Client implements Stringable
7661

7762
private string $uri;
7863

79-
private array $typeMap;
80-
81-
/** @psalm-var Encoder<array|stdClass|Document|PackedArray, mixed> */
82-
private readonly Encoder $builderEncoder;
83-
8464
private WriteConcern $writeConcern;
8565

86-
private bool $autoEncryptionEnabled;
66+
private DriverOptions $driverOptions;
8767

8868
/**
8969
* Constructs a new Client instance.
@@ -113,32 +93,11 @@ class Client implements Stringable
11393
*/
11494
public function __construct(?string $uri = null, array $uriOptions = [], array $driverOptions = [])
11595
{
116-
$driverOptions += ['typeMap' => self::DEFAULT_TYPE_MAP];
117-
118-
if (! is_array($driverOptions['typeMap'])) {
119-
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
120-
}
121-
122-
if (isset($driverOptions['autoEncryption']) && is_array($driverOptions['autoEncryption'])) {
123-
$driverOptions['autoEncryption'] = $this->prepareEncryptionOptions($driverOptions['autoEncryption']);
124-
}
125-
126-
if (isset($driverOptions['builderEncoder']) && ! $driverOptions['builderEncoder'] instanceof Encoder) {
127-
throw InvalidArgumentException::invalidType('"builderEncoder" option', $driverOptions['builderEncoder'], Encoder::class);
128-
}
129-
130-
$driverOptions['driver'] = $this->mergeDriverInfo($driverOptions['driver'] ?? []);
96+
$this->driverOptions = DriverOptions::fromArray($driverOptions);
13197

13298
$this->uri = $uri ?? self::DEFAULT_URI;
133-
$this->builderEncoder = $driverOptions['builderEncoder'] ?? new BuilderEncoder();
134-
$this->typeMap = $driverOptions['typeMap'];
135-
136-
/* Database and Collection objects may need to know whether auto
137-
* encryption is enabled for dropping collections. Track this via an
138-
* internal option until PHPC-2615 is implemented. */
139-
$this->autoEncryptionEnabled = isset($driverOptions['autoEncryption']['keyVaultNamespace']);
14099

141-
$driverOptions = array_diff_key($driverOptions, ['builderEncoder' => 1, 'typeMap' => 1]);
100+
$driverOptions = array_diff_key($this->driverOptions->toArray(), ['builderEncoder' => 1, 'typeMap' => 1]);
142101

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

@@ -157,8 +116,8 @@ public function __debugInfo(): array
157116
return [
158117
'manager' => $this->manager,
159118
'uri' => $this->uri,
160-
'typeMap' => $this->typeMap,
161-
'builderEncoder' => $this->builderEncoder,
119+
'typeMap' => $this->driverOptions->typeMap,
120+
'builderEncoder' => $this->driverOptions->builderEncoder,
162121
'writeConcern' => $this->writeConcern,
163122
];
164123
}
@@ -230,9 +189,9 @@ public function bulkWrite(BulkWriteCommand|ClientBulkWrite $bulk, array $options
230189
*/
231190
public function createClientEncryption(array $options): ClientEncryption
232191
{
233-
$options = $this->prepareEncryptionOptions($options);
192+
$options = AutoEncryptionOptions::fromArray($options);
234193

235-
return $this->manager->createClientEncryption($options);
194+
return $this->manager->createClientEncryption($options->toArray());
236195
}
237196

238197
/**
@@ -269,7 +228,11 @@ public function dropDatabase(string $databaseName, array $options = []): void
269228
*/
270229
public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection
271230
{
272-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder, 'autoEncryptionEnabled' => $this->autoEncryptionEnabled];
231+
$options += [
232+
'typeMap' => $this->driverOptions->typeMap,
233+
'builderEncoder' => $this->driverOptions->builderEncoder,
234+
'autoEncryptionEnabled' => $this->driverOptions->isAutoEncryptionEnabled(),
235+
];
273236

274237
return new Collection($this->manager, $databaseName, $collectionName, $options);
275238
}
@@ -284,7 +247,11 @@ public function getCollection(string $databaseName, string $collectionName, arra
284247
*/
285248
public function getDatabase(string $databaseName, array $options = []): Database
286249
{
287-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder, 'autoEncryptionEnabled' => $this->autoEncryptionEnabled];
250+
$options += [
251+
'typeMap' => $this->driverOptions->typeMap,
252+
'builderEncoder' => $this->driverOptions->builderEncoder,
253+
'autoEncryptionEnabled' => $this->driverOptions->isAutoEncryptionEnabled(),
254+
];
288255

289256
return new Database($this->manager, $databaseName, $options);
290257
}
@@ -320,7 +287,7 @@ public function getReadPreference(): ReadPreference
320287
*/
321288
public function getTypeMap(): array
322289
{
323-
return $this->typeMap;
290+
return $this->driverOptions->typeMap;
324291
}
325292

326293
/**
@@ -429,7 +396,7 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
429396
$pipeline = new Pipeline(...$pipeline);
430397
}
431398

432-
$pipeline = $this->builderEncoder->encodeIfSupported($pipeline);
399+
$pipeline = $this->driverOptions->builderEncoder->encodeIfSupported($pipeline);
433400

434401
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
435402
$options['readPreference'] = $this->readPreference;
@@ -442,15 +409,15 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
442409
}
443410

444411
if (! isset($options['typeMap'])) {
445-
$options['typeMap'] = $this->typeMap;
412+
$options['typeMap'] = $this->driverOptions->typeMap;
446413
}
447414

448415
$operation = new Watch($this->manager, null, null, $pipeline, $options);
449416

450417
return $operation->execute($server);
451418
}
452419

453-
private static function getVersion(): string
420+
public static function getVersion(): string
454421
{
455422
if (self::$version === null) {
456423
try {
@@ -462,56 +429,4 @@ private static function getVersion(): string
462429

463430
return self::$version;
464431
}
465-
466-
private function mergeDriverInfo(array $driver): array
467-
{
468-
$mergedDriver = [
469-
'name' => 'PHPLIB',
470-
'version' => self::getVersion(),
471-
];
472-
473-
if (isset($driver['name'])) {
474-
if (! is_string($driver['name'])) {
475-
throw InvalidArgumentException::invalidType('"name" handshake option', $driver['name'], 'string');
476-
}
477-
478-
$mergedDriver['name'] .= self::HANDSHAKE_SEPARATOR . $driver['name'];
479-
}
480-
481-
if (isset($driver['version'])) {
482-
if (! is_string($driver['version'])) {
483-
throw InvalidArgumentException::invalidType('"version" handshake option', $driver['version'], 'string');
484-
}
485-
486-
$mergedDriver['version'] .= self::HANDSHAKE_SEPARATOR . $driver['version'];
487-
}
488-
489-
if (isset($driver['platform'])) {
490-
$mergedDriver['platform'] = $driver['platform'];
491-
}
492-
493-
return $mergedDriver;
494-
}
495-
496-
private function prepareEncryptionOptions(array $options): array
497-
{
498-
if (isset($options['keyVaultClient'])) {
499-
if ($options['keyVaultClient'] instanceof self) {
500-
$options['keyVaultClient'] = $options['keyVaultClient']->manager;
501-
} elseif (! $options['keyVaultClient'] instanceof Manager) {
502-
throw InvalidArgumentException::invalidType('"keyVaultClient" option', $options['keyVaultClient'], [self::class, Manager::class]);
503-
}
504-
}
505-
506-
// The server requires an empty document for automatic credentials.
507-
if (isset($options['kmsProviders']) && is_array($options['kmsProviders'])) {
508-
foreach ($options['kmsProviders'] as $name => $provider) {
509-
if ($provider === []) {
510-
$options['kmsProviders'][$name] = new stdClass();
511-
}
512-
}
513-
}
514-
515-
return $options;
516-
}
517432
}

0 commit comments

Comments
 (0)