1717
1818namespace MongoDB ;
1919
20- use Composer \InstalledVersions ;
2120use Iterator ;
22- use MongoDB \BSON \Document ;
23- use MongoDB \BSON \PackedArray ;
24- use MongoDB \Builder \BuilderEncoder ;
2521use MongoDB \Builder \Pipeline ;
26- use MongoDB \Codec \Encoder ;
2722use MongoDB \Driver \BulkWriteCommand ;
2823use MongoDB \Driver \BulkWriteCommandResult ;
2924use MongoDB \Driver \ClientEncryption ;
3833use MongoDB \Exception \InvalidArgumentException ;
3934use MongoDB \Exception \UnexpectedValueException ;
4035use MongoDB \Exception \UnsupportedException ;
41- use MongoDB \Model \BSONArray ;
42- use MongoDB \Model \BSONDocument ;
36+ use MongoDB \Model \AutoEncryptionOptions ;
4337use MongoDB \Model \DatabaseInfo ;
38+ use MongoDB \Model \DriverOptions ;
4439use MongoDB \Operation \ClientBulkWriteCommand ;
4540use MongoDB \Operation \DropDatabase ;
4641use MongoDB \Operation \ListDatabaseNames ;
4742use MongoDB \Operation \ListDatabases ;
4843use MongoDB \Operation \Watch ;
4944use stdClass ;
5045use Stringable ;
51- use Throwable ;
5246
5347use function array_diff_key ;
54- use function is_array ;
55- use function is_string ;
5648
49+ /** @psalm-import-type stage from Builder\Pipeline */
5750class 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-
69- private static ?string $ version = null ;
70-
7154 private Manager $ manager ;
7255
7356 private ReadConcern $ readConcern ;
@@ -76,14 +59,9 @@ class Client implements Stringable
7659
7760 private string $ uri ;
7861
79- private array $ typeMap ;
80-
81- /** @psalm-var Encoder<array|stdClass|Document|PackedArray, mixed> */
82- private readonly Encoder $ builderEncoder ;
83-
8462 private WriteConcern $ writeConcern ;
8563
86- private bool $ autoEncryptionEnabled ;
64+ private DriverOptions $ driverOptions ;
8765
8866 /**
8967 * Constructs a new Client instance.
@@ -113,32 +91,11 @@ class Client implements Stringable
11391 */
11492 public function __construct (?string $ uri = null , array $ uriOptions = [], array $ driverOptions = [])
11593 {
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 ' ] ?? []);
94+ $ this ->driverOptions = DriverOptions::fromArray ($ driverOptions );
13195
13296 $ 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 ' ]);
14097
141- $ driverOptions = array_diff_key ($ driverOptions , ['builderEncoder ' => 1 , 'typeMap ' => 1 ]);
98+ $ driverOptions = array_diff_key ($ this -> driverOptions -> toArray () , ['builderEncoder ' => 1 , 'typeMap ' => 1 ]);
14299
143100 $ this ->manager = new Manager ($ uri , $ uriOptions , $ driverOptions );
144101
@@ -157,8 +114,8 @@ public function __debugInfo(): array
157114 return [
158115 'manager ' => $ this ->manager ,
159116 'uri ' => $ this ->uri ,
160- 'typeMap ' => $ this ->typeMap ,
161- 'builderEncoder ' => $ this ->builderEncoder ,
117+ 'typeMap ' => $ this ->driverOptions -> typeMap ,
118+ 'builderEncoder ' => $ this ->driverOptions -> builderEncoder ,
162119 'writeConcern ' => $ this ->writeConcern ,
163120 ];
164121 }
@@ -226,13 +183,13 @@ public function bulkWrite(BulkWriteCommand|ClientBulkWrite $bulk, array $options
226183 /**
227184 * Returns a ClientEncryption instance for explicit encryption and decryption
228185 *
229- * @param array $options Encryption options
186+ * @param array{kmsProviders?: stdClass|array<string, array>, keyVaultClient?: Client|Manager} $ options
230187 */
231188 public function createClientEncryption (array $ options ): ClientEncryption
232189 {
233- $ options = $ this -> prepareEncryptionOptions ($ options );
190+ $ options = AutoEncryptionOptions:: fromArray ($ options );
234191
235- return $ this ->manager ->createClientEncryption ($ options );
192+ return $ this ->manager ->createClientEncryption ($ options-> toArray () );
236193 }
237194
238195 /**
@@ -269,7 +226,11 @@ public function dropDatabase(string $databaseName, array $options = []): void
269226 */
270227 public function getCollection (string $ databaseName , string $ collectionName , array $ options = []): Collection
271228 {
272- $ options += ['typeMap ' => $ this ->typeMap , 'builderEncoder ' => $ this ->builderEncoder , 'autoEncryptionEnabled ' => $ this ->autoEncryptionEnabled ];
229+ $ options += [
230+ 'typeMap ' => $ this ->driverOptions ->typeMap ,
231+ 'builderEncoder ' => $ this ->driverOptions ->builderEncoder ,
232+ 'autoEncryptionEnabled ' => $ this ->driverOptions ->isAutoEncryptionEnabled (),
233+ ];
273234
274235 return new Collection ($ this ->manager , $ databaseName , $ collectionName , $ options );
275236 }
@@ -284,7 +245,11 @@ public function getCollection(string $databaseName, string $collectionName, arra
284245 */
285246 public function getDatabase (string $ databaseName , array $ options = []): Database
286247 {
287- $ options += ['typeMap ' => $ this ->typeMap , 'builderEncoder ' => $ this ->builderEncoder , 'autoEncryptionEnabled ' => $ this ->autoEncryptionEnabled ];
248+ $ options += [
249+ 'typeMap ' => $ this ->driverOptions ->typeMap ,
250+ 'builderEncoder ' => $ this ->driverOptions ->builderEncoder ,
251+ 'autoEncryptionEnabled ' => $ this ->driverOptions ->isAutoEncryptionEnabled (),
252+ ];
288253
289254 return new Database ($ this ->manager , $ databaseName , $ options );
290255 }
@@ -320,7 +285,7 @@ public function getReadPreference(): ReadPreference
320285 */
321286 public function getTypeMap (): array
322287 {
323- return $ this ->typeMap ;
288+ return $ this ->driverOptions -> typeMap ;
324289 }
325290
326291 /**
@@ -419,8 +384,8 @@ public function startSession(array $options = []): Session
419384 * Create a change stream for watching changes to the cluster.
420385 *
421386 * @see Watch::__construct() for supported options
422- * @param array $pipeline Aggregation pipeline
423- * @param array $options Command options
387+ * @psalm- param list<stage> $pipeline Aggregation pipeline
388+ * @param array $options Command options
424389 * @throws InvalidArgumentException for parameter/option parsing errors
425390 */
426391 public function watch (array $ pipeline = [], array $ options = []): ChangeStream
@@ -429,7 +394,8 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
429394 $ pipeline = new Pipeline (...$ pipeline );
430395 }
431396
432- $ pipeline = $ this ->builderEncoder ->encodeIfSupported ($ pipeline );
397+ /** @var array<array-key, mixed> $pipeline */
398+ $ pipeline = $ this ->driverOptions ->builderEncoder ->encodeIfSupported ($ pipeline );
433399
434400 if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ($ options )) {
435401 $ options ['readPreference ' ] = $ this ->readPreference ;
@@ -442,76 +408,11 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
442408 }
443409
444410 if (! isset ($ options ['typeMap ' ])) {
445- $ options ['typeMap ' ] = $ this ->typeMap ;
411+ $ options ['typeMap ' ] = $ this ->driverOptions -> typeMap ;
446412 }
447413
448414 $ operation = new Watch ($ this ->manager , null , null , $ pipeline , $ options );
449415
450416 return $ operation ->execute ($ server );
451417 }
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- }
517418}
0 commit comments