mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 07:15:45 +01:00
54 lines
1.2 KiB
PHP
54 lines
1.2 KiB
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\Message\Template;
|
|
use Respect\Validation\Rules\Core\Simple;
|
|
|
|
use function array_merge;
|
|
use function call_user_func_array;
|
|
use function count;
|
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
#[Template(
|
|
'{{name}} must be valid',
|
|
'{{name}} must be invalid',
|
|
)]
|
|
final class Callback extends Simple
|
|
{
|
|
/** @var callable */
|
|
private $callback;
|
|
|
|
/** @var mixed[] */
|
|
private readonly array $arguments;
|
|
|
|
public function __construct(callable $callback, mixed ...$arguments)
|
|
{
|
|
$this->callback = $callback;
|
|
$this->arguments = $arguments;
|
|
}
|
|
|
|
public function isValid(mixed $input): bool
|
|
{
|
|
return (bool) call_user_func_array($this->callback, $this->getArguments($input));
|
|
}
|
|
|
|
/** @return mixed[] */
|
|
private function getArguments(mixed $input): array
|
|
{
|
|
$arguments = [$input];
|
|
if (count($this->arguments) === 0) {
|
|
return $arguments;
|
|
}
|
|
|
|
return array_merge($arguments, $this->arguments);
|
|
}
|
|
}
|