mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 15:50:03 +01:00
There are a few "problems" with the current engine: - Allowing each rule to execute assert() and check() means duplication in some cases. - Because we use exceptions to assert/check, we can only invert a validation (with Not) if there are errors. That means that we have limited granularity control. - There is a lot of logic in the exceptions. That means that even after it throws an exception, something could still happen. We're stable on that front, but I want to simplify them. Besides, debugging exception code is painful because the stack trace does not go beyond the exception. Apart from that, there are many limitations with templating, and working that out in the current implementation makes it much harder. These changes will improve the library in many aspects, but they will also change the behavior and break backward compatibility. However, that's a price I'm willing to pay for the improvements we'll have. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
106 lines
3 KiB
PHP
106 lines
3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Message;
|
|
|
|
use ReflectionClass;
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
use Respect\Validation\Message\Parameter\Processor;
|
|
use Respect\Validation\Mode;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Rule;
|
|
use Throwable;
|
|
|
|
use function call_user_func;
|
|
use function preg_replace_callback;
|
|
use function sprintf;
|
|
|
|
final class StandardRenderer implements Renderer
|
|
{
|
|
/** @var array<string, array<Template>> */
|
|
private array $templates = [];
|
|
|
|
/** @var callable */
|
|
private $translator;
|
|
|
|
public function __construct(
|
|
callable $translator,
|
|
private readonly Processor $processor
|
|
) {
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
public function render(Result $result, ?string $template = null): string
|
|
{
|
|
$parameters = $result->parameters;
|
|
$parameters['name'] ??= $result->name ?? $this->processor->process('input', $result->input);
|
|
$parameters['input'] = $result->input;
|
|
|
|
$rendered = (string) preg_replace_callback(
|
|
'/{{(\w+)(\|([^}]+))?}}/',
|
|
function (array $matches) use ($parameters) {
|
|
if (!isset($parameters[$matches[1]])) {
|
|
return $matches[0];
|
|
}
|
|
|
|
return $this->processor->process($matches[1], $parameters[$matches[1]], $matches[3] ?? null);
|
|
},
|
|
$this->translate($template ?? $this->getTemplateMessage($result))
|
|
);
|
|
|
|
if (!$result->hasCustomTemplate() && $result->nextSibling !== null) {
|
|
$rendered .= ' ' . $this->render($result->nextSibling);
|
|
}
|
|
|
|
return $rendered;
|
|
}
|
|
|
|
private function translate(string $message): string
|
|
{
|
|
try {
|
|
return call_user_func($this->translator, $message);
|
|
} catch (Throwable $throwable) {
|
|
throw new ComponentException(sprintf('Failed to translate "%s"', $message), 0, $throwable);
|
|
}
|
|
}
|
|
|
|
/** @return array<Template> */
|
|
private function extractTemplates(Rule $rule): array
|
|
{
|
|
if (!isset($this->templates[$rule::class])) {
|
|
$reflection = new ReflectionClass($rule);
|
|
foreach ($reflection->getAttributes(Template::class) as $attribute) {
|
|
$this->templates[$rule::class][] = $attribute->newInstance();
|
|
}
|
|
}
|
|
|
|
return $this->templates[$rule::class] ?? [];
|
|
}
|
|
|
|
private function getTemplateMessage(Result $result): string
|
|
{
|
|
if ($result->hasCustomTemplate()) {
|
|
return $result->template;
|
|
}
|
|
|
|
foreach ($this->extractTemplates($result->rule) as $template) {
|
|
if ($template->id !== $result->template) {
|
|
continue;
|
|
}
|
|
|
|
if ($result->mode == Mode::NEGATIVE) {
|
|
return $template->negative;
|
|
}
|
|
|
|
return $template->default;
|
|
}
|
|
|
|
return $result->template;
|
|
}
|
|
}
|