respect-validation/library/Rules/Not.php
Henrique Moody dd896bb12d
Move template definitions to the rules
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>
2024-01-29 23:17:27 +01:00

113 lines
2.6 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\NonNegatable;
use Respect\Validation\Validatable;
use function array_shift;
use function count;
use function current;
use function sprintf;
final class Not extends AbstractRule
{
public const TEMPLATE_NONE = 'none';
public const TEMPLATE_SOME = 'some';
private readonly Validatable $rule;
public function __construct(Validatable $rule)
{
$this->rule = $this->extractNegatedRule($rule);
}
public function getNegatedRule(): Validatable
{
return $this->rule;
}
public function setName(string $name): Validatable
{
$this->rule->setName($name);
return parent::setName($name);
}
public function validate(mixed $input): bool
{
return $this->rule->validate($input) === false;
}
public function assert(mixed $input): void
{
if ($this->validate($input)) {
return;
}
$rule = $this->rule;
if ($rule instanceof AllOf) {
$rule = $this->absorbAllOf($rule, $input);
}
$exception = $rule->reportError($input);
$exception->updateMode(ValidationException::MODE_NEGATIVE);
throw $exception;
}
private function absorbAllOf(AllOf $rule, mixed $input): Validatable
{
$rules = $rule->getRules();
while (($current = array_shift($rules))) {
$rule = $current;
if (!$rule instanceof AllOf) {
continue;
}
if (!$rule->validate($input)) {
continue;
}
$rules = $rule->getRules();
}
return $rule;
}
private function extractNegatedRule(Validatable $rule): Validatable
{
if ($rule instanceof NonNegatable) {
throw new ComponentException(
sprintf(
'"%s" can not be wrapped in Not()',
$rule::class
)
);
}
if ($rule instanceof self && $rule->getNegatedRule() instanceof self) {
return $this->extractNegatedRule($rule->getNegatedRule()->getNegatedRule());
}
if (!$rule instanceof AllOf) {
return $rule;
}
$rules = $rule->getRules();
if (count($rules) === 1) {
return $this->extractNegatedRule(current($rules));
}
return $rule;
}
}