Compare commits

...

3 commits

Author SHA1 Message Date
Henrique Moody b653b055b4
Update the validation engine of the "Ip" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-25 00:15:43 +01:00
Henrique Moody dd3a87b2db
Update the validation engine of the "Sorted" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-25 00:13:32 +01:00
Henrique Moody 18923a2c93
Update the validation engine of the "VideoUrl" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-24 19:32:38 +01:00
3 changed files with 31 additions and 56 deletions

View file

@ -11,6 +11,8 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException; use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Message\Template; use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use function bccomp; use function bccomp;
use function explode; use function explode;
@ -37,7 +39,7 @@ use const FILTER_VALIDATE_IP;
'{{name}} must not be an IP address in the {{range|raw}} range', '{{name}} must not be an IP address in the {{range|raw}} range',
self::TEMPLATE_NETWORK_RANGE, self::TEMPLATE_NETWORK_RANGE,
)] )]
final class Ip extends AbstractRule final class Ip extends Standard
{ {
public const TEMPLATE_NETWORK_RANGE = '__network_range__'; public const TEMPLATE_NETWORK_RANGE = '__network_range__';
@ -55,38 +57,27 @@ final class Ip extends AbstractRule
$this->range = $this->createRange(); $this->range = $this->createRange();
} }
public function validate(mixed $input): bool public function evaluate(mixed $input): Result
{ {
$parameters = ['range' => $this->range];
$template = $this->range ? self::TEMPLATE_NETWORK_RANGE : self::TEMPLATE_STANDARD;
if (!is_string($input)) { if (!is_string($input)) {
return false; return Result::failed($input, $this, $parameters, $template);
} }
if (!$this->verifyAddress($input)) { if (!$this->verifyAddress($input)) {
return false; return Result::failed($input, $this, $parameters, $template);
} }
if ($this->mask) { if ($this->mask) {
return $this->belongsToSubnet($input); return new Result($this->belongsToSubnet($input), $input, $this, $parameters, $template);
} }
if ($this->startAddress && $this->endAddress) { if ($this->startAddress && $this->endAddress) {
return $this->verifyNetwork($input); return new Result($this->verifyNetwork($input), $input, $this, $parameters, $template);
} }
return true; return Result::passed($input, $this, $parameters, $template);
}
/**
* @return array<string, mixed>
*/
public function getParams(): array
{
return ['range' => $this->range];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->range ? self::TEMPLATE_NETWORK_RANGE : self::TEMPLATE_STANDARD;
} }
private function createRange(): ?string private function createRange(): ?string

View file

@ -11,6 +11,8 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException; use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Message\Template; use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use function array_values; use function array_values;
use function count; use function count;
@ -29,7 +31,7 @@ use function str_split;
'{{name}} must not be sorted in descending order', '{{name}} must not be sorted in descending order',
self::TEMPLATE_DESCENDING, self::TEMPLATE_DESCENDING,
)] )]
final class Sorted extends AbstractRule final class Sorted extends Standard
{ {
public const TEMPLATE_ASCENDING = '__ascending__'; public const TEMPLATE_ASCENDING = '__ascending__';
public const TEMPLATE_DESCENDING = '__descending__'; public const TEMPLATE_DESCENDING = '__descending__';
@ -47,30 +49,22 @@ final class Sorted extends AbstractRule
} }
} }
public function validate(mixed $input): bool public function evaluate(mixed $input): Result
{ {
$template = $this->direction === self::ASCENDING ? self::TEMPLATE_ASCENDING : self::TEMPLATE_DESCENDING;
if (!is_array($input) && !is_string($input)) { if (!is_array($input) && !is_string($input)) {
return false; return Result::failed($input, $this, [], $template);
} }
$values = $this->getValues($input); $values = $this->getValues($input);
$count = count($values); $count = count($values);
for ($position = 1; $position < $count; ++$position) { for ($position = 1; $position < $count; ++$position) {
if (!$this->isSorted($values[$position], $values[$position - 1])) { if (!$this->isSorted($values[$position], $values[$position - 1])) {
return false; return Result::failed($input, $this, [], $template);
} }
} }
return true; return Result::passed($input, $this, [], $template);
}
protected function getStandardTemplate(mixed $input): string
{
if ($this->direction === Sorted::ASCENDING) {
return self::TEMPLATE_ASCENDING;
}
return self::TEMPLATE_DESCENDING;
} }
private function isSorted(mixed $current, mixed $last): bool private function isSorted(mixed $current, mixed $last): bool

View file

@ -9,14 +9,15 @@ declare(strict_types=1);
namespace Respect\Validation\Rules; namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException; use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template; use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use function array_keys; use function array_keys;
use function is_string; use function is_string;
use function mb_strtolower; use function mb_strtolower;
use function preg_match; use function preg_match;
use function sprintf;
#[Template( #[Template(
'{{name}} must be a valid video URL', '{{name}} must be a valid video URL',
@ -28,7 +29,7 @@ use function sprintf;
'{{name}} must not be a valid {{service|raw}} video URL', '{{name}} must not be a valid {{service|raw}} video URL',
self::TEMPLATE_SERVICE, self::TEMPLATE_SERVICE,
)] )]
final class VideoUrl extends AbstractRule final class VideoUrl extends Standard
{ {
public const TEMPLATE_SERVICE = '__service__'; public const TEMPLATE_SERVICE = '__service__';
@ -44,18 +45,20 @@ final class VideoUrl extends AbstractRule
private readonly ?string $service = null private readonly ?string $service = null
) { ) {
if ($service !== null && !$this->isSupportedService($service)) { if ($service !== null && !$this->isSupportedService($service)) {
throw new ComponentException(sprintf('"%s" is not a recognized video service.', $service)); throw new InvalidRuleConstructorException('"%s" is not a recognized video service.', $service);
} }
} }
public function validate(mixed $input): bool public function evaluate(mixed $input): Result
{ {
$parameters = ['service' => $this->service];
$template = $this->service !== null ? self::TEMPLATE_SERVICE : self::TEMPLATE_STANDARD;
if (!is_string($input)) { if (!is_string($input)) {
return false; return Result::failed($input, $this, $parameters, $template);
} }
if ($this->service !== null) { if ($this->service !== null) {
return $this->isValid($this->service, $input); return new Result($this->isValid($this->service, $input), $input, $this, $parameters, $template);
} }
foreach (array_keys(self::SERVICES) as $service) { foreach (array_keys(self::SERVICES) as $service) {
@ -63,23 +66,10 @@ final class VideoUrl extends AbstractRule
continue; continue;
} }
return true; return Result::passed($input, $this, $parameters, $template);
} }
return false; return Result::failed($input, $this, $parameters, $template);
}
/**
* @return array<string, string|null>
*/
public function getParams(): array
{
return ['service' => $this->service];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->service ? self::TEMPLATE_SERVICE : self::TEMPLATE_STANDARD;
} }
private function isSupportedService(string $service): bool private function isSupportedService(string $service): bool