Skip to content

Commit 644ecb5

Browse files
committed
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.
1 parent fd7438f commit 644ecb5

14 files changed

+91
-121
lines changed

library/ContainerRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public static function createContainer(): Container
5151
'respect.validation.ignored_backtrace_paths' => [__DIR__ . '/Validator.php'],
5252
Validator::class => factory(static fn(Container $container) => new Validator(
5353
$container->get(Factory::class),
54+
$container->get(Renderer::class),
5455
$container->get('respect.validation.formatter.message'),
5556
$container->get('respect.validation.formatter.full_message'),
5657
$container->get('respect.validation.formatter.messages'),
57-
$container->get(Translator::class),
5858
$container->get(ResultFilter::class),
5959
$container->get('respect.validation.ignored_backtrace_paths'),
6060
)),

library/Message/ArrayFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ interface ArrayFormatter
1818
*
1919
* @return array<string, mixed>
2020
*/
21-
public function format(Result $result, array $templates, Translator $translator): array;
21+
public function format(Result $result, Renderer $renderer, array $templates): array;
2222
}

library/Message/Formatter/FirstResultStringFormatter.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,25 @@
1111

1212
use Respect\Validation\Message\Renderer;
1313
use Respect\Validation\Message\StringFormatter;
14-
use Respect\Validation\Message\Translator;
1514
use Respect\Validation\Result;
1615

1716
final readonly class FirstResultStringFormatter implements StringFormatter
1817
{
1918
public function __construct(
20-
private Renderer $renderer,
2119
private TemplateResolver $templateResolver,
2220
) {
2321
}
2422

2523
/** @param array<string|int, mixed> $templates */
26-
public function format(Result $result, array $templates, Translator $translator): string
24+
public function format(Result $result, Renderer $renderer, array $templates): string
2725
{
2826
$matchedTemplates = $this->templateResolver->selectMatches($result, $templates);
2927
if (!$this->templateResolver->hasMatch($result, $matchedTemplates)) {
3028
foreach ($result->children as $child) {
31-
return $this->format(
32-
$child,
33-
$matchedTemplates,
34-
$translator,
35-
);
29+
return $this->format($child, $renderer, $matchedTemplates);
3630
}
3731
}
3832

39-
return $this->renderer->render($this->templateResolver->resolve($result, $matchedTemplates), $translator);
33+
return $renderer->render($this->templateResolver->resolve($result, $matchedTemplates));
4034
}
4135
}

library/Message/Formatter/NestedArrayFormatter.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Respect\Validation\Message\ArrayFormatter;
1313
use Respect\Validation\Message\Renderer;
14-
use Respect\Validation\Message\Translator;
1514
use Respect\Validation\Result;
1615

1716
use function count;
@@ -20,7 +19,6 @@
2019
final readonly class NestedArrayFormatter implements ArrayFormatter
2120
{
2221
public function __construct(
23-
private Renderer $renderer,
2422
private TemplateResolver $templateResolver,
2523
) {
2624
}
@@ -30,14 +28,13 @@ public function __construct(
3028
*
3129
* @return array<string|int, mixed>
3230
*/
33-
public function format(Result $result, array $templates, Translator $translator): array
31+
public function format(Result $result, Renderer $renderer, array $templates): array
3432
{
3533
$matchedTemplates = $this->templateResolver->selectMatches($result, $templates);
3634
if (count($result->children) === 0 || $this->templateResolver->hasMatch($result, $matchedTemplates)) {
3735
return [
38-
$result->path->value ?? $result->id->value => $this->renderer->render(
36+
$result->path->value ?? $result->id->value => $renderer->render(
3937
$this->templateResolver->resolve($result->withoutParentPath(), $matchedTemplates),
40-
$translator,
4138
),
4239
];
4340
}
@@ -47,8 +44,8 @@ public function format(Result $result, array $templates, Translator $translator)
4744
$key = $child->path->value ?? $child->id->value;
4845
$messages[$key] = $this->format(
4946
$child->withoutParentPath()->withoutName(),
47+
$renderer,
5048
$this->templateResolver->selectMatches($child, $matchedTemplates),
51-
$translator,
5249
);
5350
if (count($messages[$key]) !== 1) {
5451
continue;
@@ -59,9 +56,8 @@ public function format(Result $result, array $templates, Translator $translator)
5956

6057
if (count($messages) > 1) {
6158
$self = [
62-
'__root__' => $this->renderer->render(
59+
'__root__' => $renderer->render(
6360
$this->templateResolver->resolve($result->withoutParentPath(), $matchedTemplates),
64-
$translator,
6561
),
6662
];
6763

library/Message/Formatter/NestedListStringFormatter.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Respect\Validation\Message\Renderer;
1313
use Respect\Validation\Message\StringFormatter;
14-
use Respect\Validation\Message\Translator;
1514
use Respect\Validation\Result;
1615

1716
use function array_filter;
@@ -26,23 +25,22 @@
2625
final readonly class NestedListStringFormatter implements StringFormatter
2726
{
2827
public function __construct(
29-
private Renderer $renderer,
3028
private TemplateResolver $templateResolver,
3129
) {
3230
}
3331

3432
/** @param array<string|int, mixed> $templates */
35-
public function format(Result $result, array $templates, Translator $translator): string
33+
public function format(Result $result, Renderer $renderer, array $templates): string
3634
{
37-
return $this->formatRecursively($result, $templates, $translator, 0);
35+
return $this->formatRecursively($result, $renderer, $templates, 0);
3836
}
3937

4038
/** @param array<string|int, mixed> $templates */
4139
private function formatRecursively(
4240
Result $result,
41+
Renderer $renderer,
4342
array $templates,
44-
Translator $translator,
45-
int $depth = 0,
43+
int $depth,
4644
Result ...$siblings,
4745
): string {
4846
$matchedTemplates = $this->templateResolver->selectMatches($result, $templates);
@@ -55,12 +53,11 @@ private function formatRecursively(
5553
$formatted .= sprintf(
5654
'%s- %s' . PHP_EOL,
5755
$indentation,
58-
$this->renderer->render(
56+
$renderer->render(
5957
$this->templateResolver->resolve(
6058
$result->withoutParentPath(),
6159
$matchedTemplates,
6260
),
63-
$translator,
6461
),
6562
);
6663
$depth++;
@@ -70,8 +67,8 @@ private function formatRecursively(
7067
foreach ($result->children as $child) {
7168
$formatted .= $this->formatRecursively(
7269
$displayedName === $child->name ? $child->withoutName() : $child,
70+
$renderer,
7371
$matchedTemplates,
74-
$translator,
7572
$depth,
7673
...array_filter($result->children, static fn(Result $sibling) => $sibling !== $child),
7774
);

library/Message/InterpolationRenderer.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,30 @@ final class InterpolationRenderer implements Renderer
3131
private array $templates = [];
3232

3333
public function __construct(
34+
private readonly Translator $translator,
3435
private readonly Stringifier $stringifier = new ValidationStringifier(),
3536
) {
3637
}
3738

38-
public function render(Result $result, Translator $translator): string
39+
public function render(Result $result): string
3940
{
4041
$parameters = ['path' => $result->path, 'input' => $result->input, 'name' => $this->getName($result)];
4142
$parameters += $result->parameters;
4243

4344
$rendered = (string) preg_replace_callback(
4445
'/{{(\w+)(\|([^}]+))?}}/',
45-
function (array $matches) use ($parameters, $translator) {
46+
function (array $matches) use ($parameters) {
4647
if (!isset($parameters[$matches[1]])) {
4748
return $matches[0];
4849
}
4950

50-
return $this->placeholder($matches[1], $parameters[$matches[1]], $translator, $matches[3] ?? null);
51+
return $this->placeholder($matches[1], $parameters[$matches[1]], $matches[3] ?? null);
5152
},
52-
$translator->translate($this->getTemplateMessage($result)),
53+
$this->translator->translate($this->getTemplateMessage($result)),
5354
);
5455

5556
if (!$result->hasCustomTemplate() && $result->adjacent !== null) {
56-
$rendered .= ' ' . $this->render($result->adjacent, $translator);
57+
$rendered .= ' ' . $this->render($result->adjacent);
5758
}
5859

5960
return $rendered;
@@ -75,27 +76,26 @@ private function extractTemplates(Rule $rule): array
7576
private function placeholder(
7677
string $name,
7778
mixed $value,
78-
Translator $translator,
7979
string|null $modifier = null,
8080
): string {
8181
if ($modifier === 'quote' && is_string($value)) {
82-
return $this->placeholder($name, new Quoted($value), $translator);
82+
return $this->placeholder($name, new Quoted($value));
8383
}
8484

8585
if ($modifier === 'listOr' && is_array($value)) {
86-
return $this->placeholder($name, new Listed($value, $translator->translate('or')), $translator);
86+
return $this->placeholder($name, new Listed($value, $this->translator->translate('or')));
8787
}
8888

8989
if ($modifier === 'listAnd' && is_array($value)) {
90-
return $this->placeholder($name, new Listed($value, $translator->translate('and')), $translator);
90+
return $this->placeholder($name, new Listed($value, $this->translator->translate('and')));
9191
}
9292

9393
if ($modifier === 'raw' && is_scalar($value)) {
9494
return is_bool($value) ? (string) (int) $value : (string) $value;
9595
}
9696

9797
if ($modifier === 'trans' && is_string($value)) {
98-
return $translator->translate($value);
98+
return $this->translator->translate($value);
9999
}
100100

101101
return $this->stringifier->stringify($value, 0) ?? print_r($value, true);

library/Message/Renderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313

1414
interface Renderer
1515
{
16-
public function render(Result $result, Translator $translator): string;
16+
public function render(Result $result): string;
1717
}

library/Message/StringFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
interface StringFormatter
1515
{
1616
/** @param array<string, mixed> $templates */
17-
public function format(Result $result, array $templates, Translator $translator): string;
17+
public function format(Result $result, Renderer $renderer, array $templates): string;
1818
}

library/Validator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
use Respect\Validation\Exceptions\ValidationException;
1313
use Respect\Validation\Message\ArrayFormatter;
14+
use Respect\Validation\Message\Renderer;
1415
use Respect\Validation\Message\StringFormatter;
15-
use Respect\Validation\Message\Translator;
1616
use Respect\Validation\Mixins\Builder;
1717
use Respect\Validation\Rules\Core\Nameable;
1818
use Respect\Validation\Rules\Core\Reducer;
@@ -38,10 +38,10 @@ final class Validator implements Rule, Nameable
3838
/** @param array<string> $ignoredBacktracePaths */
3939
public function __construct(
4040
private readonly Factory $factory,
41+
private readonly Renderer $renderer,
4142
private readonly StringFormatter $mainMessageFormatter,
4243
private readonly StringFormatter $fullMessageFormatter,
4344
private readonly ArrayFormatter $messagesFormatter,
44-
private readonly Translator $translator,
4545
private readonly ResultFilter $resultFilter,
4646
private readonly array $ignoredBacktracePaths,
4747
) {
@@ -89,9 +89,9 @@ public function assert(mixed $input, array|string|Throwable|callable|null $templ
8989
$failedResult = $this->resultFilter->filter($result);
9090

9191
$exception = new ValidationException(
92-
$this->mainMessageFormatter->format($failedResult, $templates, $this->translator),
93-
$this->fullMessageFormatter->format($failedResult, $templates, $this->translator),
94-
$this->messagesFormatter->format($failedResult, $templates, $this->translator),
92+
$this->mainMessageFormatter->format($failedResult, $this->renderer, $templates),
93+
$this->fullMessageFormatter->format($failedResult, $this->renderer, $templates),
94+
$this->messagesFormatter->format($failedResult, $this->renderer, $templates),
9595
$this->ignoredBacktracePaths,
9696
);
9797

tests/library/Message/TestingMessageRenderer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
namespace Respect\Validation\Test\Message;
1111

1212
use Respect\Validation\Message\Renderer;
13-
use Respect\Validation\Message\Translator;
1413
use Respect\Validation\Result;
1514

1615
final class TestingMessageRenderer implements Renderer
1716
{
18-
public function render(Result $result, Translator $translator): string
17+
public function render(Result $result): string
1918
{
2019
return $result->template;
2120
}

0 commit comments

Comments
 (0)