Skip to content

Commit

Permalink
Handle nullable value in params passed to Strings::sprintAssocDefault…
Browse files Browse the repository at this point in the history
…() (#316)
  • Loading branch information
piotrooo committed Jul 12, 2023
1 parent 9dd5556 commit 34cd2cf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Ouzo/Goodies/Utilities/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public static function sprintAssocDefault(?string $string, ?array $params, ?stri
}
if (!is_null($params)) {
foreach ($params as $key => $value) {
$string = preg_replace("/%{($key)}/", $value, $string);
$string = preg_replace("/%{($key)}/", $value ?: $default, $string);
}
}
if (!is_null($default)) {
Expand Down
2 changes: 2 additions & 0 deletions test/src/Ouzo/Goodies/Utilities/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ public static function sprintAssocDefault(): array
return [
['This is %{what}! This is %{place}! No, this is invalid %{invalid_placeholder} placeholder!', ['what' => 'madness', 'place' => 'Sparta'], '', 'This is madness! This is Sparta! No, this is invalid placeholder!'],
['This is %{what}! This is %{place}! No, this is invalid %{invalid_placeholder} placeholder!', ['what' => 'madness', 'place' => 'Sparta'], 'tomato', 'This is madness! This is Sparta! No, this is invalid tomato placeholder!'],
['This is %{what}! This is %{place}! No, this is invalid %{invalid_placeholder} placeholder!', ['what' => 'madness', 'place' => null], 'tomato', 'This is madness! This is tomato! No, this is invalid tomato placeholder!'],
['This is %{what}! This is %{place}! No, this is invalid %{invalid_placeholder} placeholder!', ['what' => 'madness', 'place' => ''], 'tomato', 'This is madness! This is tomato! No, this is invalid tomato placeholder!'],
[null, ['what' => 'madness', 'place' => 'Sparta'], 'tomato', null],
['This is %{what}! This is %{place}! No, this is invalid %{invalid_placeholder} placeholder!', null, 'tomato', 'This is tomato! This is tomato! No, this is invalid tomato placeholder!'],
['This is %{what}! This is %{place}! No, this is invalid %{invalid_placeholder} placeholder!', null, null, 'This is %{what}! This is %{place}! No, this is invalid %{invalid_placeholder} placeholder!'],
Expand Down

0 comments on commit 34cd2cf

Please sign in to comment.