respect-validation/library/Rules/Templated.php
2025-12-18 19:03:39 +01:00

38 lines
964 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\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Wrapper;
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Templated extends Wrapper
{
/** @param array<string, mixed> $parameters */
public function __construct(
Rule $rule,
private readonly string $template,
private readonly array $parameters = [],
) {
parent::__construct($rule);
}
public function evaluate(mixed $input): Result
{
$result = $this->rule->evaluate($input);
if ($result->hasCustomTemplate()) {
return $result;
}
return $result->withTemplate($this->template)->withExtraParameters($this->parameters);
}
}