Skip to content

Commit

Permalink
Strings::sprintAssoc should treat null and empty string the same
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrooo committed Jun 27, 2023
1 parent 03d9715 commit 1905be1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Ouzo/Goodies/Utilities/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ public static function sprintAssoc(string|array|null $string, ?array $params): a
return $string;
}
foreach ($params as $key => $value) {
if (!is_null($key) && !is_null($value)) {
if (!is_null($key)) {
$value ??= Strings::EMPTY;
$string = preg_replace("/%{($key)}/", $value, $string);
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/src/Ouzo/Goodies/Utilities/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ public static function sprintAssoc(): array
['This is %{what}! %{what}? This is %{place}! And %{invalid_placeholder}!', ['what' => 'madness', 'place' => 'Sparta'], 'This is madness! madness? This is Sparta! And %{invalid_placeholder}!'],
[null, ['what' => 'madness', 'place' => 'Sparta'], null],
['This is %{what}! %{what}? This is %{place}!', null, 'This is %{what}! %{what}? This is %{place}!'],
['This is %{what}! %{what}? This is %{place}!', ['what' => 'madness', 'place' => null, null => 'Sparta'], 'This is madness! madness? This is %{place}!'],
['This is %{what}! %{what}? This is %{place}!', ['what' => 'madness', 'place' => '', null => 'Sparta'], 'This is madness! madness? This is !'],
['This is %{what}! %{what}? This is %{place}!', ['what' => 'madness', 'place' => null, null => 'Sparta'], 'This is madness! madness? This is !'],
];
}

Expand Down

0 comments on commit 1905be1

Please sign in to comment.