Skip to content

Commit

Permalink
v4
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemoody committed Dec 27, 2024
1 parent a8f0066 commit 341730d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
11 changes: 8 additions & 3 deletions library/Message/StandardFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,21 @@ private function extractDeduplicatedChildren(Result $result): array
$deduplicatedResults = [];
$duplicateCounters = [];
foreach ($result->children as $child) {
$id = $child->path ?? $child->id;
if ($child->path !== null) {
$deduplicatedResults[$child->path] = $child->isValid ? null : $child;
continue;
}

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

return array_values(array_filter($deduplicatedResults));
Expand Down
4 changes: 4 additions & 0 deletions library/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public function withPath(string|int $path): self
return $this->clone(
adjacent: $this->adjacent?->withPath($path),
path: $this->path === null ? $path : $path . '.' . $this->path,
// children: array_map(
// static fn (Result $child) => $child->path === null ? $child->withPath($child->name ?? $path) : $child,
// $this->children
// ),
);
}

Expand Down
42 changes: 21 additions & 21 deletions tests/feature/Rules/EachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,52 +105,52 @@

test('With wrapper name, default', expectAll(
fn() => v::each(v::intType())->setName('Wrapper')->assert(['a', 'b', 'c']),
'Wrapper must be an integer',
'`.0` must be an integer',
<<<'FULL_MESSAGE'
- Each item in Wrapper must be valid
- Wrapper must be an integer
- Wrapper must be an integer
- Wrapper must be an integer
- `.0` must be an integer
- `.1` must be an integer
- `.2` must be an integer
FULL_MESSAGE,
[
'__root__' => 'Each item in Wrapper must be valid',
0 => 'Wrapper must be an integer',
1 => 'Wrapper must be an integer',
2 => 'Wrapper must be an integer',
0 => '`.0` must be an integer',
1 => '`.1` must be an integer',
2 => '`.2` must be an integer',
],
));

test('With wrapper name, inverted', expectAll(
fn() => v::not(v::each(v::intType())->setName('Wrapper'))->setName('Not')->assert([1, 2, 3]),
'Wrapper must not be an integer',
'`.0` must not be an integer',
<<<'FULL_MESSAGE'
- Each item in Wrapper must be invalid
- Wrapper must not be an integer
- Wrapper must not be an integer
- Wrapper must not be an integer
- `.0` must not be an integer
- `.1` must not be an integer
- `.2` must not be an integer
FULL_MESSAGE,
[
'__root__' => 'Each item in Wrapper must be invalid',
0 => 'Wrapper must not be an integer',
1 => 'Wrapper must not be an integer',
2 => 'Wrapper must not be an integer',
0 => '`.0` must not be an integer',
1 => '`.1` must not be an integer',
2 => '`.2` must not be an integer',
],
));

test('With Not name, inverted', expectAll(
fn() => v::not(v::each(v::intType()))->setName('Not')->assert([1, 2, 3]),
'Not must not be an integer',
'`.0` must not be an integer',
<<<'FULL_MESSAGE'
- Each item in Not must be invalid
- Not must not be an integer
- Not must not be an integer
- Not must not be an integer
- `.0` must not be an integer
- `.1` must not be an integer
- `.2` must not be an integer
FULL_MESSAGE,
[
'__root__' => 'Each item in Not must be invalid',
0 => 'Not must not be an integer',
1 => 'Not must not be an integer',
2 => 'Not must not be an integer',
0 => '`.0` must not be an integer',
1 => '`.1` must not be an integer',
2 => '`.2` must not be an integer',
],
));

Expand Down

0 comments on commit 341730d

Please sign in to comment.