Skip to content

Commit 89b9d8e

Browse files
committed
v2
1 parent ae48959 commit 89b9d8e

File tree

8 files changed

+45
-34
lines changed

8 files changed

+45
-34
lines changed

library/Message/StandardFormatter.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414

1515
use function array_filter;
1616
use function array_key_exists;
17+
use function array_map;
1718
use function array_reduce;
1819
use function array_values;
1920
use function count;
2021
use function current;
22+
use function end;
23+
use function explode;
2124
use function is_array;
2225
use function is_string;
2326
use function Respect\Stringifier\stringify;
@@ -42,11 +45,7 @@ public function main(Result $result, array $templates, Translator $translator):
4245
$selectedTemplates = $this->selectTemplates($result, $templates);
4346
if (!$this->isFinalTemplate($result, $selectedTemplates)) {
4447
foreach ($this->extractDeduplicatedChildren($result) as $child) {
45-
if ($result->path !== null && $child->path !== null && $child->path !== $result->path) {
46-
$child = $child->withPath($result->path);
47-
} elseif ($result->path !== null && $child->path === null) {
48-
$child = $child->withPath($result->path);
49-
}
48+
$child = $this->resultWithPath($result, $child);
5049

5150
return $this->main($child, $selectedTemplates, $translator);
5251
}
@@ -63,32 +62,38 @@ public function full(
6362
array $templates,
6463
Translator $translator,
6564
int $depth = 0,
66-
?Result $parent = null,
6765
Result ...$siblings
6866
): string {
6967
$selectedTemplates = $this->selectTemplates($result, $templates);
7068
$isFinalTemplate = $this->isFinalTemplate($result, $selectedTemplates);
7169

7270
$rendered = '';
7371
if ($this->isAlwaysVisible($result, ...$siblings) || $isFinalTemplate) {
72+
$resultToRender = $result;
73+
if ($depth > 0 && $result->path !== null) {
74+
$paths = explode('.', (string) $result->path);
75+
$resultToRender->path = end($paths);
76+
}
7477
$indentation = str_repeat(' ', $depth * 2);
7578
$rendered .= sprintf(
7679
'%s- %s' . PHP_EOL,
7780
$indentation,
78-
$this->renderer->render($this->getTemplated($result, $selectedTemplates), $translator),
81+
$this->renderer->render($this->getTemplated($resultToRender, $selectedTemplates), $translator),
7982
);
8083
$depth++;
8184
}
8285

8386
if (!$isFinalTemplate) {
84-
$results = $this->extractDeduplicatedChildren($result);
87+
$results = array_map(
88+
fn(Result $child) => $this->resultWithPath($result, $child),
89+
$this->extractDeduplicatedChildren($result)
90+
);
8591
foreach ($results as $child) {
8692
$rendered .= $this->full(
8793
$child,
8894
$selectedTemplates,
8995
$translator,
9096
$depth,
91-
$result,
9297
...array_filter($results, static fn (Result $sibling) => $sibling !== $child)
9398
);
9499
$rendered .= PHP_EOL;
@@ -142,6 +147,19 @@ public function array(Result $result, array $templates, Translator $translator):
142147
return $messages;
143148
}
144149

150+
public function resultWithPath(Result $parent, Result $child): Result
151+
{
152+
if ($parent->path !== null && $child->path !== null && $child->path !== $parent->path) {
153+
return $child->withPath($parent->path);
154+
}
155+
156+
if ($parent->path !== null && $child->path === null) {
157+
return $child->withPath($parent->path);
158+
}
159+
160+
return $child;
161+
}
162+
145163
private function isAlwaysVisible(Result $result, Result ...$siblings): bool
146164
{
147165
if ($result->isValid) {

library/Result.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Respect\Validation;
1111

1212
use Respect\Validation\Rules\Core\Nameable;
13-
use Respect\Validation\Rules\Core\Renameable;
1413

1514
use function array_filter;
1615
use function array_map;
@@ -39,7 +38,7 @@ public function __construct(
3938
public readonly ?string $name = null,
4039
?string $id = null,
4140
public readonly ?Result $adjacent = null,
42-
public readonly string|int|null $path = null,
41+
public string|int|null $path = null,
4342
Result ...$children,
4443
) {
4544
$this->id = $id ?? lcfirst(substr((string) strrchr($rule::class, '\\'), 1));
@@ -119,10 +118,6 @@ public function withPath(string|int $path): self
119118
return $this->clone(
120119
adjacent: $this->adjacent?->withPath($path),
121120
path: $this->path === null ? $path : $path . '.' . $this->path,
122-
// children: array_map(
123-
// static fn (Result $child) => $child->path === null ? $child->withPath($child->name ?? $path) : $child,
124-
// $this->children
125-
// ),
126121
);
127122
}
128123

library/Rules/KeyExists.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Respect\Validation\Message\Template;
1515
use Respect\Validation\Result;
1616
use Respect\Validation\Rules\Core\KeyRelated;
17-
use Respect\Validation\Rules\Core\Renameable;
1817
use Respect\Validation\Rules\Core\Standard;
1918

2019
use function array_key_exists;

library/Rules/PropertyExists.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use ReflectionObject;
1414
use Respect\Validation\Message\Template;
1515
use Respect\Validation\Result;
16-
use Respect\Validation\Rules\Core\Renameable;
1716
use Respect\Validation\Rules\Core\Standard;
1817

1918
use function is_object;

tests/feature/Issues/Issue1289Test.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@
4949
<<<'FULL_MESSAGE'
5050
- `.0` must pass the rules
5151
- `.default` must pass one of the rules
52-
- 2 must be a string
53-
- 2 must be a boolean
52+
- `.default` must be a string
53+
- `.default` must be a boolean
5454
- `.description` must be a string value
5555
FULL_MESSAGE,
5656
[
5757
0 => [
5858
'__root__' => '`.0` must pass the rules',
5959
'default' => [
6060
'__root__' => '`.default` must pass one of the rules',
61-
'stringType' => '2 must be a string',
62-
'boolType' => '2 must be a boolean',
61+
'stringType' => '`.default` be a string',
62+
'boolType' => '`.default` be a boolean',
6363
],
6464
'description' => '`.description` must be a string value',
6565
],

tests/feature/Issues/Issue1334Test.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ function (): void {
2828
- `.0` must pass the rules
2929
- `.street` must be present
3030
- `.other` must pass the rules
31-
- 123 must be a string or must be null
31+
- `.other` must be a string or must be null
3232
- `.1` must pass the rules
33-
- "" must not be empty
33+
- `.street` must not be empty
3434
- `.2` must pass the rules
35-
- 123 must be a string
35+
- `.street` must be a string
3636
FULL_MESSAGE,
3737
[
3838
'each' => [
3939
'__root__' => 'Each item in `[["region": "Oregon", "country": "USA", "other": 123], ["street": "", "region": "Oregon", "country": "USA"], ["s ... ]` must be valid',
4040
0 => [
4141
'__root__' => '`.0` must pass the rules',
4242
'street' => '`.street` must be present',
43-
'other' => '123 must be a string or must be null',
43+
'other' => '`.other` must be a string or must be null',
4444
],
45-
1 => '"" must not be empty',
46-
2 => '123 must be a string',
45+
1 => '`.street` must not be empty',
46+
2 => '`.street` must be a string',
4747
],
4848
],
4949
));

tests/feature/Issues/Issue1376Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
- `.title` must be present
2121
- `.description` must be present
2222
- `.author` must pass all the rules
23-
- "foo" must be an integer
24-
- The length of "foo" must be between 1 and 2
23+
- `.author` must be an integer
24+
- The length of `.author` must be between 1 and 2
2525
- `.user` must be present
2626
FULL_MESSAGE,
2727
[

tests/feature/Rules/EachTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@
282282
FULL_MESSAGE,
283283
[
284284
'__root__' => 'Each item in `[["not_int": "wrong"], ["my_int": 2], "not an array"]` must be valid',
285-
0 => 'my_int must be present',
286-
1 => 'my_int must be an odd number',
285+
0 => '`.my_int` must be present',
286+
1 => '`.my_int` must be an odd number',
287287
2 => [
288-
'__root__' => '"not an array" must pass all the rules',
289-
'arrayType' => '"not an array" must be an array',
290-
'my_int' => 'my_int must be present',
288+
'__root__' => '`.2` must pass all the rules',
289+
'arrayType' => '`.2` must be an array',
290+
'my_int' => '`.my_int` must be present',
291291
],
292292
],
293293
));

0 commit comments

Comments
 (0)