Skip to content

Commit

Permalink
PATH
Browse files Browse the repository at this point in the history
Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Dec 21, 2024
1 parent 0b221f1 commit 904ddb5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
44 changes: 32 additions & 12 deletions library/Message/StandardFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
11 changes: 9 additions & 2 deletions library/Message/StandardRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,15 +31,20 @@ 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();
}

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(
Expand Down
5 changes: 2 additions & 3 deletions library/Rules/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion library/Rules/KeyExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions library/Rules/KeyOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function __construct(
private readonly int|string $key,
Rule $rule,
) {
$rule->setName($rule->getName() ?? (string) $key);
parent::__construct($rule);
}

Expand All @@ -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);
}
}

0 comments on commit 904ddb5

Please sign in to comment.