mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 15:50:03 +01:00
We want to release version 3.0 as fresh as possible, without having to maintain backward compatibility with the previous versions. Because that version will be on for some time, we decided it will be best to support only PHP version 8.5 or higher. Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
57 lines
1.6 KiB
PHP
57 lines
1.6 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\Exceptions\InvalidRuleConstructorException;
|
|
use Respect\Validation\Message\Template;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Rule;
|
|
|
|
use function array_diff;
|
|
use function array_merge;
|
|
use function array_values;
|
|
use function count;
|
|
use function in_array;
|
|
use function mb_detect_encoding;
|
|
use function mb_list_encodings;
|
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
#[Template(
|
|
'{{name}} must only contain characters from the {{charset|raw}} charset',
|
|
'{{name}} must not contain any characters from the {{charset|raw}} charset',
|
|
)]
|
|
final readonly class Charset implements Rule
|
|
{
|
|
/** @var non-empty-array<string> */
|
|
private array $charset;
|
|
|
|
public function __construct(string $charset, string ...$charsets)
|
|
{
|
|
$available = mb_list_encodings();
|
|
$charsets = array_merge([$charset], $charsets);
|
|
$diff = array_diff($charsets, $available);
|
|
if (count($diff) > 0) {
|
|
throw new InvalidRuleConstructorException('Invalid charset provided: %s', array_values($diff));
|
|
}
|
|
|
|
$this->charset = $charsets;
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
return new Result(
|
|
in_array(mb_detect_encoding($input, $this->charset, true), $this->charset),
|
|
$input,
|
|
$this,
|
|
['charset' => $this->charset],
|
|
);
|
|
}
|
|
}
|