respect-validation/library/Exceptions/NestedValidationException.php
Henrique Moody e4f2c8154a
Use PHP attributes to define templates
Creating a specific exception for each rule adds a painful overhead. If
you want to make a custom message for your rule, you will need to create
an exception and then register that exception namespace to be able to
use it—all that is just for customizing the message of your rule.

Having different namespaces also implies that you need to fetch the
exception of the rule from another directory to change it. As Uncle Bob
said, "Classes that change together belong together. Classes that are
not reused together should not be grouped."

This commit will drastically change this library, moving all the
templates from the exceptions to the rules. Consequently, the Factory
becomes much simpler, and the library gets a bit smaller, too.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:43:57 +01:00

236 lines
6.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 IteratorAggregate;
use RecursiveIteratorIterator;
use SplObjectStorage;
use function array_shift;
use function count;
use function current;
use function implode;
use function is_array;
use function is_string;
use function spl_object_hash;
use function sprintf;
use function str_repeat;
use const PHP_EOL;
/**
* @implements IteratorAggregate<ValidationException>
*/
class NestedValidationException extends ValidationException implements IteratorAggregate
{
/**
* @var ValidationException[]
*/
private array $exceptions = [];
/**
* @return ValidationException[]
*/
public function getChildren(): array
{
return $this->exceptions;
}
public function addChild(ValidationException $exception): self
{
$this->exceptions[spl_object_hash($exception)] = $exception;
return $this;
}
/**
* @param ValidationException[] $exceptions
*/
public function addChildren(array $exceptions): self
{
foreach ($exceptions as $exception) {
$this->addChild($exception);
}
return $this;
}
/**
* @return SplObjectStorage<ValidationException, int>
*/
public function getIterator(): SplObjectStorage
{
/** @var SplObjectStorage<ValidationException, int> */
$childrenExceptions = new SplObjectStorage();
$recursiveIteratorIterator = $this->getRecursiveIterator();
$lastDepth = 0;
$lastDepthOriginal = 0;
$knownDepths = [];
foreach ($recursiveIteratorIterator as $childException) {
if ($this->isOmissible($childException)) {
continue;
}
$currentDepth = $lastDepth;
$currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1;
if (isset($knownDepths[$currentDepthOriginal])) {
$currentDepth = $knownDepths[$currentDepthOriginal];
} elseif ($currentDepthOriginal > $lastDepthOriginal) {
++$currentDepth;
}
if (!isset($knownDepths[$currentDepthOriginal])) {
$knownDepths[$currentDepthOriginal] = $currentDepth;
}
$lastDepth = $currentDepth;
$lastDepthOriginal = $currentDepthOriginal;
$childrenExceptions->attach($childException, $currentDepth);
}
return $childrenExceptions;
}
/**
* Returns a key->value array with all the messages of the exception.
*
* In this array the "keys" are the ids of the exceptions (defined name or
* name of the rule) and the values are the message.
*
* Once templates are passed it overwrites the templates of the given
* messages.
*
* @param string[]|string[][] $templates
*
* @return string[]
*/
public function getMessages(array $templates = []): array
{
$messages = [$this->getId() => $this->renderMessage($this, $templates)];
foreach ($this->getChildren() as $exception) {
$id = $exception->getId();
if (!$exception instanceof self) {
$messages[$id] = $this->renderMessage(
$exception,
$this->findTemplates($templates, $this->getId())
);
continue;
}
$messages[$id] = $exception->getMessages($this->findTemplates($templates, $id, $this->getId()));
if (count($messages[$id]) > 1) {
continue;
}
$messages[$id] = current($messages[$exception->getId()]);
}
if (count($messages) > 1) {
unset($messages[$this->getId()]);
}
return $messages;
}
public function getFullMessage(): string
{
$messages = [];
$leveler = 1;
if (!$this->isOmissible($this)) {
$leveler = 0;
$messages[] = sprintf('- %s', $this->getMessage());
}
$exceptions = $this->getIterator();
/** @var ValidationException $exception */
foreach ($exceptions as $exception) {
$messages[] = sprintf(
'%s- %s',
str_repeat(' ', (int) ($exceptions[$exception] - $leveler) * 2),
$exception->getMessage()
);
}
return implode(PHP_EOL, $messages);
}
/**
* @param string[]|string[][] $templates
*/
protected function renderMessage(ValidationException $exception, array $templates): string
{
if (isset($templates[$exception->getId()]) && is_string($templates[$exception->getId()])) {
$exception->updateTemplate($templates[$exception->getId()]);
}
return $exception->getMessage();
}
/**
* @param string[]|string[][] $templates
*
* @return string[]|string[][]
*/
protected function findTemplates(array $templates, mixed ...$ids): array
{
while (count($ids) > 0) {
$id = array_shift($ids);
if (!isset($templates[$id])) {
continue;
}
if (!is_array($templates[$id])) {
continue;
}
$templates = $templates[$id];
}
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 NonOmissibleValidationException;
}
}