99use MongoDB \Driver \Server ;
1010use stdClass ;
1111use function array_key_exists ;
12+ use function array_map ;
1213use function assertArrayHasKey ;
1314use function assertContains ;
1415use function assertCount ;
@@ -181,6 +182,26 @@ public function getEventObserverForClient(string $id) : EventObserver
181182 return $ this ->eventObserversByClient [$ id ];
182183 }
183184
185+ /** @param string|array $readPreferenceTags */
186+ private function convertReadPreferenceTags ($ readPreferenceTags ) : array
187+ {
188+ return array_map (
189+ static function (string $ readPreferenceTagSet ) : array {
190+ $ tags = explode (', ' , $ readPreferenceTagSet );
191+ 192+ return array_map (
193+ static function (string $ tag ) : array {
194+ list ($ key , $ value ) = explode (': ' , $ tag );
195+ 196+ return [$ key => $ value ];
197+ },
198+ $ tags
199+ );
200+ },
201+ (array ) $ readPreferenceTags
202+ );
203+ }
204+ 184205 private function createClient (string $ id , stdClass $ o )
185206 {
186207 Util::assertHasOnlyKeys ($ o , ['id ' , 'uriOptions ' , 'useMultipleMongoses ' , 'observeEvents ' , 'ignoreCommandMonitoringEvents ' ]);
@@ -205,10 +226,18 @@ private function createClient(string $id, stdClass $o)
205226
206227 if (isset ($ o ->uriOptions )) {
207228 assertInternalType ('object ' , $ o ->uriOptions );
208- /* TODO: If readPreferenceTags is set, assert it is an array of
209- * strings and convert to an array of documents expected by the
210- * PHP driver. */
211229 $ uriOptions = (array ) $ o ->uriOptions ;
230+ 231+ if (! empty ($ uriOptions ['readPreferenceTags ' ])) {
232+ /* readPreferenceTags may take the following form:
233+ *
234+ * 1. A string containing multiple tags: "dc:ny,rack:1".
235+ * Expected result: [["dc" => "ny", "rack" => "1"]]
236+ * 2. An array containing multiple strings as above: ["dc:ny,rack:1", "dc:la"].
237+ * Expected result: [["dc" => "ny", "rack" => "1"], ["dc" => "la"]]
238+ */
239+ $ uriOptions ['readPreferenceTags ' ] = $ this ->convertReadPreferenceTags ($ uriOptions ['readPreferenceTags ' ]);
240+ }
212241 }
213242
214243 if (isset ($ observeEvents )) {
0 commit comments