9
9
use MongoDB \Driver \Server ;
10
10
use stdClass ;
11
11
use function array_key_exists ;
12
+ use function array_map ;
12
13
use function assertArrayHasKey ;
13
14
use function assertContains ;
14
15
use function assertCount ;
@@ -181,6 +182,26 @@ public function getEventObserverForClient(string $id) : EventObserver
181
182
return $ this ->eventObserversByClient [$ id ];
182
183
}
183
184
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
+
184
205
private function createClient (string $ id , stdClass $ o )
185
206
{
186
207
Util::assertHasOnlyKeys ($ o , ['id ' , 'uriOptions ' , 'useMultipleMongoses ' , 'observeEvents ' , 'ignoreCommandMonitoringEvents ' ]);
@@ -205,10 +226,18 @@ private function createClient(string $id, stdClass $o)
205
226
206
227
if (isset ($ o ->uriOptions )) {
207
228
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. */
211
229
$ 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
+ }
212
241
}
213
242
214
243
if (isset ($ observeEvents )) {
0 commit comments