Skip to content

Commit c6cecce

Browse files
committed
Use DriverOptions value object in Client
1 parent 64136c0 commit c6cecce

File tree

1 file changed

+23
-105
lines changed

1 file changed

+23
-105
lines changed

src/Client.php

Lines changed: 23 additions & 105 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;
@@ -46,26 +42,18 @@
4642
use MongoDB\Operation\ListDatabaseNames;
4743
use MongoDB\Operation\ListDatabases;
4844
use MongoDB\Operation\Watch;
49-
use stdClass;
45+
use MongoDB\ValueObject\AutoEncryptionOptions;
46+
use MongoDB\ValueObject\DriverOptions;
5047
use Stringable;
5148
use Throwable;
5249

5350
use function array_diff_key;
54-
use function is_array;
5551
use function is_string;
5652

5753
class Client implements Stringable
5854
{
5955
public const DEFAULT_URI = 'mongodb://127.0.0.1/';
6056

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-
6957
private static ?string $version = null;
7058

7159
private Manager $manager;
@@ -76,14 +64,9 @@ class Client implements Stringable
7664

7765
private string $uri;
7866

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

86-
private bool $autoEncryptionEnabled;
69+
private DriverOptions $driverOptions;
8770

8871
/**
8972
* Constructs a new Client instance.
@@ -113,32 +96,11 @@ class Client implements Stringable
11396
*/
11497
public function __construct(?string $uri = null, array $uriOptions = [], array $driverOptions = [])
11598
{
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'] ?? []);
99+
$this->driverOptions = DriverOptions::fromArray($driverOptions);
131100

132101
$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']);
140102

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

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

@@ -157,8 +119,8 @@ public function __debugInfo(): array
157119
return [
158120
'manager' => $this->manager,
159121
'uri' => $this->uri,
160-
'typeMap' => $this->typeMap,
161-
'builderEncoder' => $this->builderEncoder,
122+
'typeMap' => $this->driverOptions->typeMap,
123+
'builderEncoder' => $this->driverOptions->builderEncoder,
162124
'writeConcern' => $this->writeConcern,
163125
];
164126
}
@@ -230,9 +192,9 @@ public function bulkWrite(BulkWriteCommand|ClientBulkWrite $bulk, array $options
230192
*/
231193
public function createClientEncryption(array $options): ClientEncryption
232194
{
233-
$options = $this->prepareEncryptionOptions($options);
195+
$options = AutoEncryptionOptions::fromArray($options);
234196

235-
return $this->manager->createClientEncryption($options);
197+
return $this->manager->createClientEncryption($options->toArray());
236198
}
237199

238200
/**
@@ -269,7 +231,11 @@ public function dropDatabase(string $databaseName, array $options = []): void
269231
*/
270232
public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection
271233
{
272-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder, 'autoEncryptionEnabled' => $this->autoEncryptionEnabled];
234+
$options += [
235+
'typeMap' => $this->driverOptions->typeMap,
236+
'builderEncoder' => $this->driverOptions->builderEncoder,
237+
'autoEncryptionEnabled' => $this->driverOptions->isAutoEncryptionEnabled(),
238+
];
273239

274240
return new Collection($this->manager, $databaseName, $collectionName, $options);
275241
}
@@ -284,7 +250,11 @@ public function getCollection(string $databaseName, string $collectionName, arra
284250
*/
285251
public function getDatabase(string $databaseName, array $options = []): Database
286252
{
287-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder, 'autoEncryptionEnabled' => $this->autoEncryptionEnabled];
253+
$options += [
254+
'typeMap' => $this->driverOptions->typeMap,
255+
'builderEncoder' => $this->driverOptions->builderEncoder,
256+
'autoEncryptionEnabled' => $this->driverOptions->isAutoEncryptionEnabled(),
257+
];
288258

289259
return new Database($this->manager, $databaseName, $options);
290260
}
@@ -320,7 +290,7 @@ public function getReadPreference(): ReadPreference
320290
*/
321291
public function getTypeMap(): array
322292
{
323-
return $this->typeMap;
293+
return $this->driverOptions->typeMap;
324294
}
325295

326296
/**
@@ -429,7 +399,7 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
429399
$pipeline = new Pipeline(...$pipeline);
430400
}
431401

432-
$pipeline = $this->builderEncoder->encodeIfSupported($pipeline);
402+
$pipeline = $this->driverOptions->builderEncoder->encodeIfSupported($pipeline);
433403

434404
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
435405
$options['readPreference'] = $this->readPreference;
@@ -442,15 +412,15 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
442412
}
443413

444414
if (! isset($options['typeMap'])) {
445-
$options['typeMap'] = $this->typeMap;
415+
$options['typeMap'] = $this->driverOptions->typeMap;
446416
}
447417

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

450420
return $operation->execute($server);
451421
}
452422

453-
private static function getVersion(): string
423+
public static function getVersion(): string
454424
{
455425
if (self::$version === null) {
456426
try {
@@ -462,56 +432,4 @@ private static function getVersion(): string
462432

463433
return self::$version;
464434
}
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-
}
517435
}

0 commit comments

Comments
 (0)