Skip to content

Commit

Permalink
fix: Only replace hyphens in placeholders when dealing with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Jan 25, 2025
1 parent 7d21db1 commit 8113d9a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Concerns/FindsIdentityInRouteParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ public function setParameter(string $parameter): void
*/
public function getRouteParameterName(Tenancy $tenancy): string
{
return PlaceholderHelper::replace(
return PlaceholderHelper::replaceForParameter(
$this->getParameter(),
[
'tenancy' => $tenancy->getName(),
'resolver' => $this->getName(),
]
],
);
}

Expand Down
50 changes: 35 additions & 15 deletions src/Support/PlaceholderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,28 @@ final class PlaceholderHelper
*
* @return string
*/
public static function replace(string $pattern, array $placeholders = []): string
public static function replaceForParameter(string $pattern, array $placeholders = []): string
{
return self::replace($pattern, $placeholders, true);
}

/**
* @param string $pattern
* @param array<lowercase-string, string|callable():string> $placeholders
* @param bool $forParameter
*
* @return string
*/
public static function replace(string $pattern, array $placeholders = [], bool $forParameter = false): string
{
$newString = $pattern;

foreach ($placeholders as $placeholder => $replacement) {
$newString = self::replacePlaceholder(
$newString,
$placeholder,
! is_string($replacement) ? $replacement() : $replacement
! is_string($replacement) ? $replacement() : $replacement,
$forParameter
);
}

Expand All @@ -33,21 +46,28 @@ public static function replace(string $pattern, array $placeholders = []): strin
*
* @return string
*/
private static function replacePlaceholder(string $string, string $placeholder, string $value): string
private static function replacePlaceholder(string $string, string $placeholder, string $value, bool $forParameter): string
{
$search = [
'{' . strtolower($placeholder) . '}',
'{' . ucfirst($placeholder) . '}',
'{' . strtoupper($placeholder) . '}',
];

$replace = [
$value,
ucfirst($value),
strtoupper($value),
];

if ($forParameter) {
$search[] = '-';
$replace[] = '_';
}

return str_replace(
[
'-',
'{' . strtolower($placeholder) . '}',
'{' . ucfirst($placeholder) . '}',
'{' . strtoupper($placeholder) . '}',
],
[
'_',
$value,
ucfirst($value),
strtoupper($value),
],
$search,
$replace,
$string
);
}
Expand Down

0 comments on commit 8113d9a

Please sign in to comment.