Skip to content

Commit 9b7b10c

Browse files
committed
WIP: Improve messages in an array format
- Name: That's what is in the placeholder, and could be used for path and id as well - Id: Tha could be the name of the rule, just a means to identify it and be able to build templates for it on that level. - Path: That's the real path of the rule, which will be useful when dealing with arrays or objects.
1 parent 74c018b commit 9b7b10c

28 files changed

+232
-153
lines changed

library/Message/StandardFormatter.php

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
use function array_filter;
1616
use function array_key_exists;
17+
use function array_merge;
1718
use function array_reduce;
1819
use function array_values;
1920
use function count;
2021
use function current;
2122
use function is_array;
2223
use function is_string;
24+
use function key;
2325
use function Respect\Stringifier\stringify;
2426
use function rtrim;
2527
use function sprintf;
@@ -35,7 +37,7 @@ public function __construct(
3537
}
3638

3739
/**
38-
* @param array<string, mixed> $templates
40+
* @param array<string|int, mixed> $templates
3941
*/
4042
public function main(Result $result, array $templates, Translator $translator): string
4143
{
@@ -50,7 +52,7 @@ public function main(Result $result, array $templates, Translator $translator):
5052
}
5153

5254
/**
53-
* @param array<string, mixed> $templates
55+
* @param array<string|int, mixed> $templates
5456
*/
5557
public function full(
5658
Result $result,
@@ -91,32 +93,49 @@ public function full(
9193
}
9294

9395
/**
94-
* @param array<string, mixed> $templates
96+
* @param array<string|int, mixed> $templates
9597
*
96-
* @return array<string, mixed>
98+
* @return array<string|int, mixed>
9799
*/
98100
public function array(Result $result, array $templates, Translator $translator): array
99101
{
100102
$selectedTemplates = $this->selectTemplates($result, $templates);
101103
$deduplicatedChildren = $this->extractDeduplicatedChildren($result);
102104
if (count($deduplicatedChildren) === 0 || $this->isFinalTemplate($result, $selectedTemplates)) {
103105
return [
104-
$result->id => $this->renderer->render($this->getTemplated($result, $selectedTemplates), $translator),
106+
$result->path ?? $result->id => $this->renderer->render(
107+
$this->getTemplated($result, $selectedTemplates),
108+
$translator
109+
),
105110
];
106111
}
107112

108113
$messages = [];
109114
foreach ($deduplicatedChildren as $child) {
110-
$messages[$child->id] = $this->array(
115+
$childKey = $child->path ?? $child->id;
116+
117+
$messages[$childKey] = $this->array(
111118
$child,
112119
$this->selectTemplates($child, $selectedTemplates),
113120
$translator
114121
);
115-
if (count($messages[$child->id]) !== 1) {
122+
123+
if ($childKey == 'each' && is_array($messages['each'])) {
124+
$messages = array_merge($messages, $messages['each']);
125+
unset($messages['each']);
126+
continue;
127+
}
128+
129+
if (count($messages[$childKey]) !== 1) {
116130
continue;
117131
}
118132

119-
$messages[$child->id] = current($messages[$child->id]);
133+
$grantChildKey = key($messages[$childKey]);
134+
if ($grantChildKey != $childKey) {
135+
continue;
136+
}
137+
138+
$messages[$grantChildKey] = current($messages[$grantChildKey]);
120139
}
121140

122141
if (count($messages) > 1) {
@@ -165,56 +184,73 @@ private function isAlwaysVisible(Result $result, Result ...$siblings): bool
165184
);
166185
}
167186

168-
/** @param array<string, mixed> $templates */
187+
/** @param array<string|int, mixed> $templates */
169188
private function getTemplated(Result $result, array $templates): Result
170189
{
171190
if ($result->hasCustomTemplate()) {
172191
return $result;
173192
}
174193

175-
if (!isset($templates[$result->id]) && isset($templates['__root__'])) {
176-
return $result->withTemplate($templates['__root__']);
194+
$keys = [$result->name, $result->path, $result->id];
195+
foreach ($keys as $key) {
196+
if (isset($templates[$key]) && is_string($templates[$key])) {
197+
return $result->withTemplate($templates[$key]);
198+
}
177199
}
178200

179-
if (!isset($templates[$result->id])) {
180-
return $result;
201+
if (isset($templates['__root__'])) {
202+
return $result->withTemplate($templates['__root__']);
181203
}
182204

183-
$template = $templates[$result->id];
184-
if (is_string($template)) {
185-
return $result->withTemplate($template);
205+
if (!isset($templates[$result->id]) && !isset($templates[$result->path]) && !isset($templates[$result->name])) {
206+
return $result;
186207
}
187208

188209
throw new ComponentException(
189-
sprintf('Template for "%s" must be a string, %s given', $result->id, stringify($template))
210+
sprintf(
211+
'Template for "%s" must be a string, %s given',
212+
$result->path ?? $result->name ?? $result->id,
213+
stringify($templates)
214+
)
190215
);
191216
}
192217

193218
/**
194-
* @param array<string, mixed> $templates
219+
* @param array<string|int, mixed> $templates
195220
*/
196221
private function isFinalTemplate(Result $result, array $templates): bool
197222
{
198-
if (isset($templates[$result->id]) && is_string($templates[$result->id])) {
199-
return true;
223+
$keys = [$result->name, $result->path, $result->id];
224+
foreach ($keys as $key) {
225+
if (isset($templates[$key]) && is_string($templates[$key])) {
226+
return true;
227+
}
200228
}
201229

202230
if (count($templates) !== 1) {
203231
return false;
204232
}
205233

206-
return isset($templates['__root__']) || isset($templates[$result->id]);
234+
foreach ($keys as $key) {
235+
if (isset($templates[$key])) {
236+
return true;
237+
}
238+
}
239+
240+
return isset($templates['__root__']);
207241
}
208242

209243
/**
210-
* @param array<string, mixed> $templates
244+
* @param array<string|int, mixed> $templates
211245
*
212-
* @return array<string, mixed>
246+
* @return array<string|int, mixed>
213247
*/
214-
private function selectTemplates(Result $message, array $templates): array
248+
private function selectTemplates(Result $result, array $templates): array
215249
{
216-
if (isset($templates[$message->id]) && is_array($templates[$message->id])) {
217-
return $templates[$message->id];
250+
foreach ([$result->name, $result->path, $result->id] as $key) {
251+
if (isset($templates[$key]) && is_array($templates[$key])) {
252+
return $templates[$key];
253+
}
218254
}
219255

220256
return $templates;
@@ -227,7 +263,7 @@ private function extractDeduplicatedChildren(Result $result): array
227263
$deduplicatedResults = [];
228264
$duplicateCounters = [];
229265
foreach ($result->children as $child) {
230-
$id = $child->id;
266+
$id = $child->path ?? $child->id;
231267
if (isset($duplicateCounters[$id])) {
232268
$id .= '.' . ++$duplicateCounters[$id];
233269
} elseif (array_key_exists($id, $deduplicatedResults)) {
@@ -236,7 +272,7 @@ private function extractDeduplicatedChildren(Result $result): array
236272
$duplicateCounters[$id] = 2;
237273
$id .= '.2';
238274
}
239-
$deduplicatedResults[$id] = $child->isValid ? null : $child->withId($id);
275+
$deduplicatedResults[$id] = $child->isValid ? null : $child->withId((string) $id);
240276
}
241277

242278
return array_values(array_filter($deduplicatedResults));

library/Result.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public function __construct(
3939
public readonly Mode $mode = Mode::DEFAULT,
4040
?string $name = null,
4141
?string $id = null,
42+
public readonly string|int|null $path = null,
4243
public readonly ?Result $subsequent = null,
43-
public readonly bool $unchangeableId = false,
4444
Result ...$children,
4545
) {
4646
$this->name = $rule->getName() ?? $name;
@@ -76,21 +76,17 @@ public function withTemplate(string $template): self
7676

7777
public function withId(string $id): self
7878
{
79-
if ($this->unchangeableId) {
80-
return $this;
81-
}
82-
8379
return $this->clone(id: $id);
8480
}
8581

86-
public function withUnchangeableId(string $id): self
82+
public function withPath(string|int $path): self
8783
{
88-
return $this->clone(id: $id, unchangeableId: true);
84+
return $this->clone(path: $path);
8985
}
9086

91-
public function withPrefixedId(string $prefix): self
87+
public function withPrefix(string $prefix): self
9288
{
93-
if ($this->id === $this->name || $this->unchangeableId) {
89+
if ($this->id === $this->name || $this->path !== null) {
9490
return $this;
9591
}
9692

@@ -176,8 +172,8 @@ private function clone(
176172
?Mode $mode = null,
177173
?string $name = null,
178174
?string $id = null,
175+
string|int|null $path = null,
179176
?Result $subsequent = null,
180-
?bool $unchangeableId = null,
181177
?array $children = null
182178
): self {
183179
return new self(
@@ -189,8 +185,8 @@ private function clone(
189185
$mode ?? $this->mode,
190186
$name ?? $this->name,
191187
$id ?? $this->id,
188+
$path ?? $this->path,
192189
$subsequent ?? $this->subsequent,
193-
$unchangeableId ?? $this->unchangeableId,
194190
...($children ?? $this->children)
195191
);
196192
}

library/Rules/DateTimeDiff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private function enrichResult(string $now, mixed $input, Result $result): Result
105105
$template = $now === 'now' ? self::TEMPLATE_STANDARD : self::TEMPLATE_CUSTOMIZED;
106106

107107
return (new Result($result->isValid, $input, $this, $parameters, $template, id: $result->id))
108-
->withPrefixedId('dateTimeDiff')
108+
->withPrefix('dateTimeDiff')
109109
->withSubsequent($result->withNameIfMissing($name));
110110
}
111111

library/Rules/Each.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Respect\Validation\Result;
1515
use Respect\Validation\Rules\Core\FilteredNonEmptyArray;
1616

17-
use function array_map;
1817
use function array_reduce;
1918

2019
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
@@ -27,7 +26,10 @@ final class Each extends FilteredNonEmptyArray
2726
/** @param non-empty-array<mixed> $input */
2827
protected function evaluateNonEmptyArray(array $input): Result
2928
{
30-
$children = array_map(fn ($item) => $this->rule->evaluate($item), $input);
29+
$children = [];
30+
foreach ($input as $key => $value) {
31+
$children[] = $this->rule->evaluate($value)->withPath($key);
32+
}
3133
$isValid = array_reduce($children, static fn ($carry, $childResult) => $carry && $childResult->isValid, true);
3234
if ($isValid) {
3335
return Result::passed($input, $this)->withChildren(...$children);

library/Rules/Key.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function evaluate(mixed $input): Result
4141

4242
return $this->rule
4343
->evaluate($input[$this->key])
44-
->withUnchangeableId((string) $this->key)
44+
->withPath($this->key)
4545
->withNameIfMissing($this->rule->getName() ?? (string) $this->key);
4646
}
4747
}

library/Rules/KeyOptional.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function evaluate(mixed $input): Result
4141

4242
return $this->rule
4343
->evaluate($input[$this->key])
44-
->withUnchangeableId((string) $this->key)
44+
->withPath($this->key)
4545
->withNameIfMissing($this->rule->getName() ?? (string) $this->key);
4646
}
4747
}

library/Rules/Length.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private function enrichResult(mixed $input, Result $result): Result
5959
}
6060

6161
return (new Result($result->isValid, $input, $this, id: $result->id))
62-
->withPrefixedId('length')
62+
->withPrefix('length')
6363
->withSubsequent($result->withInput($input));
6464
}
6565

library/Rules/Max.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ final class Max extends FilteredNonEmptyArray
2626
/** @param non-empty-array<mixed> $input */
2727
protected function evaluateNonEmptyArray(array $input): Result
2828
{
29-
$result = $this->rule->evaluate(max($input))->withPrefixedId('max');
29+
$result = $this->rule->evaluate(max($input))->withPrefix('max');
3030
$template = $this->getName() === null ? self::TEMPLATE_STANDARD : self::TEMPLATE_NAMED;
3131

32-
return (new Result($result->isValid, $input, $this, [], $template, id: $result->id))->withSubsequent($result);
32+
return (new Result($result->isValid, $input, $this, [], $template, id: $result->id))
33+
->withSubsequent($result);
3334
}
3435
}

library/Rules/Min.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Min extends FilteredNonEmptyArray
2626
/** @param non-empty-array<mixed> $input */
2727
protected function evaluateNonEmptyArray(array $input): Result
2828
{
29-
$result = $this->rule->evaluate(min($input))->withPrefixedId('min');
29+
$result = $this->rule->evaluate(min($input))->withPrefix('min');
3030
$template = $this->getName() === null ? self::TEMPLATE_STANDARD : self::TEMPLATE_NAMED;
3131

3232
return (new Result($result->isValid, $input, $this, [], $template, id: $result->id))->withSubsequent($result);

library/Rules/Not.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ final class Not extends Wrapper
1818
{
1919
public function evaluate(mixed $input): Result
2020
{
21-
return $this->rule->evaluate($input)->withInvertedMode()->withPrefixedId('not');
21+
return $this->rule->evaluate($input)->withInvertedMode()->withPrefix('not');
2222
}
2323
}

0 commit comments

Comments
 (0)