Skip to content

Commit

Permalink
Do not hide messages on EachException
Browse files Browse the repository at this point in the history
  • Loading branch information
alganet committed Feb 19, 2023
1 parent 5fe4b96 commit bae314d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 36 deletions.
36 changes: 36 additions & 0 deletions library/Exceptions/EachException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace Respect\Validation\Exceptions;

use function count;
use function current;

/**
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
Expand All @@ -27,4 +30,37 @@ final class EachException extends NestedValidationException
self::STANDARD => 'Each item in {{name}} must not validate',
],
];

/**
* {@inheritDoc}
*
* @todo This method shares too much with the parent implementation
*/
public function getMessages(array $templates = []): array
{
$messages = [$this->getId() => $this->renderMessage($this, $templates)];
$count = -1;
foreach ($this->getChildren() as $exception) {
$count++;
$id = $exception->getId();
if (!$exception instanceof NestedValidationException) {
$messages[$id . '.' . $count] = $this->renderMessage(
$exception,
$this->findTemplates($templates, $this->getId())
);
continue;
}

$messages[$id . '.' . $count] = $exception->getMessages(
$this->findTemplates($templates, $id, $this->getId())
);
if (count($messages[$id . '.' . $count]) > 1) {
continue;
}

$messages[$id . '.' . $count] = current($messages[$exception->getId()]);
}

return $messages;
}
}
72 changes: 36 additions & 36 deletions library/Exceptions/NestedValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,44 +186,10 @@ public function getFullMessage(): string
return implode(PHP_EOL, $messages);
}

/**
* @return RecursiveIteratorIterator<RecursiveExceptionIterator>
*/
private function getRecursiveIterator(): RecursiveIteratorIterator
{
return new RecursiveIteratorIterator(
new RecursiveExceptionIterator($this),
RecursiveIteratorIterator::SELF_FIRST
);
}

private function isOmissible(Exception $exception): bool
{
if (!$exception instanceof self) {
return false;
}

if (count($exception->getChildren()) !== 1) {
return false;
}

/** @var ValidationException $childException */
$childException = current($exception->getChildren());
if ($childException->getMessage() === $exception->getMessage()) {
return true;
}

if ($exception->hasCustomTemplate()) {
return $childException->hasCustomTemplate();
}

return !$childException instanceof NonOmissibleException;
}

/**
* @param string[]|string[][] $templates
*/
private function renderMessage(ValidationException $exception, array $templates): string
protected function renderMessage(ValidationException $exception, array $templates): string
{
if (isset($templates[$exception->getId()]) && is_string($templates[$exception->getId()])) {
$exception->updateTemplate($templates[$exception->getId()]);
Expand All @@ -238,7 +204,7 @@ private function renderMessage(ValidationException $exception, array $templates)
*
* @return string[]|string[][]
*/
private function findTemplates(array $templates, ...$ids): array
protected function findTemplates(array $templates, ...$ids): array
{
while (count($ids) > 0) {
$id = array_shift($ids);
Expand All @@ -255,4 +221,38 @@ private function findTemplates(array $templates, ...$ids): array

return $templates;
}

/**
* @return RecursiveIteratorIterator<RecursiveExceptionIterator>
*/
private function getRecursiveIterator(): RecursiveIteratorIterator
{
return new RecursiveIteratorIterator(
new RecursiveExceptionIterator($this),
RecursiveIteratorIterator::SELF_FIRST
);
}

private function isOmissible(Exception $exception): bool
{
if (!$exception instanceof self) {
return false;
}

if (count($exception->getChildren()) !== 1) {
return false;
}

/** @var ValidationException $childException */
$childException = current($exception->getChildren());
if ($childException->getMessage() === $exception->getMessage()) {
return true;
}

if ($exception->hasCustomTemplate()) {
return $childException->hasCustomTemplate();
}

return !$childException instanceof NonOmissibleException;
}
}
22 changes: 22 additions & 0 deletions tests/unit/Rules/EachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Respect\Validation\Rules;

use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Test\RuleTestCase;
use Respect\Validation\Validatable;
use SplStack;
Expand Down Expand Up @@ -93,6 +94,27 @@ public function itShouldCheckEachValue(): void
$rule->check(range(1, 3));
}

/**
* @test
*/
public function itShouldNotOverrideMessages(): void
{
$rule = new Each(new StringType());
try {
$rule->assert([1, 2, 3]);
} catch (NestedValidationException $e) {
$this->assertEquals(
$e->getMessages(),
[
'each' => 'Each item in `{ 1, 2, 3 }` must be valid',
'stringType.0' => '1 must be of type string',
'stringType.1' => '2 must be of type string',
'stringType.2' => '3 must be of type string',
]
);
}
}

/**
* @return Traversable<int>
*/
Expand Down

0 comments on commit bae314d

Please sign in to comment.