Add key map for connection string parser

This commit is contained in:
Aaron Piotrowski 2018-07-16 15:52:01 -05:00
parent 6f72cb76da
commit 37d947b422
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB

View File

@ -4,6 +4,13 @@ namespace Amp\Sql;
abstract class ConnectionConfig abstract class ConnectionConfig
{ {
const KEY_MAP = [
'username' => 'user',
'pass' => 'password',
'database' => 'db',
'dbname' => 'db',
];
/** @var string */ /** @var string */
private $host; private $host;
@ -23,10 +30,11 @@ abstract class ConnectionConfig
* Parses a connection string into an array of keys and values given. * Parses a connection string into an array of keys and values given.
* *
* @param string $connectionString * @param string $connectionString
* @param string[] $keymap Map of alternative key names to canonical key names.
* *
* @return array * @return array
*/ */
protected static function parseConnectionString(string $connectionString): array protected static function parseConnectionString(string $connectionString, array $keymap = self::KEY_MAP): array
{ {
$values = []; $values = [];
@ -38,6 +46,11 @@ abstract class ConnectionConfig
foreach ($params as $param) { foreach ($params as $param) {
list($key, $value) = \array_map("trim", \explode("=", $param, 2) + [1 => null]); list($key, $value) = \array_map("trim", \explode("=", $param, 2) + [1 => null]);
if (isset($keymap[$key])) {
$key = $keymap[$key];
}
$values[$key] = $value; $values[$key] = $value;
} }
@ -143,5 +156,4 @@ abstract class ConnectionConfig
$new->database = $database; $new->database = $database;
return $new; return $new;
} }
} }