diff --git a/library/ChainedValidator.php b/library/ChainedValidator.php index ae40dda53..4c36fd8f1 100644 --- a/library/ChainedValidator.php +++ b/library/ChainedValidator.php @@ -53,7 +53,7 @@ public function callableType(): ChainedValidator; public function callback(callable $callback): ChainedValidator; - public function charset(string ...$charset): ChainedValidator; + public function charset(string $charset, string ...$charsets): ChainedValidator; public function cnh(): ChainedValidator; diff --git a/library/Rules/Charset.php b/library/Rules/Charset.php index 7bc12c478..0d43d7b48 100644 --- a/library/Rules/Charset.php +++ b/library/Rules/Charset.php @@ -9,45 +9,47 @@ namespace Respect\Validation\Rules; -use Respect\Validation\Exceptions\ComponentException; +use Respect\Validation\Exceptions\InvalidRuleConstructorException; use Respect\Validation\Message\Template; +use Respect\Validation\Result; 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; +use function Respect\Stringifier\stringify; #[Template( '{{name}} must be in the {{charset|raw}} charset', '{{name}} must not be in the {{charset|raw}} charset', )] -final class Charset extends AbstractRule +final class Charset extends Standard { - /** - * @var string[] - */ + /** @var non-empty-array */ private readonly array $charset; - public function __construct(string ...$charset) + public function __construct(string $charset, string ...$charsets) { $available = mb_list_encodings(); - if (!empty(array_diff($charset, $available))) { - throw new ComponentException('Invalid charset'); + $charsets = array_merge([$charset], $charsets); + $diff = array_diff($charsets, $available); + if (count($diff) > 0) { + throw new InvalidRuleConstructorException('Invalid charset provided: %s', stringify(array_values($diff))); } - $this->charset = $charset; + $this->charset = $charsets; } - public function validate(mixed $input): bool + public function evaluate(mixed $input): Result { - return in_array(mb_detect_encoding($input, $this->charset, true), $this->charset, true); - } - - /** - * @return array> - */ - public function getParams(): array - { - return ['charset' => $this->charset]; + return new Result( + in_array(mb_detect_encoding($input, $this->charset, true), $this->charset), + $input, + $this, + ['charset' => $this->charset], + ); } } diff --git a/library/StaticValidator.php b/library/StaticValidator.php index efb52e162..207d22b45 100644 --- a/library/StaticValidator.php +++ b/library/StaticValidator.php @@ -55,7 +55,7 @@ public static function callableType(): ChainedValidator; public static function callback(callable $callback): ChainedValidator; - public static function charset(string ...$charset): ChainedValidator; + public static function charset(string $charset, string ...$charsets): ChainedValidator; public static function cnh(): ChainedValidator; diff --git a/tests/unit/Rules/CharsetTest.php b/tests/unit/Rules/CharsetTest.php index 934a58181..196bdc131 100644 --- a/tests/unit/Rules/CharsetTest.php +++ b/tests/unit/Rules/CharsetTest.php @@ -12,7 +12,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; -use Respect\Validation\Exceptions\ComponentException; +use Respect\Validation\Exceptions\InvalidRuleConstructorException; use Respect\Validation\Test\RuleTestCase; use function mb_convert_encoding; @@ -24,8 +24,8 @@ final class CharsetTest extends RuleTestCase #[Test] public function itShouldThrowsExceptionWhenCharsetIsNotValid(): void { - $this->expectException(ComponentException::class); - $this->expectExceptionMessage('Invalid charset'); + $this->expectException(InvalidRuleConstructorException::class); + $this->expectExceptionMessage('Invalid charset provided: `["UTF-9"]`'); new Charset('UTF-8', 'UTF-9'); }