respect-validation/library/Rules/Base.php
Henrique Moody d1f108dc87
Make proper use of exceptions in rules
This commit will ensure that all rules that cannot be created because of
invalid arguments in the constructor will throw the
InvalidRuleConstructorException. It will also make ComponentException
extend LogicException, which makes it easier to determine that the
client has improperly used the library.

I also introduced some tests for two exceptions with logic in their
constructor.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-25 22:09:02 +01:00

47 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 Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use function mb_strlen;
use function mb_substr;
use function preg_match;
#[Template(
'{{name}} must be a number in the base {{base|raw}}',
'{{name}} must not be a number in the base {{base|raw}}',
)]
final class Base extends Standard
{
public function __construct(
private readonly int $base,
private readonly string $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
) {
$max = mb_strlen($this->chars);
if ($base > $max) {
throw new InvalidRuleConstructorException('a base between 1 and %s is required', (string) $max);
}
}
public function evaluate(mixed $input): Result
{
return new Result(
(bool) preg_match('@^[' . mb_substr($this->chars, 0, $this->base) . ']+$@', (string) $input),
$input,
$this,
['base' => $this->base]
);
}
}