mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 13:22:24 +02:00
Update the validation engine of the "Charset" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
85bff04ad8
commit
9cc57f8ffc
|
@ -53,7 +53,7 @@ interface ChainedValidator extends Validatable
|
|||
|
||||
public function callback(callable $callback): ChainedValidator;
|
||||
|
||||
public function charset(string ...$charset): ChainedValidator;
|
||||
public function charset(string $charset, string ...$charsets): ChainedValidator;
|
||||
|
||||
public function cnh(): ChainedValidator;
|
||||
|
||||
|
|
|
@ -9,45 +9,47 @@ declare(strict_types=1);
|
|||
|
||||
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<string> */
|
||||
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<string, array<string>>
|
||||
*/
|
||||
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],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ interface StaticValidator
|
|||
|
||||
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;
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace Respect\Validation\Rules;
|
|||
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');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue