Skip to content

Commit 904ddb5

Browse files
committed
PATH
Signed-off-by: Henrique Moody <[email protected]>
1 parent 0b221f1 commit 904ddb5

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

library/Message/StandardFormatter.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public function main(Result $result, array $templates, Translator $translator):
4242
$selectedTemplates = $this->selectTemplates($result, $templates);
4343
if (!$this->isFinalTemplate($result, $selectedTemplates)) {
4444
foreach ($this->extractDeduplicatedChildren($result) as $child) {
45+
if ($result->path !== null && $child->path !== null) {
46+
$child = $child->withPath(sprintf('%s.%s', $result->path, $child->path));
47+
}
4548
return $this->main($child, $selectedTemplates, $translator);
4649
}
4750
}
@@ -99,35 +102,46 @@ public function array(Result $result, array $templates, Translator $translator):
99102
{
100103
$selectedTemplates = $this->selectTemplates($result, $templates);
101104
$messages = [
102-
'__root' => $this->renderer->render($this->getTemplated($result, $selectedTemplates), $translator),
105+
'_self' => [
106+
$result->id => $this->renderer->render($this->getTemplated($result, $selectedTemplates), $translator)
107+
],
108+
'_children' => []
103109
];
104-
105-
$children = [];
106110
foreach ($this->extractDeduplicatedChildren($result) as $child) {
111+
112+
if ($child->path === null) {
113+
$messages['_self'][$child->id] = $this->renderer->render($this->getTemplated($child, $selectedTemplates), $translator);
114+
continue;
115+
}
107116
$childKey = $child->path ?? '__' . $child->id;
108117

109-
$children[$childKey] = $this->array(
118+
$messages['_children'][$childKey] = $this->array(
110119
$child,
111120
$this->selectTemplates($child, $selectedTemplates),
112121
$translator
113122
);
114123

115-
if (count($children[$childKey]) !== 1) {
124+
if (count($messages['_children'][$childKey]) !== 1) {
116125
continue;
117126
}
118127

119-
$children[$childKey] = current($children[$childKey]);
128+
$messages['_children'][$childKey] = current($messages['_children'][$childKey]);
120129
}
121130

122-
if (count($children) === 0 || $this->isFinalTemplate($result, $selectedTemplates)) {
123-
return [$result->path ?? '__' . $result->id => $messages['__root']];
131+
// if (count($messages['_children']) === 0 || $this->isFinalTemplate($result, $selectedTemplates)) {
132+
// return [$result->path ?? '__' . $result->id => $messages['__root']];
133+
// }
134+
//
135+
136+
if ($messages['_children'] === []) {
137+
unset($messages['_children']);
124138
}
125139

126140
if ($result->path !== null) {
127-
return [$result->path => $messages + $children];
141+
return [$result->path => $messages];
128142
}
129143

130-
return $messages + $children;
144+
return $messages;// + $messages['_children'];
131145
}
132146

133147
private function isAlwaysVisible(Result $result, Result ...$siblings): bool
@@ -241,12 +255,18 @@ private function extractDeduplicatedChildren(Result $result): array
241255
if (isset($duplicateCounters[$id])) {
242256
$id .= '.' . ++$duplicateCounters[$id];
243257
} elseif (array_key_exists($id, $deduplicatedResults)) {
244-
$deduplicatedResults[$id . '.1'] = $deduplicatedResults[$id]?->withId($id . '.1');
258+
$deduplicatedResults[$id . '.1'] = $child->path
259+
? $deduplicatedResults[$id]?->withPath($id . '.1')
260+
: $deduplicatedResults[$id]?->withId($id . '.1');
245261
unset($deduplicatedResults[$id]);
246262
$duplicateCounters[$id] = 2;
247263
$id .= '.2';
248264
}
249-
$deduplicatedResults[$id] = $child->isValid ? null : $child->withId((string) $id);
265+
$deduplicatedResults[$id] = $child->isValid ? null : (
266+
$child->path
267+
? $child->withPath((string) $id)
268+
: $child->withId((string) $id)
269+
);
250270
}
251271

252272
return array_values(array_filter($deduplicatedResults));

library/Message/StandardRenderer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace Respect\Validation\Message;
1111

1212
use ReflectionClass;
13+
use Respect\Stringifier\Quoter;
14+
use Respect\Stringifier\Quoters\StandardQuoter;
1315
use Respect\Stringifier\Stringifier;
1416
use Respect\Stringifier\Stringifiers\CompositeStringifier;
1517
use Respect\Validation\Mode;
@@ -29,15 +31,20 @@ final class StandardRenderer implements Renderer
2931

3032
private readonly Stringifier $stringifier;
3133

32-
public function __construct(?Stringifier $stringifier = null)
34+
public function __construct(
35+
?Stringifier $stringifier = null,
36+
private readonly Quoter $quoter = new StandardQuoter(120)
37+
)
3338
{
3439
$this->stringifier = $stringifier ?? CompositeStringifier::createDefault();
3540
}
3641

3742
public function render(Result $result, Translator $translator, ?string $template = null): string
3843
{
3944
$parameters = $result->parameters;
40-
$parameters['name'] ??= $result->name ?? $this->placeholder('input', $result->input, $translator);
45+
$parameters['name'] ??= $result->name
46+
?? ($result->path ? $this->quoter->quote($result->path, 0) : null)
47+
?? $this->placeholder('input', $result->input, $translator);
4148
$parameters['input'] = $result->input;
4249

4350
$rendered = (string) preg_replace_callback(

library/Rules/Key.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
private readonly int|string $key,
2424
Rule $rule,
2525
) {
26-
$rule->setName($rule->getName() ?? (string) $key);
26+
// $rule->setName($rule->getName() ?? (string) $key);
2727
parent::__construct($rule);
2828
}
2929

@@ -41,7 +41,6 @@ public function evaluate(mixed $input): Result
4141

4242
return $this->rule
4343
->evaluate($input[$this->key])
44-
->withPath($this->key)
45-
->withNameIfMissing($this->rule->getName() ?? (string) $this->key);
44+
->withPath($this->key);
4645
}
4746
}

library/Rules/KeyExists.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getKey(): int|string
3838

3939
public function evaluate(mixed $input): Result
4040
{
41-
return new Result($this->hasKey($input), $input, $this, name: (string) $this->key, path: $this->key);
41+
return new Result($this->hasKey($input), $input, $this, path: $this->key);
4242
}
4343

4444
private function hasKey(mixed $input): bool

library/Rules/KeyOptional.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function __construct(
2323
private readonly int|string $key,
2424
Rule $rule,
2525
) {
26-
$rule->setName($rule->getName() ?? (string) $key);
2726
parent::__construct($rule);
2827
}
2928

@@ -41,7 +40,6 @@ public function evaluate(mixed $input): Result
4140

4241
return $this->rule
4342
->evaluate($input[$this->key])
44-
->withPath($this->key)
45-
->withNameIfMissing($this->rule->getName() ?? (string) $this->key);
43+
->withPath($this->key);
4644
}
4745
}

0 commit comments

Comments
 (0)