-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow templates to have raw and translatable params
In some cases, a user would like to show the parameter just as it is, and in other cases, they need to translate a specific parameter. This change creates that capability by adding a template-style modifier to a parameter in the template. Signed-off-by: Henrique Moody <[email protected]>
- Loading branch information
1 parent
04b2722
commit 26e9fb9
Showing
32 changed files
with
351 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message; | ||
|
||
use Respect\Validation\Exceptions\ComponentException; | ||
use Throwable; | ||
|
||
use function call_user_func; | ||
use function is_scalar; | ||
use function preg_replace_callback; | ||
use function Respect\Stringifier\stringify; | ||
use function sprintf; | ||
|
||
final class TemplateRenderer | ||
{ | ||
/** @var callable */ | ||
private $translator; | ||
|
||
public function __construct( | ||
callable $translator, | ||
private readonly ParameterStringifier $parameterStringifier | ||
) { | ||
$this->translator = $translator; | ||
} | ||
|
||
/** | ||
* @param mixed[] $parameters | ||
*/ | ||
public function render(string $template, mixed $input, array $parameters): string | ||
{ | ||
$parameters['name'] ??= $this->parameterStringifier->stringify('input', $input); | ||
|
||
return (string) preg_replace_callback( | ||
'/{{(\w+)(\|(trans|raw))?}}/', | ||
function (array $matches) use ($parameters): string { | ||
if (!isset($parameters[$matches[1]])) { | ||
return $matches[0]; | ||
} | ||
|
||
$modifier = $matches[3] ?? null; | ||
if ($modifier === 'raw' && is_scalar($parameters[$matches[1]])) { | ||
return (string) $parameters[$matches[1]]; | ||
} | ||
|
||
if ($modifier === 'trans') { | ||
return $this->translate($parameters[$matches[1]]); | ||
} | ||
|
||
return $this->parameterStringifier->stringify($matches[1], $parameters[$matches[1]]); | ||
}, | ||
$this->translate($template) | ||
); | ||
} | ||
|
||
private function translate(mixed $message): string | ||
{ | ||
if (!is_scalar($message)) { | ||
throw new ComponentException(sprintf('Cannot translate scalar value "%s"', stringify($message))); | ||
} | ||
|
||
try { | ||
return call_user_func($this->translator, (string) $message); | ||
} catch (Throwable $throwable) { | ||
throw new ComponentException(sprintf('Failed to translate "%s"', $message), 0, $throwable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.