-
Notifications
You must be signed in to change notification settings - Fork 779
/
Copy pathStandardRenderer.php
127 lines (100 loc) · 3.96 KB
/
StandardRenderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Message;
use ReflectionClass;
use Respect\Stringifier\Stringifier;
use Respect\Validation\Message\Placeholder\Listed;
use Respect\Validation\Message\Placeholder\Quoted;
use Respect\Validation\Mode;
use Respect\Validation\Result;
use Respect\Validation\Rule;
use function is_array;
use function is_bool;
use function is_scalar;
use function is_string;
use function preg_replace_callback;
use function print_r;
final class StandardRenderer implements Renderer
{
/** @var array<string, array<Template>> */
private array $templates = [];
public function __construct(
private readonly Stringifier $stringifier = new StandardStringifier(),
) {
}
public function render(Result $result, Translator $translator, ?string $template = null): string
{
$parameters = $result->parameters;
$parameters['path'] = $result->path !== null ? Quoted::fromPath($result->path) : null;
$parameters['input'] = $result->input;
$builtName = $result->name ?? $parameters['path'] ?? $this->placeholder('input', $result->input, $translator);
$parameters['name'] ??= $builtName;
$rendered = (string) preg_replace_callback(
'/{{(\w+)(\|([^}]+))?}}/',
function (array $matches) use ($parameters, $translator) {
if (!isset($parameters[$matches[1]])) {
return $matches[0];
}
return $this->placeholder($matches[1], $parameters[$matches[1]], $translator, $matches[3] ?? null);
},
$translator->translate($template ?? $this->getTemplateMessage($result))
);
if (!$result->hasCustomTemplate() && $result->adjacent !== null) {
$rendered .= ' ' . $this->render($result->adjacent, $translator);
}
return $rendered;
}
/** @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 placeholder(string $name, mixed $value, Translator $translator, ?string $modifier = null): string
{
if ($modifier === 'quote' && is_string($value)) {
return $this->placeholder($name, new Quoted($value), $translator);
}
if ($modifier === 'listOr' && is_array($value)) {
return $this->placeholder($name, new Listed($value, $translator->translate('or')), $translator);
}
if ($modifier === 'listAnd' && is_array($value)) {
return $this->placeholder($name, new Listed($value, $translator->translate('and')), $translator);
}
if ($modifier === 'raw' && is_scalar($value)) {
return is_bool($value) ? (string) (int) $value : (string) $value;
}
if ($modifier === 'trans' && is_string($value)) {
return $translator->translate($value);
}
if ($name === 'name' && is_string($value)) {
return $value;
}
return $this->stringifier->stringify($value, 0) ?? print_r($value, true);
}
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::INVERTED) {
return $template->inverted;
}
return $template->default;
}
return $result->template;
}
}