Skip to content

Commit

Permalink
Fix StrSubstitutor for php 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrooo committed Jun 27, 2023
1 parent 002cea2 commit 1230eda
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Ouzo/Goodies/Utilities/StrSubstitutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ouzo\Utilities;

class StrSubstitutor
readonly class StrSubstitutor
{
private const START = '{{';
private const END = '}}';
Expand All @@ -20,6 +20,10 @@ public function __construct(

public function replace(?string $string): string
{
if (is_null($string)) {
return Strings::EMPTY;
}

$start = preg_quote(self::START);
$end = preg_quote(self::END);
return preg_replace_callback("/{$start}(.+?){$end}/u", [$this, 'replaceVars'], $string);
Expand All @@ -30,6 +34,6 @@ private function replaceVars(array $match): string
$matched = $match[0];
$name = $match[1];
$default = is_null($this->default) ? $matched : $this->default;
return isset($this->values[$name]) ? $this->values[$name] : $default;
return $this->values[$name] ?? $default;
}
}

0 comments on commit 1230eda

Please sign in to comment.