Skip to content

Commit

Permalink
Update the validation engine of simple rules
Browse files Browse the repository at this point in the history
By "simple," I mean rules that have nothing in their constructor or that
have simple templates. This change introduces two classes that will be
the foundation for migrating all the rules to the newest validation
engine:

* Standard: this abstract rule contains only the accessors to define and
  retrieve names and templates, which means that the classes that extend
  it must implement the "evaluate()" method.

* Simple: this abstract rule contains the "evaluate()" method, which
  relies on the "validate()" method, which means that the classes that
  extend it must implement that method.

I expect many changes to the Simple abstract rule once all the rules get
migrated to the newest validation engine. I've chosen to keep relying on
the "validate()" method because it will make it easier to migrate
everything.

The "Standard" abstract rule uses a trait that triggers an
"E_USER_DEPRECATED" error in every method from the old validation
engine. That aims to support the migration because I can see clearly if
any places still use the methods I would like to delete once I migrate
everything.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 22, 2024
1 parent 238f2d5 commit df378bf
Show file tree
Hide file tree
Showing 83 changed files with 356 additions and 82 deletions.
70 changes: 70 additions & 0 deletions library/Helpers/DeprecatedValidatableMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Validation\Helpers;

use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Message\Parameter\Stringify;
use Respect\Validation\Message\TemplateRenderer;

use function sprintf;
use function trigger_error;

use const E_USER_DEPRECATED;

trait DeprecatedValidatableMethods
{
public function validate(mixed $input): bool
{
$this->triggerDeprecation(__METHOD__);

return false;
}

public function assert(mixed $input): void
{
$this->triggerDeprecation(__FUNCTION__);
}

public function check(mixed $input): void
{
$this->triggerDeprecation(__FUNCTION__);
}

/** @param array<string, mixed> $extraParameters */
public function reportError(mixed $input, array $extraParameters = []): ValidationException
{
$this->triggerDeprecation(__FUNCTION__);

return new ValidationException(
input: $input,
id: 'id',
params: $extraParameters,
template: 'template',
templates: [],
formatter: new TemplateRenderer(static fn (string $message) => $message, new Stringify()),
);
}

/** @return array<string, mixed> */
public function getParams(): array
{
$this->triggerDeprecation(__FUNCTION__);

return [];
}

private function triggerDeprecation(string $function): void
{
trigger_error(
sprintf('The "%s" method is deprecated, please use the "Validator" class instead.', $function),
E_USER_DEPRECATED
);
}
}
2 changes: 1 addition & 1 deletion library/Rules/AlwaysInvalid.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'{{name}} is valid',
self::TEMPLATE_SIMPLE,
)]
final class AlwaysInvalid extends AbstractRule
final class AlwaysInvalid extends Simple
{
public const TEMPLATE_SIMPLE = '__simple__';

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/AlwaysValid.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'{{name}} is always valid',
'{{name}} is always invalid',
)]
final class AlwaysValid extends AbstractRule
final class AlwaysValid extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'{{name}} must be of type array',
'{{name}} must not be of type array',
)]
final class ArrayType extends AbstractRule
final class ArrayType extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/ArrayVal.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be an array value',
'{{name}} must not be an array value',
)]
final class ArrayVal extends AbstractRule
final class ArrayVal extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be Base64-encoded',
'{{name}} must not be Base64-encoded',
)]
final class Base64 extends AbstractRule
final class Base64 extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/BoolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'{{name}} must be of type boolean',
'{{name}} must not be of type boolean',
)]
final class BoolType extends AbstractRule
final class BoolType extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/BoolVal.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'{{name}} must be a boolean value',
'{{name}} must not be a boolean value',
)]
final class BoolVal extends AbstractRule
final class BoolVal extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Bsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'{{name}} must be a BSN',
'{{name}} must not be a BSN',
)]
final class Bsn extends AbstractRule
final class Bsn extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'{{name}} must be callable',
'{{name}} must not be callable',
)]
final class CallableType extends AbstractRule
final class CallableType extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be valid',
'{{name}} must not be valid',
)]
final class Callback extends AbstractRule
final class Callback extends Simple
{
/**
* @var callable
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Cnh.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be a valid CNH number',
'{{name}} must not be a valid CNH number',
)]
final class Cnh extends AbstractRule
final class Cnh extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Cnpj.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'{{name}} must be a valid CNPJ number',
'{{name}} must not be a valid CNPJ number',
)]
final class Cnpj extends AbstractRule
final class Cnpj extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Countable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'{{name}} must be countable',
'{{name}} must not be countable',
)]
final class Countable extends AbstractRule
final class Countable extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Cpf.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'{{name}} must be a valid CPF number',
'{{name}} must not be a valid CPF number',
)]
final class Cpf extends AbstractRule
final class Cpf extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'{{name}} must be a directory',
'{{name}} must not be a directory',
)]
final class Directory extends AbstractRule
final class Directory extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'{{name}} must be valid email',
'{{name}} must not be an email',
)]
final class Email extends AbstractRule
final class Email extends Simple
{
private readonly ?EmailValidator $validator;

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Even.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be an even number',
'{{name}} must not be an even number',
)]
final class Even extends AbstractRule
final class Even extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Executable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be an executable file',
'{{name}} must not be an executable file',
)]
final class Executable extends AbstractRule
final class Executable extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must exist',
'{{name}} must not exist',
)]
final class Exists extends AbstractRule
final class Exists extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/FalseVal.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'{{name}} must evaluate to `false`',
'{{name}} must not evaluate to `false`',
)]
final class FalseVal extends AbstractRule
final class FalseVal extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Fibonacci.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'{{name}} must be a valid Fibonacci number',
'{{name}} must not be a valid Fibonacci number',
)]
final class Fibonacci extends AbstractRule
final class Fibonacci extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be a file',
'{{name}} must not be a file',
)]
final class File extends AbstractRule
final class File extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Finite.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'{{name}} must be a finite number',
'{{name}} must not be a finite number',
)]
final class Finite extends AbstractRule
final class Finite extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'{{name}} must be of type float',
'{{name}} must not be of type float',
)]
final class FloatType extends AbstractRule
final class FloatType extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/FloatVal.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'{{name}} must be a float number',
'{{name}} must not be a float number',
)]
final class FloatVal extends AbstractRule
final class FloatVal extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'{{name}} must be a valid image',
'{{name}} must not be a valid image',
)]
final class Image extends AbstractRule
final class Image extends Simple
{
private readonly finfo $fileInfo;

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Imei.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be a valid IMEI',
'{{name}} must not be a valid IMEI',
)]
final class Imei extends AbstractRule
final class Imei extends Simple
{
private const IMEI_SIZE = 15;

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Infinite.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'{{name}} must be an infinite number',
'{{name}} must not be an infinite number',
)]
final class Infinite extends AbstractRule
final class Infinite extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'{{name}} must be of type integer',
'{{name}} must not be of type integer',
)]
final class IntType extends AbstractRule
final class IntType extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/IntVal.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be an integer number',
'{{name}} must not be an integer number',
)]
final class IntVal extends AbstractRule
final class IntVal extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Isbn.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'{{name}} must be a ISBN',
'{{name}} must not be a ISBN',
)]
final class Isbn extends AbstractRule
final class Isbn extends Simple
{
/**
* @see https://howtodoinjava.com/regex/java-regex-validate-international-standard-book-number-isbns
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'{{name}} must be iterable',
'{{name}} must not be iterable',
)]
final class IterableType extends AbstractRule
final class IterableType extends Simple
{
use CanValidateIterable;

Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'{{name}} must be a valid JSON string',
'{{name}} must not be a valid JSON string',
)]
final class Json extends AbstractRule
final class Json extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/LeapDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'{{name}} must be leap date',
'{{name}} must not be leap date',
)]
final class LeapDate extends AbstractRule
final class LeapDate extends Simple
{
public function __construct(
private readonly string $format
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/LeapYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'{{name}} must be a leap year',
'{{name}} must not be a leap year',
)]
final class LeapYear extends AbstractRule
final class LeapYear extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
2 changes: 1 addition & 1 deletion library/Rules/Lowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'{{name}} must be lowercase',
'{{name}} must not be lowercase',
)]
final class Lowercase extends AbstractRule
final class Lowercase extends Simple
{
public function validate(mixed $input): bool
{
Expand Down
Loading

0 comments on commit df378bf

Please sign in to comment.