mirror of
https://github.com/Respect/Validation.git
synced 2024-05-16 21:26:40 +02:00
Compare commits
3 commits
74cb7bd509
...
b653b055b4
Author | SHA1 | Date | |
---|---|---|---|
b653b055b4 | |||
dd3a87b2db | |||
18923a2c93 |
|
@ -11,6 +11,8 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Message\Template;
|
||||
use Respect\Validation\Result;
|
||||
use Respect\Validation\Rules\Core\Standard;
|
||||
|
||||
use function bccomp;
|
||||
use function explode;
|
||||
|
@ -37,7 +39,7 @@ use const FILTER_VALIDATE_IP;
|
|||
'{{name}} must not be an IP address in the {{range|raw}} range',
|
||||
self::TEMPLATE_NETWORK_RANGE,
|
||||
)]
|
||||
final class Ip extends AbstractRule
|
||||
final class Ip extends Standard
|
||||
{
|
||||
public const TEMPLATE_NETWORK_RANGE = '__network_range__';
|
||||
|
||||
|
@ -55,38 +57,27 @@ final class Ip extends AbstractRule
|
|||
$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)) {
|
||||
return false;
|
||||
return Result::failed($input, $this, $parameters, $template);
|
||||
}
|
||||
|
||||
if (!$this->verifyAddress($input)) {
|
||||
return false;
|
||||
return Result::failed($input, $this, $parameters, $template);
|
||||
}
|
||||
|
||||
if ($this->mask) {
|
||||
return $this->belongsToSubnet($input);
|
||||
return new Result($this->belongsToSubnet($input), $input, $this, $parameters, $template);
|
||||
}
|
||||
|
||||
if ($this->startAddress && $this->endAddress) {
|
||||
return $this->verifyNetwork($input);
|
||||
return new Result($this->verifyNetwork($input), $input, $this, $parameters, $template);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
return Result::passed($input, $this, $parameters, $template);
|
||||
}
|
||||
|
||||
private function createRange(): ?string
|
||||
|
|
|
@ -11,6 +11,8 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Message\Template;
|
||||
use Respect\Validation\Result;
|
||||
use Respect\Validation\Rules\Core\Standard;
|
||||
|
||||
use function array_values;
|
||||
use function count;
|
||||
|
@ -29,7 +31,7 @@ use function str_split;
|
|||
'{{name}} must not be sorted in descending order',
|
||||
self::TEMPLATE_DESCENDING,
|
||||
)]
|
||||
final class Sorted extends AbstractRule
|
||||
final class Sorted extends Standard
|
||||
{
|
||||
public const TEMPLATE_ASCENDING = '__ascending__';
|
||||
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)) {
|
||||
return false;
|
||||
return Result::failed($input, $this, [], $template);
|
||||
}
|
||||
|
||||
$values = $this->getValues($input);
|
||||
$count = count($values);
|
||||
for ($position = 1; $position < $count; ++$position) {
|
||||
if (!$this->isSorted($values[$position], $values[$position - 1])) {
|
||||
return false;
|
||||
return Result::failed($input, $this, [], $template);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getStandardTemplate(mixed $input): string
|
||||
{
|
||||
if ($this->direction === Sorted::ASCENDING) {
|
||||
return self::TEMPLATE_ASCENDING;
|
||||
}
|
||||
|
||||
return self::TEMPLATE_DESCENDING;
|
||||
return Result::passed($input, $this, [], $template);
|
||||
}
|
||||
|
||||
private function isSorted(mixed $current, mixed $last): bool
|
||||
|
|
|
@ -9,14 +9,15 @@ 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 Respect\Validation\Rules\Core\Standard;
|
||||
|
||||
use function array_keys;
|
||||
use function is_string;
|
||||
use function mb_strtolower;
|
||||
use function preg_match;
|
||||
use function sprintf;
|
||||
|
||||
#[Template(
|
||||
'{{name}} must be a valid video URL',
|
||||
|
@ -28,7 +29,7 @@ use function sprintf;
|
|||
'{{name}} must not be a valid {{service|raw}} video URL',
|
||||
self::TEMPLATE_SERVICE,
|
||||
)]
|
||||
final class VideoUrl extends AbstractRule
|
||||
final class VideoUrl extends Standard
|
||||
{
|
||||
public const TEMPLATE_SERVICE = '__service__';
|
||||
|
||||
|
@ -44,18 +45,20 @@ final class VideoUrl extends AbstractRule
|
|||
private readonly ?string $service = null
|
||||
) {
|
||||
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)) {
|
||||
return false;
|
||||
return Result::failed($input, $this, $parameters, $template);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -63,23 +66,10 @@ final class VideoUrl extends AbstractRule
|
|||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
return Result::passed($input, $this, $parameters, $template);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
return Result::failed($input, $this, $parameters, $template);
|
||||
}
|
||||
|
||||
private function isSupportedService(string $service): bool
|
||||
|
|
Loading…
Reference in a new issue