mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 15:50:03 +01:00
Change the contract of the formatters
Both `ArrayFormatter` and `StringFormatter` accept an instance of the `Translator`. Thinking about it a bit better, I realised that a formatter might not always need a `Translator`, but it will surely need a `Renderer`. Besides, the `InterpolationRenderer` needs to take translation into account, so it seems more natural to me that this is the one that will get an instance of the `Translator`, as other implementations of the `Renderer` might not even deal with translations.
This commit is contained in:
parent
fd7438f9c7
commit
644ecb5190
14 changed files with 91 additions and 121 deletions
|
|
@ -11,7 +11,6 @@ namespace Respect\Validation\Message\Formatter;
|
|||
|
||||
use Respect\Validation\Message\ArrayFormatter;
|
||||
use Respect\Validation\Message\Renderer;
|
||||
use Respect\Validation\Message\Translator;
|
||||
use Respect\Validation\Result;
|
||||
|
||||
use function count;
|
||||
|
|
@ -20,7 +19,6 @@ use function current;
|
|||
final readonly class NestedArrayFormatter implements ArrayFormatter
|
||||
{
|
||||
public function __construct(
|
||||
private Renderer $renderer,
|
||||
private TemplateResolver $templateResolver,
|
||||
) {
|
||||
}
|
||||
|
|
@ -30,14 +28,13 @@ final readonly class NestedArrayFormatter implements ArrayFormatter
|
|||
*
|
||||
* @return array<string|int, mixed>
|
||||
*/
|
||||
public function format(Result $result, array $templates, Translator $translator): array
|
||||
public function format(Result $result, Renderer $renderer, array $templates): array
|
||||
{
|
||||
$matchedTemplates = $this->templateResolver->selectMatches($result, $templates);
|
||||
if (count($result->children) === 0 || $this->templateResolver->hasMatch($result, $matchedTemplates)) {
|
||||
return [
|
||||
$result->path->value ?? $result->id->value => $this->renderer->render(
|
||||
$result->path->value ?? $result->id->value => $renderer->render(
|
||||
$this->templateResolver->resolve($result->withoutParentPath(), $matchedTemplates),
|
||||
$translator,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
|
@ -47,8 +44,8 @@ final readonly class NestedArrayFormatter implements ArrayFormatter
|
|||
$key = $child->path->value ?? $child->id->value;
|
||||
$messages[$key] = $this->format(
|
||||
$child->withoutParentPath()->withoutName(),
|
||||
$renderer,
|
||||
$this->templateResolver->selectMatches($child, $matchedTemplates),
|
||||
$translator,
|
||||
);
|
||||
if (count($messages[$key]) !== 1) {
|
||||
continue;
|
||||
|
|
@ -59,9 +56,8 @@ final readonly class NestedArrayFormatter implements ArrayFormatter
|
|||
|
||||
if (count($messages) > 1) {
|
||||
$self = [
|
||||
'__root__' => $this->renderer->render(
|
||||
'__root__' => $renderer->render(
|
||||
$this->templateResolver->resolve($result->withoutParentPath(), $matchedTemplates),
|
||||
$translator,
|
||||
),
|
||||
];
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue