mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
It's easier to identify the reason for choosing a specific message in the rule than in the exception. The same goes for the key we use to determine the templates. This change will simplify the `ValidationException` because it will already receive the template it needs to use. As a consequence, the `Factory` also becomes more straightforward. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Exceptions;
|
|
|
|
use Respect\Validation\Validatable;
|
|
|
|
final class EachException extends NestedValidationException
|
|
{
|
|
/**
|
|
* @var array<string, array<string, string>>
|
|
*/
|
|
protected array $defaultTemplates = [
|
|
self::MODE_DEFAULT => [
|
|
Validatable::TEMPLATE_STANDARD => 'Each item in {{name}} must be valid',
|
|
],
|
|
self::MODE_NEGATIVE => [
|
|
Validatable::TEMPLATE_STANDARD => 'Each item in {{name}} must not validate',
|
|
],
|
|
];
|
|
|
|
/**
|
|
* @todo This method shares too much with the parent implementation
|
|
*
|
|
* @param array<string, string> $templates
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function getMessages(array $templates = []): array
|
|
{
|
|
$messages = [];
|
|
$count = -1;
|
|
foreach ($this->getChildren() as $exception) {
|
|
$count++;
|
|
$id = $exception->getId();
|
|
|
|
$messages[$id . '.' . $count] = $this->renderMessage(
|
|
$exception,
|
|
$this->findTemplates($templates, $this->getId())
|
|
);
|
|
}
|
|
|
|
return $messages;
|
|
}
|
|
}
|