Skip to content

Commit 8fd826d

Browse files
committed
Add ability to provide key => value pairs in Str::list
1 parent 442c409 commit 8fd826d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Support/Str.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ public static function list(array $list): string
1313
{
1414
$values = [];
1515

16-
foreach ($list as $value) {
16+
foreach ($list as $key => $value) {
1717
if (is_array($value)) {
18-
$values[] = static::list($value);
18+
$token = static::list($value);
1919
} else {
20-
$values[] = $value;
20+
$token = $value;
2121
}
22+
23+
if (is_string($key)) {
24+
$token = $key.' '.$token;
25+
}
26+
27+
$values[] = $token;
2228
}
2329

2430
return sprintf('(%s)', implode(' ', $values));

tests/Unit/Support/StrTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
expect(Str::list([]))->toBe('()');
6666
});
6767

68+
test('list returns a properly formatted parenthesized list for an array with string keys', function () {
69+
expect(Str::list(['a' => '"b"', 'c' => '"d"']))->toBe('(a "b" c "d")');
70+
expect(Str::list(['a' => ['"b"', '"c"'], 'd' => '"e"']))->toBe('(a ("b" "c") d "e")');
71+
});
72+
6873
test('enums returns value for a single backed enum', function () {
6974
$result = Str::enums(ImapFlag::Seen);
7075

0 commit comments

Comments
 (0)