mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 08:12:16 +02:00
d7dc0f2b4e
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>
43 lines
932 B
PHP
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);
|
|
}
|
|
}
|