respect-validation/library/Rules/NullOr.php
Henrique Moody d7dc0f2b4e
Refactor the "NullOr" rule and related classes
This commit will rename the "Nullable" rule to "NullOr" 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 "nullOr" had a typo,
and it was using "nullOf" instead. I fixed that, too.

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

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