public static function _validateReadPreference($readPreference, array $tags = null)
{
$new_preference = [];
if (strcasecmp($readPreference, self::RP_PRIMARY) === 0) {
if (!empty($tags)) {
trigger_error("You can't use read preference tags with a read preference of PRIMARY", E_USER_WARNING);
return false;
}
$new_preference['type'] = self::RP_PRIMARY;
} elseif (strcasecmp($readPreference, self::RP_PRIMARY_PREFERRED) === 0) {
$new_preference['type'] = self::RP_PRIMARY_PREFERRED;
} elseif (strcasecmp($readPreference, self::RP_SECONDARY) === 0) {
$new_preference['type'] = self::RP_SECONDARY;
} elseif (strcasecmp($readPreference, self::RP_SECONDARY_PREFERRED) === 0) {
$new_preference['type'] = self::RP_SECONDARY_PREFERRED;
} elseif (strcasecmp($readPreference, self::RP_NEAREST) === 0) {
$new_preference['type'] = self::RP_NEAREST;
} else {
trigger_error("The value '{$readPreference}' is not valid as read preference type", E_USER_WARNING);
return false;
}
if ($tags) {
// Also supports string format (dc:east,use:reporting), convert to arrays
foreach ($tags as $i => $tagset) {
if (is_string($tagset)) {
$array = [];
// Empty string can be used to allow no tag matching in the case where
// tagsets specified earlier in the array do not match any servers
$tagset = $tagset ? explode(',', $tagset) : [];
foreach ($tagset as $key_value_pair) {
list($key, $value) = explode(':', $key_value_pair);
$key = trim($key);
$value = trim($value);
if ($key === '' || $value === '') {
$msg = "Invalid tagset \"{$key_value_pair}\". Must contain non-empty key and value.";
trigger_error($msg, E_USER_WARNING);
return false;
}
$array[$key] = $value;
}
$tags[$i] = $array;
}
}
$new_preference['tagsets'] = $tags;
}
return $new_preference;
}