Skip to content

Commit 03fa128

Browse files
committed
Use DriverOptions value object in Client
1 parent 6e5e0d7 commit 03fa128

File tree

1 file changed

+22
-124
lines changed

1 file changed

+22
-124
lines changed

src/Client.php

Lines changed: 22 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@
1717

1818
namespace MongoDB;
1919

20-
use Composer\InstalledVersions;
2120
use Iterator;
22-
use MongoDB\BSON\Document;
23-
use MongoDB\BSON\PackedArray;
24-
use MongoDB\Builder\BuilderEncoder;
2521
use MongoDB\Builder\Pipeline;
26-
use MongoDB\Codec\Encoder;
2722
use MongoDB\Driver\BulkWriteCommand;
2823
use MongoDB\Driver\BulkWriteCommandResult;
2924
use MongoDB\Driver\ClientEncryption;
@@ -38,36 +33,22 @@
3833
use MongoDB\Exception\InvalidArgumentException;
3934
use MongoDB\Exception\UnexpectedValueException;
4035
use MongoDB\Exception\UnsupportedException;
41-
use MongoDB\Model\BSONArray;
42-
use MongoDB\Model\BSONDocument;
4336
use MongoDB\Model\DatabaseInfo;
4437
use MongoDB\Operation\ClientBulkWriteCommand;
4538
use MongoDB\Operation\DropDatabase;
4639
use MongoDB\Operation\ListDatabaseNames;
4740
use MongoDB\Operation\ListDatabases;
4841
use MongoDB\Operation\Watch;
49-
use stdClass;
42+
use MongoDB\ValueObject\AutoEncryptionOptions;
43+
use MongoDB\ValueObject\DriverOptions;
5044
use Stringable;
51-
use Throwable;
5245

5346
use function array_diff_key;
54-
use function is_array;
55-
use function is_string;
5647

5748
class Client implements Stringable
5849
{
5950
public const DEFAULT_URI = 'mongodb://127.0.0.1/';
6051

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-
69-
private static ?string $version = null;
70-
7152
private Manager $manager;
7253

7354
private ReadConcern $readConcern;
@@ -76,14 +57,9 @@ class Client implements Stringable
7657

7758
private string $uri;
7859

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

86-
private bool $autoEncryptionEnabled;
62+
private DriverOptions $driverOptions;
8763

8864
/**
8965
* Constructs a new Client instance.
@@ -113,32 +89,11 @@ class Client implements Stringable
11389
*/
11490
public function __construct(?string $uri = null, array $uriOptions = [], array $driverOptions = [])
11591
{
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'] ?? []);
92+
$this->driverOptions = DriverOptions::fromArray($driverOptions);
13193

13294
$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']);
14095

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

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

@@ -157,8 +112,8 @@ public function __debugInfo(): array
157112
return [
158113
'manager' => $this->manager,
159114
'uri' => $this->uri,
160-
'typeMap' => $this->typeMap,
161-
'builderEncoder' => $this->builderEncoder,
115+
'typeMap' => $this->driverOptions->typeMap,
116+
'builderEncoder' => $this->driverOptions->builderEncoder,
162117
'writeConcern' => $this->writeConcern,
163118
];
164119
}
@@ -230,9 +185,9 @@ public function bulkWrite(BulkWriteCommand|ClientBulkWrite $bulk, array $options
230185
*/
231186
public function createClientEncryption(array $options): ClientEncryption
232187
{
233-
$options = $this->prepareEncryptionOptions($options);
188+
$options = AutoEncryptionOptions::fromArray($options);
234189

235-
return $this->manager->createClientEncryption($options);
190+
return $this->manager->createClientEncryption($options->toArray());
236191
}
237192

238193
/**
@@ -269,7 +224,11 @@ public function dropDatabase(string $databaseName, array $options = []): void
269224
*/
270225
public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection
271226
{
272-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder, 'autoEncryptionEnabled' => $this->autoEncryptionEnabled];
227+
$options += [
228+
'typeMap' => $this->driverOptions->typeMap,
229+
'builderEncoder' => $this->driverOptions->builderEncoder,
230+
'autoEncryptionEnabled' => $this->driverOptions->isAutoEncryptionEnabled(),
231+
];
273232

274233
return new Collection($this->manager, $databaseName, $collectionName, $options);
275234
}
@@ -284,7 +243,11 @@ public function getCollection(string $databaseName, string $collectionName, arra
284243
*/
285244
public function getDatabase(string $databaseName, array $options = []): Database
286245
{
287-
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder, 'autoEncryptionEnabled' => $this->autoEncryptionEnabled];
246+
$options += [
247+
'typeMap' => $this->driverOptions->typeMap,
248+
'builderEncoder' => $this->driverOptions->builderEncoder,
249+
'autoEncryptionEnabled' => $this->driverOptions->isAutoEncryptionEnabled(),
250+
];
288251

289252
return new Database($this->manager, $databaseName, $options);
290253
}
@@ -320,7 +283,7 @@ public function getReadPreference(): ReadPreference
320283
*/
321284
public function getTypeMap(): array
322285
{
323-
return $this->typeMap;
286+
return $this->driverOptions->typeMap;
324287
}
325288

326289
/**
@@ -429,7 +392,7 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
429392
$pipeline = new Pipeline(...$pipeline);
430393
}
431394

432-
$pipeline = $this->builderEncoder->encodeIfSupported($pipeline);
395+
$pipeline = $this->driverOptions->builderEncoder->encodeIfSupported($pipeline);
433396

434397
if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
435398
$options['readPreference'] = $this->readPreference;
@@ -442,76 +405,11 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
442405
}
443406

444407
if (! isset($options['typeMap'])) {
445-
$options['typeMap'] = $this->typeMap;
408+
$options['typeMap'] = $this->driverOptions->typeMap;
446409
}
447410

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

450413
return $operation->execute($server);
451414
}
452-
453-
private static function getVersion(): string
454-
{
455-
if (self::$version === null) {
456-
try {
457-
self::$version = InstalledVersions::getPrettyVersion('mongodb/mongodb') ?? 'unknown';
458-
} catch (Throwable) {
459-
self::$version = 'error';
460-
}
461-
}
462-
463-
return self::$version;
464-
}
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-
}
517415
}

0 commit comments

Comments
 (0)