respect-validation/library/Rules/UndefOr.php
Henrique Moody 707dcae65f
Refactor the "UndefOr" rule and related classes
This commit will rename the "Optional" rule to"UndefOr" while soft
deprecating the old name. It should work the same as the previous one
but with a different name. It will also prefix the result ID, allowing
more message customization.

While working on it, I realized that the prefix "undefOr" had a typo,
and it was using "undefOf" instead. I fixed that, too.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-26 01:35:36 +01:00

46 lines
1 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\Helpers\CanValidateUndefined;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Wrapper;
#[Template(
'The value must be undefined',
'The value must not be undefined',
self::TEMPLATE_STANDARD,
)]
#[Template(
'{{name}} must be undefined',
'{{name}} must not be undefined',
self::TEMPLATE_NAMED,
)]
final class UndefOr extends Wrapper
{
use CanValidateUndefined;
public const TEMPLATE_NAMED = '__named__';
public function evaluate(mixed $input): Result
{
if (!$this->isUndefined($input)) {
return $this->rule->evaluate($input)->withPrefixedId('undefOr');
}
if ($this->getName()) {
return Result::passed($input, $this, [], self::TEMPLATE_NAMED);
}
return Result::passed($input, $this);
}
}