Skip to content

Commit

Permalink
Update the validation engine of the "Charset" rule
Browse files Browse the repository at this point in the history
Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 23, 2024
1 parent 85bff04 commit 9cc57f8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion library/ChainedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
40 changes: 21 additions & 19 deletions library/Rules/Charset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<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],
);
}
}
2 changes: 1 addition & 1 deletion library/StaticValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Rules/CharsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
}
Expand Down

0 comments on commit 9cc57f8

Please sign in to comment.