From 904ddb57e6b7cd684bdd07eb7d4eb1db0013cd18 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Sat, 21 Dec 2024 14:14:41 +0100 Subject: [PATCH] PATH Signed-off-by: Henrique Moody --- library/Message/StandardFormatter.php | 44 +++++++++++++++++++-------- library/Message/StandardRenderer.php | 11 +++++-- library/Rules/Key.php | 5 ++- library/Rules/KeyExists.php | 2 +- library/Rules/KeyOptional.php | 4 +-- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/library/Message/StandardFormatter.php b/library/Message/StandardFormatter.php index 9c8392109..867040ef1 100644 --- a/library/Message/StandardFormatter.php +++ b/library/Message/StandardFormatter.php @@ -42,6 +42,9 @@ public function main(Result $result, array $templates, Translator $translator): $selectedTemplates = $this->selectTemplates($result, $templates); if (!$this->isFinalTemplate($result, $selectedTemplates)) { foreach ($this->extractDeduplicatedChildren($result) as $child) { + if ($result->path !== null && $child->path !== null) { + $child = $child->withPath(sprintf('%s.%s', $result->path, $child->path)); + } return $this->main($child, $selectedTemplates, $translator); } } @@ -99,35 +102,46 @@ public function array(Result $result, array $templates, Translator $translator): { $selectedTemplates = $this->selectTemplates($result, $templates); $messages = [ - '__root' => $this->renderer->render($this->getTemplated($result, $selectedTemplates), $translator), + '_self' => [ + $result->id => $this->renderer->render($this->getTemplated($result, $selectedTemplates), $translator) + ], + '_children' => [] ]; - - $children = []; foreach ($this->extractDeduplicatedChildren($result) as $child) { + + if ($child->path === null) { + $messages['_self'][$child->id] = $this->renderer->render($this->getTemplated($child, $selectedTemplates), $translator); + continue; + } $childKey = $child->path ?? '__' . $child->id; - $children[$childKey] = $this->array( + $messages['_children'][$childKey] = $this->array( $child, $this->selectTemplates($child, $selectedTemplates), $translator ); - if (count($children[$childKey]) !== 1) { + if (count($messages['_children'][$childKey]) !== 1) { continue; } - $children[$childKey] = current($children[$childKey]); + $messages['_children'][$childKey] = current($messages['_children'][$childKey]); } - if (count($children) === 0 || $this->isFinalTemplate($result, $selectedTemplates)) { - return [$result->path ?? '__' . $result->id => $messages['__root']]; +// if (count($messages['_children']) === 0 || $this->isFinalTemplate($result, $selectedTemplates)) { +// return [$result->path ?? '__' . $result->id => $messages['__root']]; +// } +// + + if ($messages['_children'] === []) { + unset($messages['_children']); } if ($result->path !== null) { - return [$result->path => $messages + $children]; + return [$result->path => $messages]; } - return $messages + $children; + return $messages;// + $messages['_children']; } private function isAlwaysVisible(Result $result, Result ...$siblings): bool @@ -241,12 +255,18 @@ private function extractDeduplicatedChildren(Result $result): array if (isset($duplicateCounters[$id])) { $id .= '.' . ++$duplicateCounters[$id]; } elseif (array_key_exists($id, $deduplicatedResults)) { - $deduplicatedResults[$id . '.1'] = $deduplicatedResults[$id]?->withId($id . '.1'); + $deduplicatedResults[$id . '.1'] = $child->path + ? $deduplicatedResults[$id]?->withPath($id . '.1') + : $deduplicatedResults[$id]?->withId($id . '.1'); unset($deduplicatedResults[$id]); $duplicateCounters[$id] = 2; $id .= '.2'; } - $deduplicatedResults[$id] = $child->isValid ? null : $child->withId((string) $id); + $deduplicatedResults[$id] = $child->isValid ? null : ( + $child->path + ? $child->withPath((string) $id) + : $child->withId((string) $id) + ); } return array_values(array_filter($deduplicatedResults)); diff --git a/library/Message/StandardRenderer.php b/library/Message/StandardRenderer.php index 5bee117b0..f3cda1ff8 100644 --- a/library/Message/StandardRenderer.php +++ b/library/Message/StandardRenderer.php @@ -10,6 +10,8 @@ namespace Respect\Validation\Message; use ReflectionClass; +use Respect\Stringifier\Quoter; +use Respect\Stringifier\Quoters\StandardQuoter; use Respect\Stringifier\Stringifier; use Respect\Stringifier\Stringifiers\CompositeStringifier; use Respect\Validation\Mode; @@ -29,7 +31,10 @@ final class StandardRenderer implements Renderer private readonly Stringifier $stringifier; - public function __construct(?Stringifier $stringifier = null) + public function __construct( + ?Stringifier $stringifier = null, + private readonly Quoter $quoter = new StandardQuoter(120) + ) { $this->stringifier = $stringifier ?? CompositeStringifier::createDefault(); } @@ -37,7 +42,9 @@ public function __construct(?Stringifier $stringifier = null) public function render(Result $result, Translator $translator, ?string $template = null): string { $parameters = $result->parameters; - $parameters['name'] ??= $result->name ?? $this->placeholder('input', $result->input, $translator); + $parameters['name'] ??= $result->name + ?? ($result->path ? $this->quoter->quote($result->path, 0) : null) + ?? $this->placeholder('input', $result->input, $translator); $parameters['input'] = $result->input; $rendered = (string) preg_replace_callback( diff --git a/library/Rules/Key.php b/library/Rules/Key.php index e30b85e4f..0d2b328ef 100644 --- a/library/Rules/Key.php +++ b/library/Rules/Key.php @@ -23,7 +23,7 @@ public function __construct( private readonly int|string $key, Rule $rule, ) { - $rule->setName($rule->getName() ?? (string) $key); +// $rule->setName($rule->getName() ?? (string) $key); parent::__construct($rule); } @@ -41,7 +41,6 @@ public function evaluate(mixed $input): Result return $this->rule ->evaluate($input[$this->key]) - ->withPath($this->key) - ->withNameIfMissing($this->rule->getName() ?? (string) $this->key); + ->withPath($this->key); } } diff --git a/library/Rules/KeyExists.php b/library/Rules/KeyExists.php index 2efa7abc5..46d5e1164 100644 --- a/library/Rules/KeyExists.php +++ b/library/Rules/KeyExists.php @@ -38,7 +38,7 @@ public function getKey(): int|string public function evaluate(mixed $input): Result { - return new Result($this->hasKey($input), $input, $this, name: (string) $this->key, path: $this->key); + return new Result($this->hasKey($input), $input, $this, path: $this->key); } private function hasKey(mixed $input): bool diff --git a/library/Rules/KeyOptional.php b/library/Rules/KeyOptional.php index 4284aba0b..904f0c19b 100644 --- a/library/Rules/KeyOptional.php +++ b/library/Rules/KeyOptional.php @@ -23,7 +23,6 @@ public function __construct( private readonly int|string $key, Rule $rule, ) { - $rule->setName($rule->getName() ?? (string) $key); parent::__construct($rule); } @@ -41,7 +40,6 @@ public function evaluate(mixed $input): Result return $this->rule ->evaluate($input[$this->key]) - ->withPath($this->key) - ->withNameIfMissing($this->rule->getName() ?? (string) $this->key); + ->withPath($this->key); } }