mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
The `InterpolationRenderer` was violating the open-closed principle, because every time we would want to add a new modifier, we would need to change its implementation. This commit changes that behaviour by creating a `Modifier` interface. The classes implementing that interface are using a chain of responsibility to pass the data to the next one. Using a chain of responsibility makes a lot of sense, since it's only possible to have one modifier at a time.
89 lines
2.7 KiB
PHP
89 lines
2.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Message;
|
|
|
|
use Respect\Validation\Message\Formatter\TemplateResolver;
|
|
use Respect\Validation\Name;
|
|
use Respect\Validation\Result;
|
|
|
|
use function array_key_exists;
|
|
use function array_pad;
|
|
use function assert;
|
|
use function is_string;
|
|
use function preg_replace_callback;
|
|
|
|
final readonly class InterpolationRenderer implements Renderer
|
|
{
|
|
public function __construct(
|
|
private Translator $translator,
|
|
private Modifier $modifier,
|
|
private TemplateResolver $templateResolver = new TemplateResolver(),
|
|
) {
|
|
}
|
|
|
|
/** @param array<string|int, mixed> $templates */
|
|
public function render(Result $result, array $templates): string
|
|
{
|
|
$parameters = ['path' => $result->path, 'input' => $result->input, 'subject' => $this->getName($result)];
|
|
$parameters += $result->parameters;
|
|
|
|
$givenTemplate = $this->templateResolver->getGivenTemplate($result, $templates);
|
|
$ruleTemplate = $this->templateResolver->getRuleTemplate($result);
|
|
|
|
$rendered = (string) preg_replace_callback(
|
|
'/{{(\w+)(\|([^}]+))?}}/',
|
|
fn(array $matches) => $this->processPlaceholder($parameters, $matches),
|
|
$this->translator->translate($givenTemplate ?? $ruleTemplate),
|
|
);
|
|
|
|
if (!$result->hasCustomTemplate() && $givenTemplate === null && $result->adjacent !== null) {
|
|
$rendered .= ' ' . $this->render($result->adjacent, $templates);
|
|
}
|
|
|
|
return $rendered;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $parameters
|
|
* @param array<int, string|null> $matches
|
|
*/
|
|
private function processPlaceholder(array $parameters, array $matches): string
|
|
{
|
|
[$placeholder, $name, , $pipe] = array_pad($matches, 4, null);
|
|
assert(is_string($placeholder));
|
|
assert(is_string($name));
|
|
if (!array_key_exists($name, $parameters)) {
|
|
return $placeholder;
|
|
}
|
|
|
|
return $this->modifier->modify($parameters[$name], $pipe);
|
|
}
|
|
|
|
private function getName(Result $result): mixed
|
|
{
|
|
if (array_key_exists('name', $result->parameters) && is_string($result->parameters['name'])) {
|
|
return new Name($result->parameters['name']);
|
|
}
|
|
|
|
if (array_key_exists('name', $result->parameters)) {
|
|
return $result->parameters['name'];
|
|
}
|
|
|
|
if ($result->name !== null) {
|
|
return $result->name;
|
|
}
|
|
|
|
if ($result->path?->value !== null) {
|
|
return $result->path;
|
|
}
|
|
|
|
return $result->input;
|
|
}
|
|
}
|