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+ /**
50+ * @psalm-import-type stage from Builder\Pipeline
51+ */
5752class Client implements Stringable
5853{
5954 public const DEFAULT_URI = 'mongodb://127.0.0.1/ ' ;
6055
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-
7156 private Manager $ manager ;
7257
7358 private ReadConcern $ readConcern ;
@@ -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 }
@@ -226,13 +185,13 @@ public function bulkWrite(BulkWriteCommand|ClientBulkWrite $bulk, array $options
226185 /**
227186 * Returns a ClientEncryption instance for explicit encryption and decryption
228187 *
229- * @param array $options Encryption options
188+ * @param array{kmsProviders?: stdClass|array<string, array>, keyVaultClient?: Client|Manager} $ 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 /**
@@ -419,8 +386,8 @@ public function startSession(array $options = []): Session
419386 * Create a change stream for watching changes to the cluster.
420387 *
421388 * @see Watch::__construct() for supported options
422- * @param array $pipeline Aggregation pipeline
423- * @param array $options Command options
389+ * @psalm- param list<stage> $pipeline Aggregation pipeline
390+ * @param array $options Command options
424391 * @throws InvalidArgumentException for parameter/option parsing errors
425392 */
426393 public function watch (array $ pipeline = [], array $ options = []): ChangeStream
@@ -429,7 +396,8 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
429396 $ pipeline = new Pipeline (...$ pipeline );
430397 }
431398
432- $ pipeline = $ this ->builderEncoder ->encodeIfSupported ($ pipeline );
399+ /** @var array<array-key, mixed> $pipeline */
400+ $ pipeline = $ this ->driverOptions ->builderEncoder ->encodeIfSupported ($ pipeline );
433401
434402 if (! isset ($ options ['readPreference ' ]) && ! is_in_transaction ($ options )) {
435403 $ options ['readPreference ' ] = $ this ->readPreference ;
@@ -442,76 +410,11 @@ public function watch(array $pipeline = [], array $options = []): ChangeStream
442410 }
443411
444412 if (! isset ($ options ['typeMap ' ])) {
445- $ options ['typeMap ' ] = $ this ->typeMap ;
413+ $ options ['typeMap ' ] = $ this ->driverOptions -> typeMap ;
446414 }
447415
448416 $ operation = new Watch ($ this ->manager , null , null , $ pipeline , $ options );
449417
450418 return $ operation ->execute ($ server );
451419 }
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- }
517420}
0 commit comments