respect-validation/library/Message/Formatter/NestedArrayFormatter.php
Henrique Moody 82e0722443
Split the Formatter into different formatters
I've noticed that the `StandardFormatter` was quite bloated, which made
it difficult to maintain. Understanding what each method was doing was
quite complicated. Besides, the name "Standard" doesn't mean anything,
because it doesn't say what the implementation does.

I split the `Formatter` into two different interfaces: `StringFormatter`
and `ArrayFormatter`, and I moved some code around:

*  `StandardFormatter::main()` -> `FirstResultStringFormatter`
* `StandardFormatter::full()` -> `NestedListStringFormatter`
* `StandardFormatter::array()` -> `NestedArrayFormatter`

That opens up new ways of handling error messages, potentially
introducing features like `JsonStringFormatter` or `FlatArrayFormatter`
in the future.

While working on this, I removed a significant amount of unnecessary
code, which also improved my overall understanding of those formatters.

I'm not very happy with all the methods in `ValidatorDefaults`, but I
will refactor that later.
2025-12-19 16:20:28 +01:00

75 lines
2.1 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
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;
use function current;
final readonly class NestedArrayFormatter implements ArrayFormatter
{
use PathProcessor;
public function __construct(
private Renderer $renderer,
private TemplateResolver $templateResolver,
) {
}
/**
* @param array<string|int, mixed> $templates
*
* @return array<string|int, mixed>
*/
public function format(Result $result, array $templates, Translator $translator): array
{
$matchedTemplates = $this->templateResolver->selectMatches($result, $templates);
if (count($result->children) === 0 || $this->templateResolver->hasMatch($result, $matchedTemplates)) {
return [
$result->getDeepestPath() ?? $result->id => $this->renderer->render(
$this->templateResolver->resolve($result->withDeepestPath(), $matchedTemplates),
$translator,
),
];
}
$messages = [];
foreach ($result->children as $child) {
$key = $child->getDeepestPath() ?? $child->id ?? 0;
$messages[$key] = $this->format(
$this->overwritePath($result, $child),
$this->templateResolver->selectMatches($child, $matchedTemplates),
$translator,
);
if (count($messages[$key]) !== 1) {
continue;
}
$messages[$key] = current($messages[$key]);
}
if (count($messages) > 1) {
$self = [
'__root__' => $this->renderer->render(
$this->templateResolver->resolve($result->withDeepestPath(), $matchedTemplates),
$translator,
),
];
return $self + $messages;
}
return $messages;
}
}