respect-validation/library/Rules/Lazy.php
Henrique Moody 0066786fa7
Remove deprecated methods check() and validate()
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>
2025-12-18 17:29:02 +01:00

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);
}
}