mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 23:35:45 +01:00
We want to release version 3.0 as fresh as possible, without having to maintain backward compatibility with the previous versions. Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
40 lines
998 B
PHP
40 lines
998 B
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Attribute;
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Rule;
|
|
|
|
use function call_user_func;
|
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
|
final class Lazy implements Rule
|
|
{
|
|
/** @var callable(mixed): Rule */
|
|
private $ruleCreator;
|
|
|
|
/** @param callable(mixed): Rule $ruleCreator */
|
|
public function __construct(callable $ruleCreator)
|
|
{
|
|
$this->ruleCreator = $ruleCreator;
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
$rule = call_user_func($this->ruleCreator, $input);
|
|
if (!$rule instanceof Rule) {
|
|
throw new ComponentException('Lazy failed because it could not create the rule');
|
|
}
|
|
|
|
return $rule->evaluate($input);
|
|
}
|
|
}
|