Skip to content

Commit fd32552

Browse files
committed
DB: connection array config helper [ToDo Docs]
1 parent 5eb98a1 commit fd32552

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Db/Helper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,20 @@ private static function doCreatePgArray(array $array, bool $string): string
5050
return '{' . \implode(',', $array) . '}';
5151
}
5252

53+
54+
/**
55+
* @param array<string, string|int|float|NULL> $config
56+
*/
57+
public static function prepareConfig(array $config): string
58+
{
59+
$configItems = [];
60+
foreach ($config as $key => $value) {
61+
if ($value !== NULL) {
62+
$configItems[] = $key . '=\'' . $value . '\'';
63+
}
64+
}
65+
66+
return \implode(' ', $configItems);
67+
}
68+
5369
}

tests/Unit/BasicTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public function testPreventConnectionUnserialization(): void
7070
}, \RuntimeException::class, 'You can\'t serialize or unserialize \'Forrest79\PhPgSql\Db\Connection\' instances.');
7171
}
7272

73+
74+
public function testPrepareConfigHelper(): void
75+
{
76+
Tester\Assert::same('dbname=\'test_db\' port=\'5432\' connection_timeout=\'1.5\'', Db\Helper::prepareConfig(['dbname' => 'test_db', 'port' => 5432, 'connection_timeout' => 1.5, 'password' => NULL]));
77+
}
78+
7379
}
7480

7581
(new BasicTest())->run();

0 commit comments

Comments
 (0)