Skip to content

Trace paths instead of IDs #1532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions library/FailedResultIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

namespace Respect\Validation;

use Countable;
use Iterator;
use RecursiveIterator;

use function array_filter;
use function array_key_exists;
use function array_map;
use function array_values;
use function count;
use function current;
use function key;
use function next;
use function reset;

/**
* @implements Iterator<int, Result>
*/
final class FailedResultIterator implements Iterator, Countable, RecursiveIterator
{
private array $children;

public function __construct(
private readonly Result $result,
) {
$this->children = $this->extractDeduplicatedChildren();
}

public function extractDeduplicatedChildren(): array
{
/** @var array<string, Result> $deduplicatedResults */
$deduplicatedResults = [];
$duplicateCounters = [];
foreach ($this->result->children as $child) {
if ($child->path !== null) {
$deduplicatedResults[$child->path->value] = $child->isValid ? null : $child;
continue;
}

$id = $child->id;
if (isset($duplicateCounters[$id])) {
$id .= '.' . ++$duplicateCounters[$id];
} elseif (array_key_exists($id, $deduplicatedResults)) {
$deduplicatedResults[$id . '.1'] = $deduplicatedResults[$id]?->withId($id . '.1');
unset($deduplicatedResults[$id]);
$duplicateCounters[$id] = 2;
$id .= '.2';
}

$deduplicatedResults[$id] = $child->isValid ? null : $child->withId($id);
}

return array_map(
function (Result $child): Result {
if ($this->result->path !== null && $child->path !== null && $child->path->isEqual($this->result->path)) {
return $child->withPath($this->result->path);
}

if ($this->result->path !== null && $child->path === null) {
return $child->withPath($this->result->path);
}

return $child;
},
array_values(array_filter($deduplicatedResults))
);
}

public function current(): Result|false
{
return current($this->children);
}

public function getArrayCopy(): array
{
return $this->children;
}

public function next(): void
{
next($this->children);
}

public function key(): ?int
{
return key($this->children);
}

public function valid(): bool
{
return key($this->children) !== null;
}

public function rewind(): void
{
reset($this->children);
}

public function count(): int
{
return count($this->children);
}

public function hasChildren(): bool
{
return $this->result->children !== [];
}

public function getChildren(): ?self
{
if (!$this->hasChildren()) {
return null;
}

return new self($this->result);
}
}
35 changes: 35 additions & 0 deletions library/Message/Placeholder/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Validation\Message\Placeholder;

final class Path
{
public function __construct(
public readonly int|string $value,
public readonly ?Path $child = null
) {
}

public function withParent(int|string $value): self
{
return new self($value, $this);
}

public function isEqual(Path $path): bool
{
return $this->value === $path->value
&& $this->child?->isEqual($path->child) ?? true;
}

public function getDeepest(): Path
{
return $this->child?->getDeepest() ?? $this;
}
}
57 changes: 12 additions & 45 deletions library/Message/StandardFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
namespace Respect\Validation\Message;

use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\FailedResultIterator;
use Respect\Validation\Result;

use function array_filter;
use function array_key_exists;
use function array_map;
use function array_reduce;
use function array_values;
use function count;
use function current;
use function is_array;
Expand All @@ -42,8 +40,8 @@ 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) {
return $this->main($this->resultWithPath($result, $child), $selectedTemplates, $translator);
foreach (new FailedResultIterator($result) as $child) {
return $this->main($child, $selectedTemplates, $translator);
}
}

Expand Down Expand Up @@ -78,17 +76,14 @@ public function full(
}

if (!$isFinalTemplate) {
$results = array_map(
fn(Result $child) => $this->resultWithPath($result, $child),
$this->extractDeduplicatedChildren($result)
);
$results = new FailedResultIterator($result);
foreach ($results as $child) {
$rendered .= $this->full(
$child,
$selectedTemplates,
$translator,
$depth,
...array_filter($results, static fn (Result $sibling) => $sibling !== $child)
...array_filter($results->getArrayCopy(), static fn (Result $sibling) => $sibling !== $child)
);
$rendered .= PHP_EOL;
}
Expand All @@ -105,10 +100,10 @@ public function full(
public function array(Result $result, array $templates, Translator $translator): array
{
$selectedTemplates = $this->selectTemplates($result, $templates);
$deduplicatedChildren = $this->extractDeduplicatedChildren($result);
$deduplicatedChildren = new FailedResultIterator($result);
if (count($deduplicatedChildren) === 0 || $this->isFinalTemplate($result, $selectedTemplates)) {
return [
$result->getDeepestPath() ?? $result->id => $this->renderer->render(
$result->path?->getDeepest()->value ?? $result->id => $this->renderer->render(
$this->getTemplated($result->withDeepestPath(), $selectedTemplates),
$translator
),
Expand All @@ -117,7 +112,7 @@ public function array(Result $result, array $templates, Translator $translator):

$messages = [];
foreach ($deduplicatedChildren as $child) {
$key = $child->getDeepestPath() ?? $child->id;
$key = $child?->path?->getDeepest()->value ?? $child->id;
$messages[$key] = $this->array(
$this->resultWithPath($result, $child),
$this->selectTemplates($child, $selectedTemplates),
Expand All @@ -144,7 +139,7 @@ public function array(Result $result, array $templates, Translator $translator):
return $messages;
}

public function resultWithPath(Result $parent, Result $child): Result
private function resultWithPath(Result $parent, Result $child): Result
{
if ($parent->path !== null && $child->path !== null && $child->path !== $parent->path) {
return $child->withPath($parent->path);
Expand Down Expand Up @@ -199,7 +194,7 @@ private function getTemplated(Result $result, array $templates): Result
return $result;
}

foreach ([$result->path, $result->name, $result->id, '__root__'] as $key) {
foreach ([$result->path?->value, $result->name, $result->id, '__root__'] as $key) {
if (!isset($templates[$key])) {
continue;
}
Expand All @@ -221,7 +216,7 @@ private function getTemplated(Result $result, array $templates): Result
*/
private function isFinalTemplate(Result $result, array $templates): bool
{
$keys = [$result->path, $result->name, $result->id];
$keys = [$result->path?->value, $result->name, $result->id];
foreach ($keys as $key) {
if (isset($templates[$key]) && is_string($templates[$key])) {
return true;
Expand All @@ -248,40 +243,12 @@ private function isFinalTemplate(Result $result, array $templates): bool
*/
private function selectTemplates(Result $result, array $templates): array
{
foreach ([$result->path, $result->name, $result->id] as $key) {
foreach ([$result->path?->value, $result->name, $result->id] as $key) {
if (isset($templates[$key]) && is_array($templates[$key])) {
return $templates[$key];
}
}

return $templates;
}

/** @return array<Result> */
private function extractDeduplicatedChildren(Result $result): array
{
/** @var array<string, Result> $deduplicatedResults */
$deduplicatedResults = [];
$duplicateCounters = [];
foreach ($result->children as $child) {
$id = $child->getDeepestPath() ?? $child->id;
if (isset($duplicateCounters[$id])) {
$id .= '.' . ++$duplicateCounters[$id];
} elseif (array_key_exists($id, $deduplicatedResults)) {
$deduplicatedResults[$id . '.1'] = $deduplicatedResults[$id]?->withId($id . '.1');
unset($deduplicatedResults[$id]);
$duplicateCounters[$id] = 2;
$id .= '.2';
}

if ($child->path === null) {
$deduplicatedResults[$id] = $child->isValid ? null : $child->withId($id);
continue;
}

$deduplicatedResults[$id] = $child->isValid ? null : $child;
}

return array_values(array_filter($deduplicatedResults));
}
}
3 changes: 2 additions & 1 deletion library/Message/StandardRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ReflectionClass;
use Respect\Stringifier\Stringifier;
use Respect\Validation\Message\Placeholder\Listed;
use Respect\Validation\Message\Placeholder\Path;
use Respect\Validation\Message\Placeholder\Quoted;
use Respect\Validation\Result;
use Respect\Validation\Rule;
Expand All @@ -36,7 +37,7 @@ public function __construct(
public function render(Result $result, Translator $translator, ?string $template = null): string
{
$parameters = $result->parameters;
$parameters['path'] = $result->path !== null ? Quoted::fromPath($result->path) : null;
$parameters['path'] = $result->path;
$parameters['input'] = $result->input;

$builtName = $result->name ?? $parameters['path'] ?? $this->placeholder('input', $result->input, $translator);
Expand Down
2 changes: 2 additions & 0 deletions library/Message/StandardStringifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Respect\Stringifier\Stringifiers\StringableObjectStringifier;
use Respect\Stringifier\Stringifiers\ThrowableObjectStringifier;
use Respect\Validation\Message\Stringifier\ListedStringifier;
use Respect\Validation\Message\Stringifier\PathStringifier;
use Respect\Validation\Message\Stringifier\QuotedStringifier;

final class StandardStringifier implements Stringifier
Expand Down Expand Up @@ -88,6 +89,7 @@ private function createStringifier(Quoter $quoter): Stringifier
$stringifier->prependStringifier(new ThrowableObjectStringifier($jsonEncodableStringifier, $quoter));
$stringifier->prependStringifier(new DateTimeStringifier($quoter, DateTimeInterface::ATOM));
$stringifier->prependStringifier(new IteratorObjectStringifier($stringifier, $quoter));
$stringifier->prependStringifier(new PathStringifier($quoter));
$stringifier->prependStringifier(new QuotedStringifier($quoter));
$stringifier->prependStringifier(new ListedStringifier($stringifier));

Expand Down
37 changes: 37 additions & 0 deletions library/Message/Stringifier/PathStringifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

namespace Respect\Validation\Message\Stringifier;

use Respect\Stringifier\Quoter;
use Respect\Stringifier\Stringifier;
use Respect\Validation\Message\Placeholder\Path;

final class PathStringifier implements Stringifier
{
public function __construct(
private readonly Quoter $quoter
) {
}

public function stringify(mixed $raw, int $depth): ?string
{
if (!$raw instanceof Path) {
return null;
}

$path = $raw;
$string = $path->value;
while ($path = $path->child) {
$string .= '.' . $path->value;
}

return $this->quoter->quote('.' . $string, $depth);
}
}
Loading
Loading