From dd896bb12d464c69c48fe8a845bf6890aefe8094 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Mon, 29 Jan 2024 17:29:01 +0100 Subject: [PATCH] Move template definitions to the rules It's easier to identify the reason for choosing a specific message in the rule than in the exception. The same goes for the key we use to determine the templates. This change will simplify the `ValidationException` because it will already receive the template it needs to use. As a consequence, the `Factory` also becomes more straightforward. Signed-off-by: Henrique Moody --- library/Exceptions/AllOfException.php | 5 -- library/Exceptions/AlnumException.php | 11 ++-- library/Exceptions/AlphaException.php | 11 ++-- library/Exceptions/AlwaysInvalidException.php | 6 ++- library/Exceptions/AlwaysValidException.php | 6 ++- library/Exceptions/AnyOfException.php | 6 ++- library/Exceptions/ArrayTypeException.php | 6 ++- library/Exceptions/ArrayValException.php | 6 ++- library/Exceptions/Base64Exception.php | 6 ++- library/Exceptions/BaseException.php | 6 ++- library/Exceptions/BetweenException.php | 6 ++- library/Exceptions/BoolTypeException.php | 6 ++- library/Exceptions/BoolValException.php | 6 ++- library/Exceptions/BsnException.php | 6 ++- library/Exceptions/CallException.php | 6 ++- library/Exceptions/CallableTypeException.php | 6 ++- library/Exceptions/CharsetException.php | 6 ++- library/Exceptions/CnhException.php | 6 ++- library/Exceptions/CnpjException.php | 6 ++- library/Exceptions/ConsonantException.php | 11 ++-- library/Exceptions/ContainsAnyException.php | 6 ++- library/Exceptions/ContainsException.php | 6 ++- library/Exceptions/ControlException.php | 11 ++-- library/Exceptions/CountableException.php | 6 ++- library/Exceptions/CountryCodeException.php | 6 ++- library/Exceptions/CpfException.php | 6 ++- library/Exceptions/CreditCardException.php | 20 ++----- library/Exceptions/CurrencyCodeException.php | 6 ++- library/Exceptions/DateException.php | 6 ++- library/Exceptions/DateTimeException.php | 18 +++---- library/Exceptions/DecimalException.php | 6 ++- library/Exceptions/DigitException.php | 11 ++-- library/Exceptions/DirectoryException.php | 6 ++- library/Exceptions/DomainException.php | 6 ++- library/Exceptions/EachException.php | 6 ++- library/Exceptions/EmailException.php | 6 ++- library/Exceptions/EndsWithException.php | 6 ++- library/Exceptions/EqualsException.php | 6 ++- library/Exceptions/EquivalentException.php | 6 ++- library/Exceptions/EvenException.php | 6 ++- library/Exceptions/ExecutableException.php | 6 ++- library/Exceptions/ExistsException.php | 6 ++- library/Exceptions/ExtensionException.php | 6 ++- library/Exceptions/FactorException.php | 6 ++- library/Exceptions/FalseValException.php | 6 ++- library/Exceptions/FibonacciException.php | 6 ++- library/Exceptions/FileException.php | 6 ++- .../FilteredValidationException.php | 6 --- library/Exceptions/FiniteException.php | 6 ++- library/Exceptions/FloatTypeException.php | 6 ++- library/Exceptions/FloatValException.php | 6 ++- library/Exceptions/GraphException.php | 11 ++-- library/Exceptions/GreaterThanException.php | 6 ++- library/Exceptions/HexRgbColorException.php | 6 ++- library/Exceptions/IbanException.php | 6 ++- library/Exceptions/IdenticalException.php | 6 ++- library/Exceptions/ImageException.php | 6 ++- library/Exceptions/ImeiException.php | 6 ++- library/Exceptions/InException.php | 6 ++- library/Exceptions/InfiniteException.php | 6 ++- library/Exceptions/InstanceException.php | 6 ++- library/Exceptions/IntTypeException.php | 6 ++- library/Exceptions/IntValException.php | 6 ++- library/Exceptions/IpException.php | 22 +++----- library/Exceptions/IsbnException.php | 6 ++- library/Exceptions/IterableTypeException.php | 6 ++- library/Exceptions/JsonException.php | 6 ++- library/Exceptions/KeyException.php | 18 +++---- library/Exceptions/KeyNestedException.php | 18 +++---- library/Exceptions/KeySetException.php | 26 ++------- library/Exceptions/KeyValueException.php | 18 +++---- library/Exceptions/LanguageCodeException.php | 6 ++- library/Exceptions/LeapDateException.php | 6 ++- library/Exceptions/LeapYearException.php | 6 ++- library/Exceptions/LengthException.php | 52 +++++------------- library/Exceptions/LessThanException.php | 6 ++- library/Exceptions/LowercaseException.php | 6 ++- library/Exceptions/LuhnException.php | 6 ++- library/Exceptions/MacAddressException.php | 6 ++- library/Exceptions/MaxAgeException.php | 6 ++- library/Exceptions/MaxException.php | 6 ++- library/Exceptions/MimetypeException.php | 6 ++- library/Exceptions/MinAgeException.php | 6 ++- library/Exceptions/MinException.php | 6 ++- library/Exceptions/MultipleException.php | 6 ++- library/Exceptions/NegativeException.php | 6 ++- library/Exceptions/NfeAccessKeyException.php | 6 ++- library/Exceptions/NifException.php | 6 ++- library/Exceptions/NipException.php | 6 ++- library/Exceptions/NoException.php | 6 ++- library/Exceptions/NoWhitespaceException.php | 6 ++- library/Exceptions/NoneOfException.php | 6 ++- library/Exceptions/NotBlankException.php | 22 +++----- library/Exceptions/NotEmojiException.php | 6 ++- library/Exceptions/NotEmptyException.php | 22 +++----- library/Exceptions/NotOptionalException.php | 22 +++----- library/Exceptions/NullTypeException.php | 6 ++- library/Exceptions/NullableException.php | 22 +++----- library/Exceptions/NumberException.php | 6 ++- library/Exceptions/NumericValException.php | 6 ++- library/Exceptions/ObjectTypeException.php | 6 ++- library/Exceptions/OddException.php | 6 ++- library/Exceptions/OneOfException.php | 6 ++- library/Exceptions/OptionalException.php | 18 +++---- library/Exceptions/PerfectSquareException.php | 6 ++- library/Exceptions/PeselException.php | 6 ++- library/Exceptions/PhoneException.php | 21 +++----- library/Exceptions/PhpLabelException.php | 6 ++- library/Exceptions/PisException.php | 6 ++- library/Exceptions/PolishIdCardException.php | 6 ++- library/Exceptions/PortugueseNifException.php | 6 ++- library/Exceptions/PositiveException.php | 6 ++- library/Exceptions/PostalCodeException.php | 6 ++- library/Exceptions/PrimeNumberException.php | 6 ++- library/Exceptions/PrintableException.php | 11 ++-- library/Exceptions/PropertyException.php | 18 +++---- .../PublicDomainSuffixException.php | 6 ++- library/Exceptions/PunctException.php | 11 ++-- library/Exceptions/ReadableException.php | 6 ++- library/Exceptions/RegexException.php | 6 ++- library/Exceptions/ResourceTypeException.php | 6 ++- library/Exceptions/RomanException.php | 6 ++- library/Exceptions/ScalarValException.php | 6 ++- library/Exceptions/SizeException.php | 31 +++-------- library/Exceptions/SlugException.php | 6 ++- library/Exceptions/SortedException.php | 16 ++---- library/Exceptions/SpaceException.php | 11 ++-- library/Exceptions/StartsWithException.php | 6 ++- library/Exceptions/StringTypeException.php | 6 ++- library/Exceptions/StringValException.php | 6 ++- .../Exceptions/SubdivisionCodeException.php | 6 ++- library/Exceptions/SubsetException.php | 6 ++- library/Exceptions/SymbolicLinkException.php | 6 ++- library/Exceptions/TimeException.php | 6 ++- library/Exceptions/TldException.php | 6 ++- library/Exceptions/TrueValException.php | 6 ++- library/Exceptions/TypeException.php | 6 ++- library/Exceptions/UniqueException.php | 6 ++- library/Exceptions/UploadedException.php | 6 ++- library/Exceptions/UppercaseException.php | 6 ++- library/Exceptions/UrlException.php | 6 ++- library/Exceptions/UuidException.php | 22 +++----- library/Exceptions/ValidationException.php | 13 ++--- library/Exceptions/VersionException.php | 6 ++- library/Exceptions/VideoUrlException.php | 22 +++----- library/Exceptions/VowelException.php | 11 ++-- library/Exceptions/WritableException.php | 6 ++- library/Exceptions/XdigitException.php | 11 ++-- library/Exceptions/YesException.php | 6 ++- library/Factory.php | 44 ++++----------- library/Rules/AbstractFilterRule.php | 7 +++ library/Rules/AbstractRelated.php | 22 ++++++-- library/Rules/AbstractRule.php | 5 ++ library/Rules/AllOf.php | 10 +++- library/Rules/AlwaysInvalid.php | 2 + library/Rules/AnyOf.php | 4 +- library/Rules/CreditCard.php | 15 ++++++ library/Rules/DateTime.php | 7 +++ library/Rules/Domain.php | 3 +- library/Rules/Ip.php | 7 +++ library/Rules/KeySet.php | 18 +++++++ library/Rules/KeyValue.php | 43 ++++++++++++--- library/Rules/Length.php | 28 ++++++++++ library/Rules/NoneOf.php | 4 +- library/Rules/Not.php | 3 ++ library/Rules/NotBlank.php | 15 ++++++ library/Rules/NotEmpty.php | 15 ++++++ library/Rules/NotOptional.php | 15 ++++++ library/Rules/Nullable.php | 15 ++++++ library/Rules/OneOf.php | 4 +- library/Rules/Optional.php | 15 ++++++ library/Rules/Phone.php | 8 +++ library/Rules/Size.php | 21 ++++++++ library/Rules/Sorted.php | 16 ++++++ library/Rules/Uuid.php | 7 +++ library/Rules/VideoUrl.php | 7 +++ library/Rules/When.php | 3 +- library/Validatable.php | 4 ++ tests/library/Stubs/CompositeSub.php | 2 + .../NestedValidationExceptionTest.php | 31 +++++++++-- .../Exceptions/ValidationExceptionTest.php | 53 ++++++++++--------- 181 files changed, 1042 insertions(+), 680 deletions(-) diff --git a/library/Exceptions/AllOfException.php b/library/Exceptions/AllOfException.php index 1e8c5114..a0140c1c 100644 --- a/library/Exceptions/AllOfException.php +++ b/library/Exceptions/AllOfException.php @@ -24,9 +24,4 @@ class AllOfException extends GroupedValidationException self::SOME => 'These rules must not pass for {{name}}', ], ]; - - protected function chooseTemplate(): string - { - return self::SOME; - } } diff --git a/library/Exceptions/AlnumException.php b/library/Exceptions/AlnumException.php index 57a41382..e1b4aa44 100644 --- a/library/Exceptions/AlnumException.php +++ b/library/Exceptions/AlnumException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule as Filter; +use Respect\Validation\Validatable; + final class AlnumException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class AlnumException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only letters (a-z) and digits (0-9)', - self::EXTRA => '{{name}} must contain only letters (a-z), digits (0-9) and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only letters (a-z) and digits (0-9)', + Filter::TEMPLATE_EXTRA => '{{name}} must contain only letters (a-z), digits (0-9) and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain letters (a-z) or digits (0-9)', - self::EXTRA => '{{name}} must not contain letters (a-z), digits (0-9) or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain letters (a-z) or digits (0-9)', + Filter::TEMPLATE_EXTRA => '{{name}} must not contain letters (a-z), digits (0-9) or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/AlphaException.php b/library/Exceptions/AlphaException.php index 8bde46a1..745cc9fe 100644 --- a/library/Exceptions/AlphaException.php +++ b/library/Exceptions/AlphaException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule; +use Respect\Validation\Validatable; + final class AlphaException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class AlphaException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only letters (a-z)', - self::EXTRA => '{{name}} must contain only letters (a-z) and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only letters (a-z)', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must contain only letters (a-z) and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain letters (a-z)', - self::EXTRA => '{{name}} must not contain letters (a-z) or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain letters (a-z)', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must not contain letters (a-z) or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/AlwaysInvalidException.php b/library/Exceptions/AlwaysInvalidException.php index 91edbb2c..0cf0aef4 100644 --- a/library/Exceptions/AlwaysInvalidException.php +++ b/library/Exceptions/AlwaysInvalidException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class AlwaysInvalidException extends ValidationException { public const SIMPLE = 'simple'; @@ -18,11 +20,11 @@ final class AlwaysInvalidException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} is always invalid', + Validatable::TEMPLATE_STANDARD => '{{name}} is always invalid', self::SIMPLE => '{{name}} is not valid', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} is always valid', + Validatable::TEMPLATE_STANDARD => '{{name}} is always valid', self::SIMPLE => '{{name}} is valid', ], ]; diff --git a/library/Exceptions/AlwaysValidException.php b/library/Exceptions/AlwaysValidException.php index 18d9c0a2..1a404ca6 100644 --- a/library/Exceptions/AlwaysValidException.php +++ b/library/Exceptions/AlwaysValidException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class AlwaysValidException extends ValidationException { /** @@ -16,10 +18,10 @@ final class AlwaysValidException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} is always valid', + Validatable::TEMPLATE_STANDARD => '{{name}} is always valid', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} is always invalid', + Validatable::TEMPLATE_STANDARD => '{{name}} is always invalid', ], ]; } diff --git a/library/Exceptions/AnyOfException.php b/library/Exceptions/AnyOfException.php index ef180345..1a12899b 100644 --- a/library/Exceptions/AnyOfException.php +++ b/library/Exceptions/AnyOfException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class AnyOfException extends NestedValidationException { /** @@ -16,10 +18,10 @@ final class AnyOfException extends NestedValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'At least one of these rules must pass for {{name}}', + Validatable::TEMPLATE_STANDARD => 'At least one of these rules must pass for {{name}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'At least one of these rules must not pass for {{name}}', + Validatable::TEMPLATE_STANDARD => 'At least one of these rules must not pass for {{name}}', ], ]; } diff --git a/library/Exceptions/ArrayTypeException.php b/library/Exceptions/ArrayTypeException.php index 9dda2c66..05c1b7c0 100644 --- a/library/Exceptions/ArrayTypeException.php +++ b/library/Exceptions/ArrayTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ArrayTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ArrayTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be of type array', + Validatable::TEMPLATE_STANDARD => '{{name}} must be of type array', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be of type array', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be of type array', ], ]; } diff --git a/library/Exceptions/ArrayValException.php b/library/Exceptions/ArrayValException.php index f5cb2cbd..c5cf5d27 100644 --- a/library/Exceptions/ArrayValException.php +++ b/library/Exceptions/ArrayValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ArrayValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ArrayValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an array value', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an array value', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an array value', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an array value', ], ]; } diff --git a/library/Exceptions/Base64Exception.php b/library/Exceptions/Base64Exception.php index f863cadf..9bebaa26 100644 --- a/library/Exceptions/Base64Exception.php +++ b/library/Exceptions/Base64Exception.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class Base64Exception extends ValidationException { /** @@ -16,10 +18,10 @@ final class Base64Exception extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be Base64-encoded', + Validatable::TEMPLATE_STANDARD => '{{name}} must be Base64-encoded', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be Base64-encoded', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be Base64-encoded', ], ]; } diff --git a/library/Exceptions/BaseException.php b/library/Exceptions/BaseException.php index 306d8e16..13e8d5a2 100644 --- a/library/Exceptions/BaseException.php +++ b/library/Exceptions/BaseException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class BaseException extends ValidationException { /** @@ -16,10 +18,10 @@ final class BaseException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a number in the base {{base}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a number in the base {{base}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a number in the base {{base}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a number in the base {{base}}', ], ]; } diff --git a/library/Exceptions/BetweenException.php b/library/Exceptions/BetweenException.php index 59cbd8dc..6ef6ba02 100644 --- a/library/Exceptions/BetweenException.php +++ b/library/Exceptions/BetweenException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class BetweenException extends NestedValidationException { /** @@ -16,10 +18,10 @@ final class BetweenException extends NestedValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be between {{minValue}} and {{maxValue}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be between {{minValue}} and {{maxValue}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be between {{minValue}} and {{maxValue}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be between {{minValue}} and {{maxValue}}', ], ]; } diff --git a/library/Exceptions/BoolTypeException.php b/library/Exceptions/BoolTypeException.php index 8377a98a..8d888294 100644 --- a/library/Exceptions/BoolTypeException.php +++ b/library/Exceptions/BoolTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class BoolTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class BoolTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be of type boolean', + Validatable::TEMPLATE_STANDARD => '{{name}} must be of type boolean', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be of type boolean', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be of type boolean', ], ]; } diff --git a/library/Exceptions/BoolValException.php b/library/Exceptions/BoolValException.php index 78958838..68aa85f5 100644 --- a/library/Exceptions/BoolValException.php +++ b/library/Exceptions/BoolValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class BoolValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class BoolValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a boolean value', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a boolean value', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a boolean value', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a boolean value', ], ]; } diff --git a/library/Exceptions/BsnException.php b/library/Exceptions/BsnException.php index d5626273..a860abba 100644 --- a/library/Exceptions/BsnException.php +++ b/library/Exceptions/BsnException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class BsnException extends ValidationException { /** @@ -16,10 +18,10 @@ final class BsnException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a BSN', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a BSN', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a BSN', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a BSN', ], ]; } diff --git a/library/Exceptions/CallException.php b/library/Exceptions/CallException.php index d9a4898c..b74510c4 100644 --- a/library/Exceptions/CallException.php +++ b/library/Exceptions/CallException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CallException extends NestedValidationException { /** @@ -16,10 +18,10 @@ final class CallException extends NestedValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{input}} must be valid when executed with {{callable}}', + Validatable::TEMPLATE_STANDARD => '{{input}} must be valid when executed with {{callable}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{input}} must not be valid when executed with {{callable}}', + Validatable::TEMPLATE_STANDARD => '{{input}} must not be valid when executed with {{callable}}', ], ]; } diff --git a/library/Exceptions/CallableTypeException.php b/library/Exceptions/CallableTypeException.php index 09db6a2f..1f4afa71 100644 --- a/library/Exceptions/CallableTypeException.php +++ b/library/Exceptions/CallableTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CallableTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class CallableTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be callable', + Validatable::TEMPLATE_STANDARD => '{{name}} must be callable', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be callable', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be callable', ], ]; } diff --git a/library/Exceptions/CharsetException.php b/library/Exceptions/CharsetException.php index 6db503c4..5e4ecc6c 100644 --- a/library/Exceptions/CharsetException.php +++ b/library/Exceptions/CharsetException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CharsetException extends ValidationException { /** @@ -16,10 +18,10 @@ final class CharsetException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be in the {{charset}} charset', + Validatable::TEMPLATE_STANDARD => '{{name}} must be in the {{charset}} charset', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be in the {{charset}} charset', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be in the {{charset}} charset', ], ]; } diff --git a/library/Exceptions/CnhException.php b/library/Exceptions/CnhException.php index 6e114590..03b98c44 100644 --- a/library/Exceptions/CnhException.php +++ b/library/Exceptions/CnhException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CnhException extends ValidationException { /** @@ -16,10 +18,10 @@ final class CnhException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid CNH number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid CNH number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid CNH number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid CNH number', ], ]; } diff --git a/library/Exceptions/CnpjException.php b/library/Exceptions/CnpjException.php index 07b96eeb..2037aaa1 100644 --- a/library/Exceptions/CnpjException.php +++ b/library/Exceptions/CnpjException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CnpjException extends ValidationException { /** @@ -16,10 +18,10 @@ final class CnpjException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid CNPJ number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid CNPJ number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid CNPJ number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid CNPJ number', ], ]; } diff --git a/library/Exceptions/ConsonantException.php b/library/Exceptions/ConsonantException.php index e303cabb..99a736c8 100644 --- a/library/Exceptions/ConsonantException.php +++ b/library/Exceptions/ConsonantException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule; +use Respect\Validation\Validatable; + final class ConsonantException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class ConsonantException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only consonants', - self::EXTRA => '{{name}} must contain only consonants and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only consonants', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must contain only consonants and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain consonants', - self::EXTRA => '{{name}} must not contain consonants or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain consonants', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must not contain consonants or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/ContainsAnyException.php b/library/Exceptions/ContainsAnyException.php index 7dd0e621..b98e26b7 100644 --- a/library/Exceptions/ContainsAnyException.php +++ b/library/Exceptions/ContainsAnyException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ContainsAnyException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ContainsAnyException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain at least one of the values {{needles}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain at least one of the values {{needles}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain any of the values {{needles}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain any of the values {{needles}}', ], ]; } diff --git a/library/Exceptions/ContainsException.php b/library/Exceptions/ContainsException.php index 636fce16..4191f4e6 100644 --- a/library/Exceptions/ContainsException.php +++ b/library/Exceptions/ContainsException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ContainsException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ContainsException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain the value {{containsValue}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain the value {{containsValue}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain the value {{containsValue}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain the value {{containsValue}}', ], ]; } diff --git a/library/Exceptions/ControlException.php b/library/Exceptions/ControlException.php index a906682a..640d8ab8 100644 --- a/library/Exceptions/ControlException.php +++ b/library/Exceptions/ControlException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule as Filter; +use Respect\Validation\Validatable; + final class ControlException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class ControlException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only control characters', - self::EXTRA => '{{name}} must contain only control characters and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only control characters', + Filter::TEMPLATE_EXTRA => '{{name}} must contain only control characters and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain control characters', - self::EXTRA => '{{name}} must not contain control characters or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain control characters', + Filter::TEMPLATE_EXTRA => '{{name}} must not contain control characters or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/CountableException.php b/library/Exceptions/CountableException.php index 7a259fb3..e7b71ec0 100644 --- a/library/Exceptions/CountableException.php +++ b/library/Exceptions/CountableException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CountableException extends ValidationException { /** @@ -16,10 +18,10 @@ final class CountableException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be countable', + Validatable::TEMPLATE_STANDARD => '{{name}} must be countable', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be countable', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be countable', ], ]; } diff --git a/library/Exceptions/CountryCodeException.php b/library/Exceptions/CountryCodeException.php index c131bd90..7e6dc9e5 100644 --- a/library/Exceptions/CountryCodeException.php +++ b/library/Exceptions/CountryCodeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CountryCodeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class CountryCodeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid country', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid country', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid country', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid country', ], ]; } diff --git a/library/Exceptions/CpfException.php b/library/Exceptions/CpfException.php index 12e47c3a..2ebf25ea 100644 --- a/library/Exceptions/CpfException.php +++ b/library/Exceptions/CpfException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CpfException extends ValidationException { /** @@ -16,10 +18,10 @@ final class CpfException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid CPF number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid CPF number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid CPF number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid CPF number', ], ]; } diff --git a/library/Exceptions/CreditCardException.php b/library/Exceptions/CreditCardException.php index 971a4e93..a5b89c38 100644 --- a/library/Exceptions/CreditCardException.php +++ b/library/Exceptions/CreditCardException.php @@ -10,31 +10,21 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; use Respect\Validation\Rules\CreditCard; +use Respect\Validation\Validatable; final class CreditCardException extends ValidationException { - public const BRANDED = 'branded'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid Credit Card number', - self::BRANDED => '{{name}} must be a valid {{brand}} Credit Card number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid Credit Card number', + CreditCard::TEMPLATE_BRANDED => '{{name}} must be a valid {{brand}} Credit Card number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid Credit Card number', - self::BRANDED => '{{name}} must not be a valid {{brand}} Credit Card number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid Credit Card number', + CreditCard::TEMPLATE_BRANDED => '{{name}} must not be a valid {{brand}} Credit Card number', ], ]; - - protected function chooseTemplate(): string - { - if ($this->getParam('brand') === CreditCard::ANY) { - return self::STANDARD; - } - - return self::BRANDED; - } } diff --git a/library/Exceptions/CurrencyCodeException.php b/library/Exceptions/CurrencyCodeException.php index 6310ed43..36ee5f18 100644 --- a/library/Exceptions/CurrencyCodeException.php +++ b/library/Exceptions/CurrencyCodeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class CurrencyCodeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class CurrencyCodeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid currency', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid currency', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid currency', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid currency', ], ]; } diff --git a/library/Exceptions/DateException.php b/library/Exceptions/DateException.php index bbe56567..158dad06 100644 --- a/library/Exceptions/DateException.php +++ b/library/Exceptions/DateException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class DateException extends ValidationException { /** @@ -16,10 +18,10 @@ final class DateException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid date in the format {{sample}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid date in the format {{sample}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid date in the format {{sample}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid date in the format {{sample}}', ], ]; } diff --git a/library/Exceptions/DateTimeException.php b/library/Exceptions/DateTimeException.php index a41a0738..b3be71c1 100644 --- a/library/Exceptions/DateTimeException.php +++ b/library/Exceptions/DateTimeException.php @@ -9,26 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\DateTime; +use Respect\Validation\Validatable; + final class DateTimeException extends ValidationException { - public const FORMAT = 'format'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid date/time', - self::FORMAT => '{{name}} must be a valid date/time in the format {{sample}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid date/time', + DateTime::TEMPLATE_FORMAT => '{{name}} must be a valid date/time in the format {{sample}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid date/time', - self::FORMAT => '{{name}} must not be a valid date/time in the format {{sample}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid date/time', + DateTime::TEMPLATE_FORMAT => '{{name}} must not be a valid date/time in the format {{sample}}', ], ]; - - protected function chooseTemplate(): string - { - return $this->getParam('format') ? self::FORMAT : self::STANDARD; - } } diff --git a/library/Exceptions/DecimalException.php b/library/Exceptions/DecimalException.php index abd872de..b1d0abd9 100644 --- a/library/Exceptions/DecimalException.php +++ b/library/Exceptions/DecimalException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class DecimalException extends ValidationException { /** @@ -16,10 +18,10 @@ final class DecimalException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must have {{decimals}} decimals', + Validatable::TEMPLATE_STANDARD => '{{name}} must have {{decimals}} decimals', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not have {{decimals}} decimals', + Validatable::TEMPLATE_STANDARD => '{{name}} must not have {{decimals}} decimals', ], ]; } diff --git a/library/Exceptions/DigitException.php b/library/Exceptions/DigitException.php index c2ca2247..76c88191 100644 --- a/library/Exceptions/DigitException.php +++ b/library/Exceptions/DigitException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule; +use Respect\Validation\Validatable; + final class DigitException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class DigitException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only digits (0-9)', - self::EXTRA => '{{name}} must contain only digits (0-9) and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only digits (0-9)', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must contain only digits (0-9) and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain digits (0-9)', - self::EXTRA => '{{name}} must not contain digits (0-9) and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain digits (0-9)', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must not contain digits (0-9) and {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/DirectoryException.php b/library/Exceptions/DirectoryException.php index 70014bf5..cb0e2bf9 100644 --- a/library/Exceptions/DirectoryException.php +++ b/library/Exceptions/DirectoryException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class DirectoryException extends ValidationException { /** @@ -16,10 +18,10 @@ final class DirectoryException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a directory', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a directory', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a directory', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a directory', ], ]; } diff --git a/library/Exceptions/DomainException.php b/library/Exceptions/DomainException.php index 070debbf..7c22abc3 100644 --- a/library/Exceptions/DomainException.php +++ b/library/Exceptions/DomainException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class DomainException extends NestedValidationException { /** @@ -16,10 +18,10 @@ final class DomainException extends NestedValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid domain', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid domain', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid domain', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid domain', ], ]; } diff --git a/library/Exceptions/EachException.php b/library/Exceptions/EachException.php index 50538be5..6d04f7a2 100644 --- a/library/Exceptions/EachException.php +++ b/library/Exceptions/EachException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class EachException extends NestedValidationException { /** @@ -16,10 +18,10 @@ final class EachException extends NestedValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'Each item in {{name}} must be valid', + Validatable::TEMPLATE_STANDARD => 'Each item in {{name}} must be valid', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'Each item in {{name}} must not validate', + Validatable::TEMPLATE_STANDARD => 'Each item in {{name}} must not validate', ], ]; diff --git a/library/Exceptions/EmailException.php b/library/Exceptions/EmailException.php index 657661d2..94f45b6a 100644 --- a/library/Exceptions/EmailException.php +++ b/library/Exceptions/EmailException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class EmailException extends ValidationException { /** @@ -16,10 +18,10 @@ final class EmailException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be valid email', + Validatable::TEMPLATE_STANDARD => '{{name}} must be valid email', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an email', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an email', ], ]; } diff --git a/library/Exceptions/EndsWithException.php b/library/Exceptions/EndsWithException.php index 257e2b38..ebd05522 100644 --- a/library/Exceptions/EndsWithException.php +++ b/library/Exceptions/EndsWithException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class EndsWithException extends ValidationException { /** @@ -16,10 +18,10 @@ final class EndsWithException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must end with {{endValue}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must end with {{endValue}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not end with {{endValue}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not end with {{endValue}}', ], ]; } diff --git a/library/Exceptions/EqualsException.php b/library/Exceptions/EqualsException.php index 46efc4ae..5d79bc0d 100644 --- a/library/Exceptions/EqualsException.php +++ b/library/Exceptions/EqualsException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class EqualsException extends ValidationException { /** @@ -16,10 +18,10 @@ final class EqualsException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must equal {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must equal {{compareTo}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not equal {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not equal {{compareTo}}', ], ]; } diff --git a/library/Exceptions/EquivalentException.php b/library/Exceptions/EquivalentException.php index a46a0a53..094bacb8 100644 --- a/library/Exceptions/EquivalentException.php +++ b/library/Exceptions/EquivalentException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class EquivalentException extends ValidationException { /** @@ -16,10 +18,10 @@ final class EquivalentException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be equivalent to {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be equivalent to {{compareTo}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be equivalent to {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be equivalent to {{compareTo}}', ], ]; } diff --git a/library/Exceptions/EvenException.php b/library/Exceptions/EvenException.php index 8f4254a0..45cb645f 100644 --- a/library/Exceptions/EvenException.php +++ b/library/Exceptions/EvenException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class EvenException extends ValidationException { /** @@ -16,10 +18,10 @@ final class EvenException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an even number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an even number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an even number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an even number', ], ]; } diff --git a/library/Exceptions/ExecutableException.php b/library/Exceptions/ExecutableException.php index da48cdd9..b360f3bc 100644 --- a/library/Exceptions/ExecutableException.php +++ b/library/Exceptions/ExecutableException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ExecutableException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ExecutableException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an executable file', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an executable file', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an executable file', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an executable file', ], ]; } diff --git a/library/Exceptions/ExistsException.php b/library/Exceptions/ExistsException.php index 2e7e7ac2..e3877c84 100644 --- a/library/Exceptions/ExistsException.php +++ b/library/Exceptions/ExistsException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ExistsException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ExistsException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must exist', + Validatable::TEMPLATE_STANDARD => '{{name}} must exist', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not exist', + Validatable::TEMPLATE_STANDARD => '{{name}} must not exist', ], ]; } diff --git a/library/Exceptions/ExtensionException.php b/library/Exceptions/ExtensionException.php index 9dbf80de..4a3d73a5 100644 --- a/library/Exceptions/ExtensionException.php +++ b/library/Exceptions/ExtensionException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ExtensionException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ExtensionException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must have {{extension}} extension', + Validatable::TEMPLATE_STANDARD => '{{name}} must have {{extension}} extension', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not have {{extension}} extension', + Validatable::TEMPLATE_STANDARD => '{{name}} must not have {{extension}} extension', ], ]; } diff --git a/library/Exceptions/FactorException.php b/library/Exceptions/FactorException.php index b9810c44..66263413 100644 --- a/library/Exceptions/FactorException.php +++ b/library/Exceptions/FactorException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class FactorException extends ValidationException { /** @@ -16,10 +18,10 @@ final class FactorException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a factor of {{dividend}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a factor of {{dividend}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a factor of {{dividend}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a factor of {{dividend}}', ], ]; } diff --git a/library/Exceptions/FalseValException.php b/library/Exceptions/FalseValException.php index d481c691..6899c65f 100644 --- a/library/Exceptions/FalseValException.php +++ b/library/Exceptions/FalseValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class FalseValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class FalseValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must evaluate to `false`', + Validatable::TEMPLATE_STANDARD => '{{name}} must evaluate to `false`', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not evaluate to `false`', + Validatable::TEMPLATE_STANDARD => '{{name}} must not evaluate to `false`', ], ]; } diff --git a/library/Exceptions/FibonacciException.php b/library/Exceptions/FibonacciException.php index fdccfc04..65db6807 100644 --- a/library/Exceptions/FibonacciException.php +++ b/library/Exceptions/FibonacciException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class FibonacciException extends ValidationException { /** @@ -16,10 +18,10 @@ final class FibonacciException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid Fibonacci number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid Fibonacci number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid Fibonacci number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid Fibonacci number', ], ]; } diff --git a/library/Exceptions/FileException.php b/library/Exceptions/FileException.php index 702e42e8..3683474b 100644 --- a/library/Exceptions/FileException.php +++ b/library/Exceptions/FileException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class FileException extends ValidationException { /** @@ -16,10 +18,10 @@ final class FileException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a file', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a file', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a file', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a file', ], ]; } diff --git a/library/Exceptions/FilteredValidationException.php b/library/Exceptions/FilteredValidationException.php index 7ef05f50..7cc6dcf5 100644 --- a/library/Exceptions/FilteredValidationException.php +++ b/library/Exceptions/FilteredValidationException.php @@ -11,10 +11,4 @@ namespace Respect\Validation\Exceptions; class FilteredValidationException extends ValidationException { - public const EXTRA = 'extra'; - - protected function chooseTemplate(): string - { - return $this->getParam('additionalChars') ? self::EXTRA : self::STANDARD; - } } diff --git a/library/Exceptions/FiniteException.php b/library/Exceptions/FiniteException.php index e52e705b..31b22738 100644 --- a/library/Exceptions/FiniteException.php +++ b/library/Exceptions/FiniteException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class FiniteException extends ValidationException { /** @@ -16,10 +18,10 @@ final class FiniteException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a finite number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a finite number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a finite number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a finite number', ], ]; } diff --git a/library/Exceptions/FloatTypeException.php b/library/Exceptions/FloatTypeException.php index ed0023a2..026862cb 100644 --- a/library/Exceptions/FloatTypeException.php +++ b/library/Exceptions/FloatTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class FloatTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class FloatTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be of type float', + Validatable::TEMPLATE_STANDARD => '{{name}} must be of type float', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be of type float', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be of type float', ], ]; } diff --git a/library/Exceptions/FloatValException.php b/library/Exceptions/FloatValException.php index 1f803185..4db516a4 100644 --- a/library/Exceptions/FloatValException.php +++ b/library/Exceptions/FloatValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class FloatValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class FloatValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a float number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a float number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a float number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a float number', ], ]; } diff --git a/library/Exceptions/GraphException.php b/library/Exceptions/GraphException.php index 14ba2856..e03887b9 100644 --- a/library/Exceptions/GraphException.php +++ b/library/Exceptions/GraphException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule as Filter; +use Respect\Validation\Validatable; + final class GraphException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class GraphException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only graphical characters', - self::EXTRA => '{{name}} must contain only graphical characters and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only graphical characters', + Filter::TEMPLATE_EXTRA => '{{name}} must contain only graphical characters and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain graphical characters', - self::EXTRA => '{{name}} must not contain graphical characters or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain graphical characters', + Filter::TEMPLATE_EXTRA => '{{name}} must not contain graphical characters or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/GreaterThanException.php b/library/Exceptions/GreaterThanException.php index be117c31..910eb56d 100644 --- a/library/Exceptions/GreaterThanException.php +++ b/library/Exceptions/GreaterThanException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class GreaterThanException extends ValidationException { /** @@ -16,10 +18,10 @@ final class GreaterThanException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be greater than {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be greater than {{compareTo}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be greater than {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be greater than {{compareTo}}', ], ]; } diff --git a/library/Exceptions/HexRgbColorException.php b/library/Exceptions/HexRgbColorException.php index af0ccbad..8732d92f 100644 --- a/library/Exceptions/HexRgbColorException.php +++ b/library/Exceptions/HexRgbColorException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class HexRgbColorException extends ValidationException { /** @@ -16,10 +18,10 @@ final class HexRgbColorException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a hex RGB color', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a hex RGB color', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a hex RGB color', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a hex RGB color', ], ]; } diff --git a/library/Exceptions/IbanException.php b/library/Exceptions/IbanException.php index 62f74f81..7ded2e32 100644 --- a/library/Exceptions/IbanException.php +++ b/library/Exceptions/IbanException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class IbanException extends ValidationException { /** @@ -16,10 +18,10 @@ final class IbanException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid IBAN', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid IBAN', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid IBAN', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid IBAN', ], ]; } diff --git a/library/Exceptions/IdenticalException.php b/library/Exceptions/IdenticalException.php index e98d8d5e..7773d1b3 100644 --- a/library/Exceptions/IdenticalException.php +++ b/library/Exceptions/IdenticalException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class IdenticalException extends ValidationException { /** @@ -16,10 +18,10 @@ final class IdenticalException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be identical as {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be identical as {{compareTo}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be identical as {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be identical as {{compareTo}}', ], ]; } diff --git a/library/Exceptions/ImageException.php b/library/Exceptions/ImageException.php index 7e1e83a8..18363f45 100644 --- a/library/Exceptions/ImageException.php +++ b/library/Exceptions/ImageException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ImageException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ImageException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid image', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid image', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid image', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid image', ], ]; } diff --git a/library/Exceptions/ImeiException.php b/library/Exceptions/ImeiException.php index 0b31f079..0928f082 100644 --- a/library/Exceptions/ImeiException.php +++ b/library/Exceptions/ImeiException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ImeiException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ImeiException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid IMEI', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid IMEI', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid IMEI', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid IMEI', ], ]; } diff --git a/library/Exceptions/InException.php b/library/Exceptions/InException.php index a5bc6757..5f3ad0a7 100644 --- a/library/Exceptions/InException.php +++ b/library/Exceptions/InException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class InException extends ValidationException { /** @@ -16,10 +18,10 @@ final class InException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be in {{haystack}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be in {{haystack}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be in {{haystack}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be in {{haystack}}', ], ]; } diff --git a/library/Exceptions/InfiniteException.php b/library/Exceptions/InfiniteException.php index e2871086..ad8cca2f 100644 --- a/library/Exceptions/InfiniteException.php +++ b/library/Exceptions/InfiniteException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class InfiniteException extends ValidationException { /** @@ -16,10 +18,10 @@ final class InfiniteException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an infinite number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an infinite number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an infinite number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an infinite number', ], ]; } diff --git a/library/Exceptions/InstanceException.php b/library/Exceptions/InstanceException.php index dda36b64..4fbf276a 100644 --- a/library/Exceptions/InstanceException.php +++ b/library/Exceptions/InstanceException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class InstanceException extends ValidationException { /** @@ -16,10 +18,10 @@ final class InstanceException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an instance of {{instanceName}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an instance of {{instanceName}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an instance of {{instanceName}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an instance of {{instanceName}}', ], ]; } diff --git a/library/Exceptions/IntTypeException.php b/library/Exceptions/IntTypeException.php index ab254c67..10739e5a 100644 --- a/library/Exceptions/IntTypeException.php +++ b/library/Exceptions/IntTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class IntTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class IntTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be of type integer', + Validatable::TEMPLATE_STANDARD => '{{name}} must be of type integer', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be of type integer', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be of type integer', ], ]; } diff --git a/library/Exceptions/IntValException.php b/library/Exceptions/IntValException.php index 9df50cda..dd901300 100644 --- a/library/Exceptions/IntValException.php +++ b/library/Exceptions/IntValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class IntValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class IntValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an integer number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an integer number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an integer number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an integer number', ], ]; } diff --git a/library/Exceptions/IpException.php b/library/Exceptions/IpException.php index 2e4890cb..4b09fe2f 100644 --- a/library/Exceptions/IpException.php +++ b/library/Exceptions/IpException.php @@ -9,30 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Ip; +use Respect\Validation\Validatable; + final class IpException extends ValidationException { - public const NETWORK_RANGE = 'network_range'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an IP address', - self::NETWORK_RANGE => '{{name}} must be an IP address in the {{range}} range', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an IP address', + Ip::TEMPLATE_NETWORK_RANGE => '{{name}} must be an IP address in the {{range}} range', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an IP address', - self::NETWORK_RANGE => '{{name}} must not be an IP address in the {{range}} range', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an IP address', + Ip::TEMPLATE_NETWORK_RANGE => '{{name}} must not be an IP address in the {{range}} range', ], ]; - - protected function chooseTemplate(): string - { - if (!$this->getParam('range')) { - return self::STANDARD; - } - - return self::NETWORK_RANGE; - } } diff --git a/library/Exceptions/IsbnException.php b/library/Exceptions/IsbnException.php index b1e53f93..e066ca5c 100644 --- a/library/Exceptions/IsbnException.php +++ b/library/Exceptions/IsbnException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class IsbnException extends ValidationException { /** @@ -16,10 +18,10 @@ final class IsbnException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a ISBN', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a ISBN', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a ISBN', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a ISBN', ], ]; } diff --git a/library/Exceptions/IterableTypeException.php b/library/Exceptions/IterableTypeException.php index 47ba6859..6d638273 100644 --- a/library/Exceptions/IterableTypeException.php +++ b/library/Exceptions/IterableTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class IterableTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class IterableTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be iterable', + Validatable::TEMPLATE_STANDARD => '{{name}} must be iterable', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be iterable', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be iterable', ], ]; } diff --git a/library/Exceptions/JsonException.php b/library/Exceptions/JsonException.php index 706ada68..9a34236a 100644 --- a/library/Exceptions/JsonException.php +++ b/library/Exceptions/JsonException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class JsonException extends ValidationException { /** @@ -16,10 +18,10 @@ final class JsonException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid JSON string', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid JSON string', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid JSON string', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid JSON string', ], ]; } diff --git a/library/Exceptions/KeyException.php b/library/Exceptions/KeyException.php index e8ff5a4e..e64633e5 100644 --- a/library/Exceptions/KeyException.php +++ b/library/Exceptions/KeyException.php @@ -9,27 +9,21 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Key; + final class KeyException extends NestedValidationException implements NonOmissibleException { - public const NOT_PRESENT = 'not_present'; - public const INVALID = 'invalid'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::NOT_PRESENT => '{{name}} must be present', - self::INVALID => '{{name}} must be valid', + Key::TEMPLATE_NOT_PRESENT => '{{name}} must be present', + Key::TEMPLATE_INVALID => '{{name}} must be valid', ], self::MODE_NEGATIVE => [ - self::NOT_PRESENT => '{{name}} must not be present', - self::INVALID => '{{name}} must not be valid', + Key::TEMPLATE_NOT_PRESENT => '{{name}} must not be present', + Key::TEMPLATE_INVALID => '{{name}} must not be valid', ], ]; - - protected function chooseTemplate(): string - { - return $this->getParam('hasReference') ? self::INVALID : self::NOT_PRESENT; - } } diff --git a/library/Exceptions/KeyNestedException.php b/library/Exceptions/KeyNestedException.php index cfd702ad..a868c1c8 100644 --- a/library/Exceptions/KeyNestedException.php +++ b/library/Exceptions/KeyNestedException.php @@ -9,27 +9,21 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\KeyNested; + final class KeyNestedException extends NestedValidationException implements NonOmissibleException { - public const NOT_PRESENT = 'not_present'; - public const INVALID = 'invalid'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::NOT_PRESENT => 'No items were found for key chain {{name}}', - self::INVALID => 'Key chain {{name}} is not valid', + KeyNested::TEMPLATE_NOT_PRESENT => 'No items were found for key chain {{name}}', + KeyNested::TEMPLATE_INVALID => 'Key chain {{name}} is not valid', ], self::MODE_NEGATIVE => [ - self::NOT_PRESENT => 'Items for key chain {{name}} must not be present', - self::INVALID => 'Key chain {{name}} must not be valid', + KeyNested::TEMPLATE_NOT_PRESENT => 'Items for key chain {{name}} must not be present', + KeyNested::TEMPLATE_INVALID => 'Key chain {{name}} must not be valid', ], ]; - - protected function chooseTemplate(): string - { - return $this->getParam('hasReference') ? self::INVALID : self::NOT_PRESENT; - } } diff --git a/library/Exceptions/KeySetException.php b/library/Exceptions/KeySetException.php index cab48eba..d6700558 100644 --- a/library/Exceptions/KeySetException.php +++ b/library/Exceptions/KeySetException.php @@ -9,35 +9,19 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; -use function count; +use Respect\Validation\Rules\KeySet; final class KeySetException extends GroupedValidationException implements NonOmissibleException { - public const STRUCTURE = 'structure'; - public const STRUCTURE_EXTRA = 'structure_extra'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::NONE => 'All of the required rules must pass for {{name}}', - self::SOME => 'These rules must pass for {{name}}', - self::STRUCTURE => 'Must have keys {{keys}}', - self::STRUCTURE_EXTRA => 'Must not have keys {{extraKeys}}', + KeySet::TEMPLATE_NONE => 'All of the required rules must pass for {{name}}', + KeySet::TEMPLATE_SOME => 'These rules must pass for {{name}}', + KeySet::TEMPLATE_STRUCTURE => 'Must have keys {{keys}}', + KeySet::TEMPLATE_STRUCTURE_EXTRA => 'Must not have keys {{extraKeys}}', ], ]; - - protected function chooseTemplate(): string - { - if (count($this->getParam('extraKeys'))) { - return self::STRUCTURE_EXTRA; - } - - if (count($this->getChildren()) === 0) { - return self::STRUCTURE; - } - - return parent::chooseTemplate(); - } } diff --git a/library/Exceptions/KeyValueException.php b/library/Exceptions/KeyValueException.php index 85a07f46..72296e32 100644 --- a/library/Exceptions/KeyValueException.php +++ b/library/Exceptions/KeyValueException.php @@ -9,26 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\KeyValue; +use Respect\Validation\Validatable; + final class KeyValueException extends ValidationException { - public const COMPONENT = 'component'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'Key {{name}} must be present', - self::COMPONENT => '{{baseKey}} must be valid to validate {{comparedKey}}', + Validatable::TEMPLATE_STANDARD => 'Key {{name}} must be present', + KeyValue::TEMPLATE_COMPONENT => '{{baseKey}} must be valid to validate {{comparedKey}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'Key {{name}} must not be present', - self::COMPONENT => '{{baseKey}} must not be valid to validate {{comparedKey}}', + Validatable::TEMPLATE_STANDARD => 'Key {{name}} must not be present', + KeyValue::TEMPLATE_COMPONENT => '{{baseKey}} must not be valid to validate {{comparedKey}}', ], ]; - - protected function chooseTemplate(): string - { - return $this->getParam('component') ? self::COMPONENT : self::STANDARD; - } } diff --git a/library/Exceptions/LanguageCodeException.php b/library/Exceptions/LanguageCodeException.php index fa813bbc..7924fcb6 100644 --- a/library/Exceptions/LanguageCodeException.php +++ b/library/Exceptions/LanguageCodeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class LanguageCodeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class LanguageCodeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid ISO 639 {{set}} language code', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid ISO 639 {{set}} language code', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid ISO 639 {{set}} language code', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid ISO 639 {{set}} language code', ], ]; } diff --git a/library/Exceptions/LeapDateException.php b/library/Exceptions/LeapDateException.php index c85a2e35..8de0466a 100644 --- a/library/Exceptions/LeapDateException.php +++ b/library/Exceptions/LeapDateException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class LeapDateException extends ValidationException { /** @@ -16,10 +18,10 @@ final class LeapDateException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be leap date', + Validatable::TEMPLATE_STANDARD => '{{name}} must be leap date', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be leap date', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be leap date', ], ]; } diff --git a/library/Exceptions/LeapYearException.php b/library/Exceptions/LeapYearException.php index 709b7d75..cf9295d3 100644 --- a/library/Exceptions/LeapYearException.php +++ b/library/Exceptions/LeapYearException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class LeapYearException extends ValidationException { /** @@ -16,10 +18,10 @@ final class LeapYearException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a leap year', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a leap year', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a leap year', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a leap year', ], ]; } diff --git a/library/Exceptions/LengthException.php b/library/Exceptions/LengthException.php index 66b0beb5..9c8db960 100644 --- a/library/Exceptions/LengthException.php +++ b/library/Exceptions/LengthException.php @@ -9,53 +9,29 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Length; + final class LengthException extends ValidationException { - public const BOTH = 'both'; - public const LOWER = 'lower'; - public const LOWER_INCLUSIVE = 'lower_inclusive'; - public const GREATER = 'greater'; - public const GREATER_INCLUSIVE = 'greater_inclusive'; - public const EXACT = 'exact'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::BOTH => '{{name}} must have a length between {{minValue}} and {{maxValue}}', - self::LOWER => '{{name}} must have a length greater than {{minValue}}', - self::LOWER_INCLUSIVE => '{{name}} must have a length greater than or equal to {{minValue}}', - self::GREATER => '{{name}} must have a length lower than {{maxValue}}', - self::GREATER_INCLUSIVE => '{{name}} must have a length lower than or equal to {{maxValue}}', - self::EXACT => '{{name}} must have a length of {{maxValue}}', + Length::TEMPLATE_BOTH => '{{name}} must have a length between {{minValue}} and {{maxValue}}', + Length::TEMPLATE_LOWER => '{{name}} must have a length greater than {{minValue}}', + Length::TEMPLATE_LOWER_INCLUSIVE => '{{name}} must have a length greater than or equal to {{minValue}}', + Length::TEMPLATE_GREATER => '{{name}} must have a length lower than {{maxValue}}', + Length::TEMPLATE_GREATER_INCLUSIVE => '{{name}} must have a length lower than or equal to {{maxValue}}', + Length::TEMPLATE_EXACT => '{{name}} must have a length of {{maxValue}}', ], self::MODE_NEGATIVE => [ - self::BOTH => '{{name}} must not have a length between {{minValue}} and {{maxValue}}', - self::LOWER => '{{name}} must not have a length greater than {{minValue}}', - self::LOWER_INCLUSIVE => '{{name}} must not have a length greater than or equal to {{minValue}}', - self::GREATER => '{{name}} must not have a length lower than {{maxValue}}', - self::GREATER_INCLUSIVE => '{{name}} must not have a length lower than or equal to {{maxValue}}', - self::EXACT => '{{name}} must not have a length of {{maxValue}}', + Length::TEMPLATE_BOTH => '{{name}} must not have a length between {{minValue}} and {{maxValue}}', + Length::TEMPLATE_LOWER => '{{name}} must not have a length greater than {{minValue}}', + Length::TEMPLATE_LOWER_INCLUSIVE => '{{name}} must not have a length greater than or equal to {{minValue}}', + Length::TEMPLATE_GREATER => '{{name}} must not have a length lower than {{maxValue}}', + Length::TEMPLATE_GREATER_INCLUSIVE => '{{name}} must not have a length lower than or equal to {{maxValue}}', + Length::TEMPLATE_EXACT => '{{name}} must not have a length of {{maxValue}}', ], ]; - - protected function chooseTemplate(): string - { - $isInclusive = $this->getParam('inclusive'); - - if (!$this->getParam('minValue')) { - return $isInclusive === true ? self::GREATER_INCLUSIVE : self::GREATER; - } - - if (!$this->getParam('maxValue')) { - return $isInclusive === true ? self::LOWER_INCLUSIVE : self::LOWER; - } - - if ($this->getParam('minValue') == $this->getParam('maxValue')) { - return self::EXACT; - } - - return self::BOTH; - } } diff --git a/library/Exceptions/LessThanException.php b/library/Exceptions/LessThanException.php index 6c5e72fc..bfe12d22 100644 --- a/library/Exceptions/LessThanException.php +++ b/library/Exceptions/LessThanException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class LessThanException extends ValidationException { /** @@ -16,10 +18,10 @@ final class LessThanException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be less than {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be less than {{compareTo}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be less than {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be less than {{compareTo}}', ], ]; } diff --git a/library/Exceptions/LowercaseException.php b/library/Exceptions/LowercaseException.php index 972e42f1..c3963186 100644 --- a/library/Exceptions/LowercaseException.php +++ b/library/Exceptions/LowercaseException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class LowercaseException extends ValidationException { /** @@ -16,10 +18,10 @@ final class LowercaseException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be lowercase', + Validatable::TEMPLATE_STANDARD => '{{name}} must be lowercase', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be lowercase', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be lowercase', ], ]; } diff --git a/library/Exceptions/LuhnException.php b/library/Exceptions/LuhnException.php index 31ba4a3a..ed103c28 100644 --- a/library/Exceptions/LuhnException.php +++ b/library/Exceptions/LuhnException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class LuhnException extends ValidationException { /** @@ -16,10 +18,10 @@ final class LuhnException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid Luhn number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid Luhn number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid Luhn number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid Luhn number', ], ]; } diff --git a/library/Exceptions/MacAddressException.php b/library/Exceptions/MacAddressException.php index 3d4f1855..f6a7359a 100644 --- a/library/Exceptions/MacAddressException.php +++ b/library/Exceptions/MacAddressException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class MacAddressException extends ValidationException { /** @@ -16,10 +18,10 @@ final class MacAddressException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid MAC address', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid MAC address', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid MAC address', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid MAC address', ], ]; } diff --git a/library/Exceptions/MaxAgeException.php b/library/Exceptions/MaxAgeException.php index e3e5d59d..b2d45184 100644 --- a/library/Exceptions/MaxAgeException.php +++ b/library/Exceptions/MaxAgeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class MaxAgeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class MaxAgeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be {{age}} years or less', + Validatable::TEMPLATE_STANDARD => '{{name}} must be {{age}} years or less', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be {{age}} years or less', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be {{age}} years or less', ], ]; } diff --git a/library/Exceptions/MaxException.php b/library/Exceptions/MaxException.php index 54e7e5db..8f1fdbcf 100644 --- a/library/Exceptions/MaxException.php +++ b/library/Exceptions/MaxException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class MaxException extends ValidationException { /** @@ -16,10 +18,10 @@ final class MaxException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be less than or equal to {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be less than or equal to {{compareTo}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be less than or equal to {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be less than or equal to {{compareTo}}', ], ]; } diff --git a/library/Exceptions/MimetypeException.php b/library/Exceptions/MimetypeException.php index 2e4593ab..354ea2ab 100644 --- a/library/Exceptions/MimetypeException.php +++ b/library/Exceptions/MimetypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class MimetypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class MimetypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must have {{mimetype}} MIME type', + Validatable::TEMPLATE_STANDARD => '{{name}} must have {{mimetype}} MIME type', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not have {{mimetype}} MIME type', + Validatable::TEMPLATE_STANDARD => '{{name}} must not have {{mimetype}} MIME type', ], ]; } diff --git a/library/Exceptions/MinAgeException.php b/library/Exceptions/MinAgeException.php index e5b302cf..698cb758 100644 --- a/library/Exceptions/MinAgeException.php +++ b/library/Exceptions/MinAgeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class MinAgeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class MinAgeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be {{age}} years or more', + Validatable::TEMPLATE_STANDARD => '{{name}} must be {{age}} years or more', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be {{age}} years or more', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be {{age}} years or more', ], ]; } diff --git a/library/Exceptions/MinException.php b/library/Exceptions/MinException.php index b48018c9..9fea0c31 100644 --- a/library/Exceptions/MinException.php +++ b/library/Exceptions/MinException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class MinException extends ValidationException { public const INCLUSIVE = 'inclusive'; @@ -18,10 +20,10 @@ final class MinException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be greater than or equal to {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be greater than or equal to {{compareTo}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be greater than or equal to {{compareTo}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be greater than or equal to {{compareTo}}', ], ]; } diff --git a/library/Exceptions/MultipleException.php b/library/Exceptions/MultipleException.php index ba98a9ba..724106d3 100644 --- a/library/Exceptions/MultipleException.php +++ b/library/Exceptions/MultipleException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class MultipleException extends ValidationException { /** @@ -16,10 +18,10 @@ final class MultipleException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be multiple of {{multipleOf}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be multiple of {{multipleOf}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be multiple of {{multipleOf}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be multiple of {{multipleOf}}', ], ]; } diff --git a/library/Exceptions/NegativeException.php b/library/Exceptions/NegativeException.php index b14a21e0..0ab2ca38 100644 --- a/library/Exceptions/NegativeException.php +++ b/library/Exceptions/NegativeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NegativeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NegativeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be negative', + Validatable::TEMPLATE_STANDARD => '{{name}} must be negative', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be negative', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be negative', ], ]; } diff --git a/library/Exceptions/NfeAccessKeyException.php b/library/Exceptions/NfeAccessKeyException.php index f035bb1d..b95beb5c 100644 --- a/library/Exceptions/NfeAccessKeyException.php +++ b/library/Exceptions/NfeAccessKeyException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NfeAccessKeyException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NfeAccessKeyException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid NFe access key', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid NFe access key', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid NFe access key', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid NFe access key', ], ]; } diff --git a/library/Exceptions/NifException.php b/library/Exceptions/NifException.php index 5ebe3817..2e616966 100644 --- a/library/Exceptions/NifException.php +++ b/library/Exceptions/NifException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NifException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NifException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a NIF', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a NIF', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a NIF', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a NIF', ], ]; } diff --git a/library/Exceptions/NipException.php b/library/Exceptions/NipException.php index 5d0fa139..467d9344 100644 --- a/library/Exceptions/NipException.php +++ b/library/Exceptions/NipException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NipException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NipException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid Polish VAT identification number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid Polish VAT identification number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid Polish VAT identification number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid Polish VAT identification number', ], ]; } diff --git a/library/Exceptions/NoException.php b/library/Exceptions/NoException.php index a9d0bd6d..dbadc182 100644 --- a/library/Exceptions/NoException.php +++ b/library/Exceptions/NoException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NoException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NoException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be similar to "No"', + Validatable::TEMPLATE_STANDARD => '{{name}} must be similar to "No"', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be similar to "No"', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be similar to "No"', ], ]; } diff --git a/library/Exceptions/NoWhitespaceException.php b/library/Exceptions/NoWhitespaceException.php index 53f5d60f..3de5271f 100644 --- a/library/Exceptions/NoWhitespaceException.php +++ b/library/Exceptions/NoWhitespaceException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NoWhitespaceException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NoWhitespaceException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must not contain whitespace', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain whitespace', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must contain whitespace', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain whitespace', ], ]; } diff --git a/library/Exceptions/NoneOfException.php b/library/Exceptions/NoneOfException.php index 1343bcdf..13e1e3db 100644 --- a/library/Exceptions/NoneOfException.php +++ b/library/Exceptions/NoneOfException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NoneOfException extends NestedValidationException { /** @@ -16,10 +18,10 @@ final class NoneOfException extends NestedValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'None of these rules must pass for {{name}}', + Validatable::TEMPLATE_STANDARD => 'None of these rules must pass for {{name}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'All of these rules must pass for {{name}}', + Validatable::TEMPLATE_STANDARD => 'All of these rules must pass for {{name}}', ], ]; } diff --git a/library/Exceptions/NotBlankException.php b/library/Exceptions/NotBlankException.php index 416635dd..8b5f564b 100644 --- a/library/Exceptions/NotBlankException.php +++ b/library/Exceptions/NotBlankException.php @@ -9,30 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\NotBlank; +use Respect\Validation\Validatable; + final class NotBlankException extends ValidationException { - public const NAMED = 'named'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'The value must not be blank', - self::NAMED => '{{name}} must not be blank', + Validatable::TEMPLATE_STANDARD => 'The value must not be blank', + NotBlank::TEMPLATE_NAMED => '{{name}} must not be blank', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'The value must be blank', - self::NAMED => '{{name}} must be blank', + Validatable::TEMPLATE_STANDARD => 'The value must be blank', + NotBlank::TEMPLATE_NAMED => '{{name}} must be blank', ], ]; - - protected function chooseTemplate(): string - { - if ($this->getParam('input') || $this->getParam('name')) { - return self::NAMED; - } - - return self::STANDARD; - } } diff --git a/library/Exceptions/NotEmojiException.php b/library/Exceptions/NotEmojiException.php index cbd4f701..6c61fc0c 100644 --- a/library/Exceptions/NotEmojiException.php +++ b/library/Exceptions/NotEmojiException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NotEmojiException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NotEmojiException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must not contain an Emoji', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain an Emoji', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must contain an Emoji', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain an Emoji', ], ]; } diff --git a/library/Exceptions/NotEmptyException.php b/library/Exceptions/NotEmptyException.php index 15b2fed4..ab5619c1 100644 --- a/library/Exceptions/NotEmptyException.php +++ b/library/Exceptions/NotEmptyException.php @@ -9,30 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\NotEmpty; +use Respect\Validation\Validatable; + final class NotEmptyException extends ValidationException { - public const NAMED = 'named'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'The value must not be empty', - self::NAMED => '{{name}} must not be empty', + Validatable::TEMPLATE_STANDARD => 'The value must not be empty', + NotEmpty::TEMPLATE_NAMED => '{{name}} must not be empty', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'The value must be empty', - self::NAMED => '{{name}} must be empty', + Validatable::TEMPLATE_STANDARD => 'The value must be empty', + NotEmpty::TEMPLATE_NAMED => '{{name}} must be empty', ], ]; - - protected function chooseTemplate(): string - { - if ($this->getParam('input') || $this->getParam('name')) { - return self::NAMED; - } - - return self::STANDARD; - } } diff --git a/library/Exceptions/NotOptionalException.php b/library/Exceptions/NotOptionalException.php index 36c2a4eb..b24e1d48 100644 --- a/library/Exceptions/NotOptionalException.php +++ b/library/Exceptions/NotOptionalException.php @@ -9,30 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\NotOptional; +use Respect\Validation\Validatable; + final class NotOptionalException extends ValidationException { - public const NAMED = 'named'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'The value must not be optional', - self::NAMED => '{{name}} must not be optional', + Validatable::TEMPLATE_STANDARD => 'The value must not be optional', + NotOptional::TEMPLATE_NAMED => '{{name}} must not be optional', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'The value must be optional', - self::NAMED => '{{name}} must be optional', + Validatable::TEMPLATE_STANDARD => 'The value must be optional', + NotOptional::TEMPLATE_NAMED => '{{name}} must be optional', ], ]; - - protected function chooseTemplate(): string - { - if ($this->getParam('input') || $this->getParam('name')) { - return self::NAMED; - } - - return self::STANDARD; - } } diff --git a/library/Exceptions/NullTypeException.php b/library/Exceptions/NullTypeException.php index aef87998..7a4469bd 100644 --- a/library/Exceptions/NullTypeException.php +++ b/library/Exceptions/NullTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NullTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NullTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be null', + Validatable::TEMPLATE_STANDARD => '{{name}} must be null', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be null', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be null', ], ]; } diff --git a/library/Exceptions/NullableException.php b/library/Exceptions/NullableException.php index 618298bc..33220ec8 100644 --- a/library/Exceptions/NullableException.php +++ b/library/Exceptions/NullableException.php @@ -9,30 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Nullable; +use Respect\Validation\Validatable; + final class NullableException extends ValidationException { - public const NAMED = 'named'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'The value must be nullable', - self::NAMED => '{{name}} must be nullable', + Validatable::TEMPLATE_STANDARD => 'The value must be nullable', + Nullable::TEMPLATE_NAMED => '{{name}} must be nullable', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'The value must not be null', - self::NAMED => '{{name}} must not be null', + Validatable::TEMPLATE_STANDARD => 'The value must not be null', + Nullable::TEMPLATE_NAMED => '{{name}} must not be null', ], ]; - - protected function chooseTemplate(): string - { - if ($this->getParam('input') || $this->getParam('name')) { - return self::NAMED; - } - - return self::STANDARD; - } } diff --git a/library/Exceptions/NumberException.php b/library/Exceptions/NumberException.php index d95bbc09..72aaae6d 100644 --- a/library/Exceptions/NumberException.php +++ b/library/Exceptions/NumberException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NumberException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NumberException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a number', ], ]; } diff --git a/library/Exceptions/NumericValException.php b/library/Exceptions/NumericValException.php index 8e845596..3ca632de 100644 --- a/library/Exceptions/NumericValException.php +++ b/library/Exceptions/NumericValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class NumericValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class NumericValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be numeric', + Validatable::TEMPLATE_STANDARD => '{{name}} must be numeric', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be numeric', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be numeric', ], ]; } diff --git a/library/Exceptions/ObjectTypeException.php b/library/Exceptions/ObjectTypeException.php index d14be825..4a5b7fa3 100644 --- a/library/Exceptions/ObjectTypeException.php +++ b/library/Exceptions/ObjectTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ObjectTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ObjectTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be of type object', + Validatable::TEMPLATE_STANDARD => '{{name}} must be of type object', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be of type object', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be of type object', ], ]; } diff --git a/library/Exceptions/OddException.php b/library/Exceptions/OddException.php index 6e62535b..c649b4f3 100644 --- a/library/Exceptions/OddException.php +++ b/library/Exceptions/OddException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class OddException extends ValidationException { /** @@ -16,10 +18,10 @@ final class OddException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an odd number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an odd number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an odd number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an odd number', ], ]; } diff --git a/library/Exceptions/OneOfException.php b/library/Exceptions/OneOfException.php index b7dddb86..baea0fc4 100644 --- a/library/Exceptions/OneOfException.php +++ b/library/Exceptions/OneOfException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class OneOfException extends NestedValidationException { /** @@ -16,10 +18,10 @@ final class OneOfException extends NestedValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'Only one of these rules must pass for {{name}}', + Validatable::TEMPLATE_STANDARD => 'Only one of these rules must pass for {{name}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'Only one of these rules must not pass for {{name}}', + Validatable::TEMPLATE_STANDARD => 'Only one of these rules must not pass for {{name}}', ], ]; } diff --git a/library/Exceptions/OptionalException.php b/library/Exceptions/OptionalException.php index b2a2312b..7b473faf 100644 --- a/library/Exceptions/OptionalException.php +++ b/library/Exceptions/OptionalException.php @@ -9,26 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Optional; +use Respect\Validation\Validatable; + final class OptionalException extends ValidationException { - public const NAMED = 'named'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => 'The value must be optional', - self::NAMED => '{{name}} must be optional', + Validatable::TEMPLATE_STANDARD => 'The value must be optional', + Optional::TEMPLATE_NAMED => '{{name}} must be optional', ], self::MODE_NEGATIVE => [ - self::STANDARD => 'The value must not be optional', - self::NAMED => '{{name}} must not be optional', + Validatable::TEMPLATE_STANDARD => 'The value must not be optional', + Optional::TEMPLATE_NAMED => '{{name}} must not be optional', ], ]; - - protected function chooseTemplate(): string - { - return $this->getParam('name') ? self::NAMED : self::STANDARD; - } } diff --git a/library/Exceptions/PerfectSquareException.php b/library/Exceptions/PerfectSquareException.php index e2fbcf80..35e4dac3 100644 --- a/library/Exceptions/PerfectSquareException.php +++ b/library/Exceptions/PerfectSquareException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PerfectSquareException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PerfectSquareException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid perfect square', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid perfect square', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid perfect square', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid perfect square', ], ]; } diff --git a/library/Exceptions/PeselException.php b/library/Exceptions/PeselException.php index bb3b82e7..b9e80201 100644 --- a/library/Exceptions/PeselException.php +++ b/library/Exceptions/PeselException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PeselException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PeselException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid PESEL', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid PESEL', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid PESEL', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid PESEL', ], ]; } diff --git a/library/Exceptions/PhoneException.php b/library/Exceptions/PhoneException.php index 22237e43..58389fc8 100644 --- a/library/Exceptions/PhoneException.php +++ b/library/Exceptions/PhoneException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Phone; + final class PhoneException extends ValidationException { public const FOR_COUNTRY = 'for_country'; @@ -19,23 +21,12 @@ final class PhoneException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::INTERNATIONAL => '{{name}} must be a valid telephone number', - self::FOR_COUNTRY => '{{name}} must be a valid telephone number for country {{countryName}}', + Phone::TEMPLATE_INTERNATIONAL => '{{name}} must be a valid telephone number', + Phone::TEMPLATE_FOR_COUNTRY => '{{name}} must be a valid telephone number for country {{countryName}}', ], self::MODE_NEGATIVE => [ - self::INTERNATIONAL => '{{name}} must not be a valid telephone number', - self::FOR_COUNTRY => '{{name}} must not be a valid telephone number for country {{countryName}}', + Phone::TEMPLATE_INTERNATIONAL => '{{name}} must not be a valid telephone number', + Phone::TEMPLATE_FOR_COUNTRY => '{{name}} must not be a valid telephone number for country {{countryName}}', ], ]; - - protected function chooseTemplate(): string - { - $countryCode = $this->getParam('countryCode'); - - if (!$countryCode) { - return self::INTERNATIONAL; - } - - return self::FOR_COUNTRY; - } } diff --git a/library/Exceptions/PhpLabelException.php b/library/Exceptions/PhpLabelException.php index a3ac244c..02abfc03 100644 --- a/library/Exceptions/PhpLabelException.php +++ b/library/Exceptions/PhpLabelException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PhpLabelException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PhpLabelException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid PHP label', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid PHP label', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid PHP label', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid PHP label', ], ]; } diff --git a/library/Exceptions/PisException.php b/library/Exceptions/PisException.php index 0302680a..f5e139dd 100644 --- a/library/Exceptions/PisException.php +++ b/library/Exceptions/PisException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PisException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PisException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid PIS number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid PIS number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid PIS number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid PIS number', ], ]; } diff --git a/library/Exceptions/PolishIdCardException.php b/library/Exceptions/PolishIdCardException.php index d80b95e7..486912a0 100644 --- a/library/Exceptions/PolishIdCardException.php +++ b/library/Exceptions/PolishIdCardException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PolishIdCardException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PolishIdCardException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid Polish Identity Card number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid Polish Identity Card number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid Polish Identity Card number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid Polish Identity Card number', ], ]; } diff --git a/library/Exceptions/PortugueseNifException.php b/library/Exceptions/PortugueseNifException.php index 644d610a..e16d1223 100644 --- a/library/Exceptions/PortugueseNifException.php +++ b/library/Exceptions/PortugueseNifException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PortugueseNifException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PortugueseNifException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a Portuguese NIF', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a Portuguese NIF', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a Portuguese NIF', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a Portuguese NIF', ], ]; } diff --git a/library/Exceptions/PositiveException.php b/library/Exceptions/PositiveException.php index e9c2bc9a..75472ac2 100644 --- a/library/Exceptions/PositiveException.php +++ b/library/Exceptions/PositiveException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PositiveException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PositiveException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be positive', + Validatable::TEMPLATE_STANDARD => '{{name}} must be positive', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be positive', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be positive', ], ]; } diff --git a/library/Exceptions/PostalCodeException.php b/library/Exceptions/PostalCodeException.php index 482c5fab..e59029b2 100644 --- a/library/Exceptions/PostalCodeException.php +++ b/library/Exceptions/PostalCodeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PostalCodeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PostalCodeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid postal code on {{countryCode}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid postal code on {{countryCode}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid postal code on {{countryCode}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid postal code on {{countryCode}}', ], ]; } diff --git a/library/Exceptions/PrimeNumberException.php b/library/Exceptions/PrimeNumberException.php index 971d11ed..c0d38fcb 100644 --- a/library/Exceptions/PrimeNumberException.php +++ b/library/Exceptions/PrimeNumberException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class PrimeNumberException extends ValidationException { /** @@ -16,10 +18,10 @@ final class PrimeNumberException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid prime number', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid prime number', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid prime number', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid prime number', ], ]; } diff --git a/library/Exceptions/PrintableException.php b/library/Exceptions/PrintableException.php index 8015294a..6885cc96 100644 --- a/library/Exceptions/PrintableException.php +++ b/library/Exceptions/PrintableException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule as Filter; +use Respect\Validation\Validatable; + final class PrintableException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class PrintableException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only printable characters', - self::EXTRA => '{{name}} must contain only printable characters and "{{additionalChars}}"', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only printable characters', + Filter::TEMPLATE_EXTRA => '{{name}} must contain only printable characters and "{{additionalChars}}"', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain printable characters', - self::EXTRA => '{{name}} must not contain printable characters or "{{additionalChars}}"', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain printable characters', + Filter::TEMPLATE_EXTRA => '{{name}} must not contain printable characters or "{{additionalChars}}"', ], ]; } diff --git a/library/Exceptions/PropertyException.php b/library/Exceptions/PropertyException.php index 1f142707..eba3f4d5 100644 --- a/library/Exceptions/PropertyException.php +++ b/library/Exceptions/PropertyException.php @@ -9,27 +9,21 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Property; + final class PropertyException extends NestedValidationException implements NonOmissibleException { - public const NOT_PRESENT = 'not_present'; - public const INVALID = 'invalid'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::NOT_PRESENT => 'Property {{name}} must be present', - self::INVALID => 'Property {{name}} must be valid', + Property::TEMPLATE_NOT_PRESENT => 'Property {{name}} must be present', + Property::TEMPLATE_INVALID => 'Property {{name}} must be valid', ], self::MODE_NEGATIVE => [ - self::NOT_PRESENT => 'Property {{name}} must not be present', - self::INVALID => 'Property {{name}} must not validate', + Property::TEMPLATE_NOT_PRESENT => 'Property {{name}} must not be present', + Property::TEMPLATE_INVALID => 'Property {{name}} must not validate', ], ]; - - protected function chooseTemplate(): string - { - return $this->getParam('hasReference') ? self::INVALID : self::NOT_PRESENT; - } } diff --git a/library/Exceptions/PublicDomainSuffixException.php b/library/Exceptions/PublicDomainSuffixException.php index 0cbf803e..653fa32b 100644 --- a/library/Exceptions/PublicDomainSuffixException.php +++ b/library/Exceptions/PublicDomainSuffixException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + class PublicDomainSuffixException extends ValidationException { /** @@ -16,10 +18,10 @@ class PublicDomainSuffixException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a public domain suffix', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a public domain suffix', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must be a public domain suffix', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a public domain suffix', ], ]; } diff --git a/library/Exceptions/PunctException.php b/library/Exceptions/PunctException.php index 4902882e..3e93e863 100644 --- a/library/Exceptions/PunctException.php +++ b/library/Exceptions/PunctException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule as Filter; +use Respect\Validation\Validatable; + final class PunctException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class PunctException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only punctuation characters', - self::EXTRA => '{{name}} must contain only punctuation characters and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only punctuation characters', + Filter::TEMPLATE_EXTRA => '{{name}} must contain only punctuation characters and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain punctuation characters', - self::EXTRA => '{{name}} must not contain punctuation characters or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain punctuation characters', + Filter::TEMPLATE_EXTRA => '{{name}} must not contain punctuation characters or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/ReadableException.php b/library/Exceptions/ReadableException.php index 55550fb5..529a9bae 100644 --- a/library/Exceptions/ReadableException.php +++ b/library/Exceptions/ReadableException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ReadableException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ReadableException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be readable', + Validatable::TEMPLATE_STANDARD => '{{name}} must be readable', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be readable', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be readable', ], ]; } diff --git a/library/Exceptions/RegexException.php b/library/Exceptions/RegexException.php index cf41f7bb..1b107f91 100644 --- a/library/Exceptions/RegexException.php +++ b/library/Exceptions/RegexException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class RegexException extends ValidationException { /** @@ -16,10 +18,10 @@ final class RegexException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must validate against {{regex}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must validate against {{regex}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not validate against {{regex}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not validate against {{regex}}', ], ]; } diff --git a/library/Exceptions/ResourceTypeException.php b/library/Exceptions/ResourceTypeException.php index 161cda20..9e971333 100644 --- a/library/Exceptions/ResourceTypeException.php +++ b/library/Exceptions/ResourceTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ResourceTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ResourceTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a resource', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a resource', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a resource', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a resource', ], ]; } diff --git a/library/Exceptions/RomanException.php b/library/Exceptions/RomanException.php index 6e79103d..e1032794 100644 --- a/library/Exceptions/RomanException.php +++ b/library/Exceptions/RomanException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class RomanException extends ValidationException { /** @@ -16,10 +18,10 @@ final class RomanException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid Roman numeral', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid Roman numeral', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid Roman numeral', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid Roman numeral', ], ]; } diff --git a/library/Exceptions/ScalarValException.php b/library/Exceptions/ScalarValException.php index a63bf38e..01d626a7 100644 --- a/library/Exceptions/ScalarValException.php +++ b/library/Exceptions/ScalarValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class ScalarValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class ScalarValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a scalar value', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a scalar value', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a scalar value', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a scalar value', ], ]; } diff --git a/library/Exceptions/SizeException.php b/library/Exceptions/SizeException.php index dcb01cc9..a83b73f4 100644 --- a/library/Exceptions/SizeException.php +++ b/library/Exceptions/SizeException.php @@ -9,38 +9,23 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Size; + final class SizeException extends NestedValidationException { - public const BOTH = 'both'; - public const LOWER = 'lower'; - public const GREATER = 'greater'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::BOTH => '{{name}} must be between {{minSize}} and {{maxSize}}', - self::LOWER => '{{name}} must be greater than {{minSize}}', - self::GREATER => '{{name}} must be lower than {{maxSize}}', + Size::TEMPLATE_BOTH => '{{name}} must be between {{minSize}} and {{maxSize}}', + Size::TEMPLATE_LOWER => '{{name}} must be greater than {{minSize}}', + Size::TEMPLATE_GREATER => '{{name}} must be lower than {{maxSize}}', ], self::MODE_NEGATIVE => [ - self::BOTH => '{{name}} must not be between {{minSize}} and {{maxSize}}', - self::LOWER => '{{name}} must not be greater than {{minSize}}', - self::GREATER => '{{name}} must not be lower than {{maxSize}}', + Size::TEMPLATE_BOTH => '{{name}} must not be between {{minSize}} and {{maxSize}}', + Size::TEMPLATE_LOWER => '{{name}} must not be greater than {{minSize}}', + Size::TEMPLATE_GREATER => '{{name}} must not be lower than {{maxSize}}', ], ]; - - protected function chooseTemplate(): string - { - if (!$this->getParam('minValue')) { - return self::GREATER; - } - - if (!$this->getParam('maxValue')) { - return self::LOWER; - } - - return self::BOTH; - } } diff --git a/library/Exceptions/SlugException.php b/library/Exceptions/SlugException.php index f72e1fc0..c73e985c 100644 --- a/library/Exceptions/SlugException.php +++ b/library/Exceptions/SlugException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class SlugException extends ValidationException { /** @@ -16,10 +18,10 @@ final class SlugException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid slug', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid slug', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid slug', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid slug', ], ]; } diff --git a/library/Exceptions/SortedException.php b/library/Exceptions/SortedException.php index 9e5f4791..be5c064a 100644 --- a/library/Exceptions/SortedException.php +++ b/library/Exceptions/SortedException.php @@ -13,25 +13,17 @@ use Respect\Validation\Rules\Sorted; final class SortedException extends ValidationException { - public const ASCENDING = 'ascending'; - public const DESCENDING = 'descending'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::ASCENDING => '{{name}} must be sorted in ascending order', - self::DESCENDING => '{{name}} must be sorted in descending order', + Sorted::TEMPLATE_ASCENDING => '{{name}} must be sorted in ascending order', + Sorted::TEMPLATE_DESCENDING => '{{name}} must be sorted in descending order', ], self::MODE_NEGATIVE => [ - self::ASCENDING => '{{name}} must not be sorted in ascending order', - self::DESCENDING => '{{name}} must not be sorted in descending order', + Sorted::TEMPLATE_ASCENDING => '{{name}} must not be sorted in ascending order', + Sorted::TEMPLATE_DESCENDING => '{{name}} must not be sorted in descending order', ], ]; - - protected function chooseTemplate(): string - { - return $this->getParam('direction') === Sorted::ASCENDING ? self::ASCENDING : self::DESCENDING; - } } diff --git a/library/Exceptions/SpaceException.php b/library/Exceptions/SpaceException.php index 7df4212a..002b094e 100644 --- a/library/Exceptions/SpaceException.php +++ b/library/Exceptions/SpaceException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule; +use Respect\Validation\Validatable; + final class SpaceException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class SpaceException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only space characters', - self::EXTRA => '{{name}} must contain only space characters and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only space characters', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must contain only space characters and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain space characters', - self::EXTRA => '{{name}} must not contain space characters or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain space characters', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must not contain space characters or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/StartsWithException.php b/library/Exceptions/StartsWithException.php index 668ec96d..30102be4 100644 --- a/library/Exceptions/StartsWithException.php +++ b/library/Exceptions/StartsWithException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class StartsWithException extends ValidationException { /** @@ -16,10 +18,10 @@ final class StartsWithException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must start with {{startValue}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must start with {{startValue}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not start with {{startValue}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not start with {{startValue}}', ], ]; } diff --git a/library/Exceptions/StringTypeException.php b/library/Exceptions/StringTypeException.php index ccac289c..20b5162c 100644 --- a/library/Exceptions/StringTypeException.php +++ b/library/Exceptions/StringTypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class StringTypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class StringTypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be of type string', + Validatable::TEMPLATE_STANDARD => '{{name}} must be of type string', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be of type string', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be of type string', ], ]; } diff --git a/library/Exceptions/StringValException.php b/library/Exceptions/StringValException.php index 6bfa400b..c3b4af9c 100644 --- a/library/Exceptions/StringValException.php +++ b/library/Exceptions/StringValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class StringValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class StringValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a string', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a string', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be string', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be string', ], ]; } diff --git a/library/Exceptions/SubdivisionCodeException.php b/library/Exceptions/SubdivisionCodeException.php index 7156cc84..83cc24a1 100644 --- a/library/Exceptions/SubdivisionCodeException.php +++ b/library/Exceptions/SubdivisionCodeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + class SubdivisionCodeException extends ValidationException { /** @@ -16,10 +18,10 @@ class SubdivisionCodeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a subdivision code of {{countryName}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a subdivision code of {{countryName}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a subdivision code of {{countryName}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a subdivision code of {{countryName}}', ], ]; } diff --git a/library/Exceptions/SubsetException.php b/library/Exceptions/SubsetException.php index 1bc79bb9..165e05ed 100644 --- a/library/Exceptions/SubsetException.php +++ b/library/Exceptions/SubsetException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class SubsetException extends ValidationException { /** @@ -16,10 +18,10 @@ final class SubsetException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be subset of {{superset}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be subset of {{superset}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be subset of {{superset}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be subset of {{superset}}', ], ]; } diff --git a/library/Exceptions/SymbolicLinkException.php b/library/Exceptions/SymbolicLinkException.php index ef3e2e9d..fc98b99b 100644 --- a/library/Exceptions/SymbolicLinkException.php +++ b/library/Exceptions/SymbolicLinkException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class SymbolicLinkException extends ValidationException { /** @@ -16,10 +18,10 @@ final class SymbolicLinkException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a symbolic link', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a symbolic link', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a symbolic link', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a symbolic link', ], ]; } diff --git a/library/Exceptions/TimeException.php b/library/Exceptions/TimeException.php index eb4a3f5b..45a213ec 100644 --- a/library/Exceptions/TimeException.php +++ b/library/Exceptions/TimeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class TimeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class TimeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid time in the format {{sample}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid time in the format {{sample}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid time in the format {{sample}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid time in the format {{sample}}', ], ]; } diff --git a/library/Exceptions/TldException.php b/library/Exceptions/TldException.php index 80ab534d..dc9370b2 100644 --- a/library/Exceptions/TldException.php +++ b/library/Exceptions/TldException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class TldException extends ValidationException { /** @@ -16,10 +18,10 @@ final class TldException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid top-level domain name', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid top-level domain name', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid top-level domain name', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid top-level domain name', ], ]; } diff --git a/library/Exceptions/TrueValException.php b/library/Exceptions/TrueValException.php index 9d294786..ded96c7c 100644 --- a/library/Exceptions/TrueValException.php +++ b/library/Exceptions/TrueValException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class TrueValException extends ValidationException { /** @@ -16,10 +18,10 @@ final class TrueValException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must evaluate to `true`', + Validatable::TEMPLATE_STANDARD => '{{name}} must evaluate to `true`', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not evaluate to `true`', + Validatable::TEMPLATE_STANDARD => '{{name}} must not evaluate to `true`', ], ]; } diff --git a/library/Exceptions/TypeException.php b/library/Exceptions/TypeException.php index fbf4ef2c..fe1efdc9 100644 --- a/library/Exceptions/TypeException.php +++ b/library/Exceptions/TypeException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class TypeException extends ValidationException { /** @@ -16,10 +18,10 @@ final class TypeException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be {{type}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be {{type}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be {{type}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be {{type}}', ], ]; } diff --git a/library/Exceptions/UniqueException.php b/library/Exceptions/UniqueException.php index 9a1eeee9..fb1799f0 100644 --- a/library/Exceptions/UniqueException.php +++ b/library/Exceptions/UniqueException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class UniqueException extends ValidationException { /** @@ -16,10 +18,10 @@ final class UniqueException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must not contain duplicates', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain duplicates', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must contain duplicates', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain duplicates', ], ]; } diff --git a/library/Exceptions/UploadedException.php b/library/Exceptions/UploadedException.php index 0270aff0..c612499d 100644 --- a/library/Exceptions/UploadedException.php +++ b/library/Exceptions/UploadedException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class UploadedException extends ValidationException { /** @@ -16,10 +18,10 @@ final class UploadedException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an uploaded file', + Validatable::TEMPLATE_STANDARD => '{{name}} must be an uploaded file', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an uploaded file', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be an uploaded file', ], ]; } diff --git a/library/Exceptions/UppercaseException.php b/library/Exceptions/UppercaseException.php index dd9d6aa7..c6ad2ed0 100644 --- a/library/Exceptions/UppercaseException.php +++ b/library/Exceptions/UppercaseException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class UppercaseException extends ValidationException { /** @@ -16,10 +18,10 @@ final class UppercaseException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be uppercase', + Validatable::TEMPLATE_STANDARD => '{{name}} must be uppercase', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be uppercase', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be uppercase', ], ]; } diff --git a/library/Exceptions/UrlException.php b/library/Exceptions/UrlException.php index 6ca8fc3f..79e46a49 100644 --- a/library/Exceptions/UrlException.php +++ b/library/Exceptions/UrlException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class UrlException extends ValidationException { /** @@ -16,10 +18,10 @@ final class UrlException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a URL', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a URL', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a URL', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a URL', ], ]; } diff --git a/library/Exceptions/UuidException.php b/library/Exceptions/UuidException.php index d5c17437..514a2e9d 100644 --- a/library/Exceptions/UuidException.php +++ b/library/Exceptions/UuidException.php @@ -9,30 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\Uuid; +use Respect\Validation\Validatable; + final class UuidException extends ValidationException { - public const VERSION = 'version'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid UUID', - self::VERSION => '{{name}} must be a valid UUID version {{version}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid UUID', + Uuid::TEMPLATE_VERSION => '{{name}} must be a valid UUID version {{version}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid UUID', - self::VERSION => '{{name}} must not be a valid UUID version {{version}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid UUID', + Uuid::TEMPLATE_VERSION => '{{name}} must not be a valid UUID version {{version}}', ], ]; - - protected function chooseTemplate(): string - { - if ($this->getParam('version')) { - return self::VERSION; - } - - return self::STANDARD; - } } diff --git a/library/Exceptions/ValidationException.php b/library/Exceptions/ValidationException.php index b62fe339..fa9a5170 100644 --- a/library/Exceptions/ValidationException.php +++ b/library/Exceptions/ValidationException.php @@ -11,6 +11,7 @@ namespace Respect\Validation\Exceptions; use InvalidArgumentException; use Respect\Validation\Message\Formatter; +use Respect\Validation\Validatable; use function key; @@ -18,24 +19,21 @@ class ValidationException extends InvalidArgumentException implements Exception { public const MODE_DEFAULT = 'default'; public const MODE_NEGATIVE = 'negative'; - public const STANDARD = 'standard'; /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be valid', + Validatable::TEMPLATE_STANDARD => '{{name}} must be valid', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be valid', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be valid', ], ]; private string $mode = self::MODE_DEFAULT; - private string $template; - /** * @param mixed[] $params */ @@ -43,10 +41,9 @@ class ValidationException extends InvalidArgumentException implements Exception private readonly mixed $input, private readonly string $id, private array $params, + private string $template, private readonly Formatter $formatter ) { - $this->template = $this->chooseTemplate(); - parent::__construct($this->createMessage()); } @@ -94,7 +91,7 @@ class ValidationException extends InvalidArgumentException implements Exception return isset($this->defaultTemplates[$this->mode][$this->template]) === false; } - protected function chooseTemplate(): string + protected function getTemplate(): string { return (string) key($this->defaultTemplates[$this->mode]); } diff --git a/library/Exceptions/VersionException.php b/library/Exceptions/VersionException.php index 5db0c635..60e913db 100644 --- a/library/Exceptions/VersionException.php +++ b/library/Exceptions/VersionException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class VersionException extends ValidationException { /** @@ -16,10 +18,10 @@ final class VersionException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a version', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a version', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a version', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a version', ], ]; } diff --git a/library/Exceptions/VideoUrlException.php b/library/Exceptions/VideoUrlException.php index 09e5a2a1..538b0a26 100644 --- a/library/Exceptions/VideoUrlException.php +++ b/library/Exceptions/VideoUrlException.php @@ -9,30 +9,22 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\VideoUrl; +use Respect\Validation\Validatable; + final class VideoUrlException extends ValidationException { - public const SERVICE = 'service'; - /** * @var array> */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be a valid video URL', - self::SERVICE => '{{name}} must be a valid {{service}} video URL', + Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid video URL', + VideoUrl::TEMPLATE_SERVICE => '{{name}} must be a valid {{service}} video URL', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be a valid video URL', - self::SERVICE => '{{name}} must not be a valid {{service}} video URL', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid video URL', + VideoUrl::TEMPLATE_SERVICE => '{{name}} must not be a valid {{service}} video URL', ], ]; - - protected function chooseTemplate(): string - { - if ($this->getParam('service')) { - return self::SERVICE; - } - - return self::STANDARD; - } } diff --git a/library/Exceptions/VowelException.php b/library/Exceptions/VowelException.php index c1780a20..34c89cac 100644 --- a/library/Exceptions/VowelException.php +++ b/library/Exceptions/VowelException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule; +use Respect\Validation\Validatable; + final class VowelException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class VowelException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must contain only vowels', - self::EXTRA => '{{name}} must contain only vowels and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must contain only vowels', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must contain only vowels and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain vowels', - self::EXTRA => '{{name}} must not contain vowels or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain vowels', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must not contain vowels or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/WritableException.php b/library/Exceptions/WritableException.php index 405797d9..937f2f61 100644 --- a/library/Exceptions/WritableException.php +++ b/library/Exceptions/WritableException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class WritableException extends ValidationException { /** @@ -16,10 +18,10 @@ final class WritableException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be writable', + Validatable::TEMPLATE_STANDARD => '{{name}} must be writable', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be writable', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be writable', ], ]; } diff --git a/library/Exceptions/XdigitException.php b/library/Exceptions/XdigitException.php index 964af3d6..52f13b6f 100644 --- a/library/Exceptions/XdigitException.php +++ b/library/Exceptions/XdigitException.php @@ -9,6 +9,9 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Rules\AbstractFilterRule; +use Respect\Validation\Validatable; + final class XdigitException extends FilteredValidationException { /** @@ -16,12 +19,12 @@ final class XdigitException extends FilteredValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} contain only hexadecimal digits', - self::EXTRA => '{{name}} contain only hexadecimal digits and {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} contain only hexadecimal digits', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} contain only hexadecimal digits and {{additionalChars}}', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not contain hexadecimal digits', - self::EXTRA => '{{name}} must not contain hexadecimal digits or {{additionalChars}}', + Validatable::TEMPLATE_STANDARD => '{{name}} must not contain hexadecimal digits', + AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must not contain hexadecimal digits or {{additionalChars}}', ], ]; } diff --git a/library/Exceptions/YesException.php b/library/Exceptions/YesException.php index a8a4af2c..2a29cbc6 100644 --- a/library/Exceptions/YesException.php +++ b/library/Exceptions/YesException.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; +use Respect\Validation\Validatable; + final class YesException extends ValidationException { /** @@ -16,10 +18,10 @@ final class YesException extends ValidationException */ protected array $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be similar to "Yes"', + Validatable::TEMPLATE_STANDARD => '{{name}} must be similar to "Yes"', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be similar to "Yes"', + Validatable::TEMPLATE_STANDARD => '{{name}} must not be similar to "Yes"', ], ]; } diff --git a/library/Factory.php b/library/Factory.php index dc8a684d..bdf76225 100644 --- a/library/Factory.php +++ b/library/Factory.php @@ -133,25 +133,25 @@ final class Factory if ($validatable->getName() !== null) { $id = $params['name'] = $validatable->getName(); } + $template = $params['template'] ?? $validatable->getTemplate($input); $exceptionNamespace = str_replace('\\Rules', '\\Exceptions', $reflection->getNamespaceName()); foreach (array_merge([$exceptionNamespace], $this->exceptionsNamespaces) as $namespace) { try { /** @var class-string $exceptionName */ $exceptionName = $namespace . '\\' . $ruleName . 'Exception'; - return $this->createValidationException( - $exceptionName, - $id, - $input, - $params, - $formatter - ); - } catch (ReflectionException $exception) { + /** @var ValidationException $exception */ + $exception = $this + ->createReflectionClass($exceptionName, ValidationException::class) + ->newInstance($input, $id, $params, $template, $formatter); + + return $exception; + } catch (ReflectionException) { continue; } } - return new ValidationException($input, $id, $params, $formatter); + return new ValidationException($input, $id, $params, $template, $formatter); } public static function setDefaultInstance(self $defaultInstance): void @@ -182,32 +182,6 @@ final class Factory return $reflection; } - /** - * @param class-string $exceptionName - * - * @param mixed[] $params - * - * @throws InvalidClassException - * @throws ReflectionException - */ - private function createValidationException( - string $exceptionName, - string $id, - mixed $input, - array $params, - Formatter $formatter - ): ValidationException { - /** @var ValidationException $exception */ - $exception = $this - ->createReflectionClass($exceptionName, ValidationException::class) - ->newInstance($input, $id, $params, $formatter); - if (isset($params['template'])) { - $exception->updateTemplate($params['template']); - } - - return $exception; - } - /** * @param ReflectionObject|ReflectionClass $reflection * @return mixed[] diff --git a/library/Rules/AbstractFilterRule.php b/library/Rules/AbstractFilterRule.php index fb3a025f..6630a7be 100644 --- a/library/Rules/AbstractFilterRule.php +++ b/library/Rules/AbstractFilterRule.php @@ -16,6 +16,8 @@ use function str_split; abstract class AbstractFilterRule extends AbstractRule { + public const TEMPLATE_EXTRA = 'extra'; + private readonly string $additionalChars; abstract protected function validateFilteredInput(string $input): bool; @@ -41,6 +43,11 @@ abstract class AbstractFilterRule extends AbstractRule return $filteredInput === '' || $this->validateFilteredInput($filteredInput); } + public function getTemplate(mixed $input): string + { + return $this->template ?? ($this->additionalChars ? self::TEMPLATE_EXTRA : self::TEMPLATE_STANDARD); + } + private function filter(string $input): string { return str_replace(str_split($this->additionalChars), '', $input); diff --git a/library/Rules/AbstractRelated.php b/library/Rules/AbstractRelated.php index c6f5fefa..9128d212 100644 --- a/library/Rules/AbstractRelated.php +++ b/library/Rules/AbstractRelated.php @@ -17,6 +17,9 @@ use function is_scalar; abstract class AbstractRelated extends AbstractRule { + public const TEMPLATE_NOT_PRESENT = 'not_present'; + public const TEMPLATE_INVALID = 'invalid'; + abstract public function hasReference(mixed $input): bool; abstract public function getReferenceValue(mixed $input): mixed; @@ -59,7 +62,7 @@ abstract class AbstractRelated extends AbstractRule { $hasReference = $this->hasReference($input); if ($this->mandatory && !$hasReference) { - throw $this->reportError($input, ['hasReference' => false]); + throw $this->reportError($input); } if ($this->rule === null || !$hasReference) { @@ -70,7 +73,7 @@ abstract class AbstractRelated extends AbstractRule $this->rule->assert($this->getReferenceValue($input)); } catch (ValidationException $validationException) { /** @var NestedValidationException $nestedValidationException */ - $nestedValidationException = $this->reportError($this->reference, ['hasReference' => true]); + $nestedValidationException = $this->reportError($input, ['name' => $this->reference]); $nestedValidationException->addChild($validationException); throw $nestedValidationException; @@ -81,7 +84,7 @@ abstract class AbstractRelated extends AbstractRule { $hasReference = $this->hasReference($input); if ($this->mandatory && !$hasReference) { - throw $this->reportError($input, ['hasReference' => false]); + throw $this->reportError($input); } if ($this->rule === null || !$hasReference) { @@ -104,4 +107,17 @@ abstract class AbstractRelated extends AbstractRule return $this->rule->validate($this->getReferenceValue($input)); } + + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if ($this->rule === null) { + return self::TEMPLATE_NOT_PRESENT; + } + + return $this->hasReference($input) ? self::TEMPLATE_INVALID : self::TEMPLATE_NOT_PRESENT; + } } diff --git a/library/Rules/AbstractRule.php b/library/Rules/AbstractRule.php index 1d1b2c47..736e3c71 100644 --- a/library/Rules/AbstractRule.php +++ b/library/Rules/AbstractRule.php @@ -60,6 +60,11 @@ abstract class AbstractRule implements Validatable return $this; } + public function getTemplate(mixed $input): string + { + return $this->template ?? self::TEMPLATE_STANDARD; + } + public function __invoke(mixed $input): bool { return $this->validate($input); diff --git a/library/Rules/AllOf.php b/library/Rules/AllOf.php index 77f8d3eb..3972c5ac 100644 --- a/library/Rules/AllOf.php +++ b/library/Rules/AllOf.php @@ -15,13 +15,16 @@ use function count; class AllOf extends AbstractComposite { + public const TEMPLATE_NONE = 'none'; + public const TEMPLATE_SOME = 'some'; + public function assert(mixed $input): void { try { parent::assert($input); } catch (AllOfException $exception) { if (count($exception->getChildren()) === count($this->getRules()) && !$exception->hasCustomTemplate()) { - $exception->updateTemplate(AllOfException::NONE); + $exception->updateTemplate(self::TEMPLATE_NONE); } throw $exception; @@ -45,4 +48,9 @@ class AllOf extends AbstractComposite return true; } + + public function getTemplate(mixed $input): string + { + return $this->template ?? self::TEMPLATE_SOME; + } } diff --git a/library/Rules/AlwaysInvalid.php b/library/Rules/AlwaysInvalid.php index 3a86c95d..624e053c 100644 --- a/library/Rules/AlwaysInvalid.php +++ b/library/Rules/AlwaysInvalid.php @@ -11,6 +11,8 @@ namespace Respect\Validation\Rules; final class AlwaysInvalid extends AbstractRule { + public const TEMPLATE_SIMPLE = 'simple'; + public function validate(mixed $input): bool { return false; diff --git a/library/Rules/AnyOf.php b/library/Rules/AnyOf.php index e94d2a4e..c2c3fb09 100644 --- a/library/Rules/AnyOf.php +++ b/library/Rules/AnyOf.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Exceptions\AnyOfException; +use Respect\Validation\Exceptions\NestedValidationException; use Respect\Validation\Exceptions\ValidationException; use function count; @@ -20,7 +20,7 @@ final class AnyOf extends AbstractComposite { try { parent::assert($input); - } catch (AnyOfException $exception) { + } catch (NestedValidationException $exception) { if (count($exception->getChildren()) === count($this->getRules())) { throw $exception; } diff --git a/library/Rules/CreditCard.php b/library/Rules/CreditCard.php index d67dd355..11501877 100644 --- a/library/Rules/CreditCard.php +++ b/library/Rules/CreditCard.php @@ -20,6 +20,8 @@ use function sprintf; final class CreditCard extends AbstractRule { + public const TEMPLATE_BRANDED = 'branded'; + public const ANY = 'Any'; public const AMERICAN_EXPRESS = 'American Express'; @@ -77,4 +79,17 @@ final class CreditCard extends AbstractRule return preg_match(self::BRAND_REGEX_LIST[$this->brand], $input) > 0; } + + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if ($this->brand === CreditCard::ANY) { + return self::TEMPLATE_STANDARD; + } + + return self::TEMPLATE_BRANDED; + } } diff --git a/library/Rules/DateTime.php b/library/Rules/DateTime.php index 74ba50af..c455c3eb 100644 --- a/library/Rules/DateTime.php +++ b/library/Rules/DateTime.php @@ -20,6 +20,8 @@ final class DateTime extends AbstractRule { use CanValidateDateTime; + public const TEMPLATE_FORMAT = 'format'; + private readonly string $sample; public function __construct( @@ -44,4 +46,9 @@ final class DateTime extends AbstractRule return $this->isDateTime($this->format, (string) $input); } + + public function getTemplate(mixed $input): string + { + return $this->template ?? ($this->format !== null ? self::TEMPLATE_FORMAT : self::TEMPLATE_STANDARD); + } } diff --git a/library/Rules/Domain.php b/library/Rules/Domain.php index b30cd780..b945d752 100644 --- a/library/Rules/Domain.php +++ b/library/Rules/Domain.php @@ -9,7 +9,6 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Exceptions\DomainException; use Respect\Validation\Exceptions\NestedValidationException; use Respect\Validation\Exceptions\ValidationException; use Respect\Validation\Validatable; @@ -141,7 +140,7 @@ final class Domain extends AbstractRule private function throwExceptions(array $exceptions, mixed $input): void { if (count($exceptions)) { - /** @var DomainException $domainException */ + /** @var NestedValidationException $domainException */ $domainException = $this->reportError($input); $domainException->addChildren($exceptions); diff --git a/library/Rules/Ip.php b/library/Rules/Ip.php index 675675b0..36c20563 100644 --- a/library/Rules/Ip.php +++ b/library/Rules/Ip.php @@ -28,6 +28,8 @@ use const FILTER_VALIDATE_IP; final class Ip extends AbstractRule { + public const TEMPLATE_NETWORK_RANGE = 'network_range'; + private ?string $range = null; private ?string $startAddress = null; @@ -66,6 +68,11 @@ final class Ip extends AbstractRule return true; } + public function getTemplate(mixed $input): string + { + return $this->template ?? ($this->range ? self::TEMPLATE_NETWORK_RANGE : self::TEMPLATE_STANDARD); + } + private function createRange(): ?string { if ($this->startAddress && $this->endAddress) { diff --git a/library/Rules/KeySet.php b/library/Rules/KeySet.php index e3a275b4..1b4a7e6e 100644 --- a/library/Rules/KeySet.php +++ b/library/Rules/KeySet.php @@ -21,6 +21,11 @@ use function is_array; final class KeySet extends AbstractWrapper implements NonNegatable { + public const TEMPLATE_NONE = 'none'; + public const TEMPLATE_SOME = 'some'; + public const TEMPLATE_STRUCTURE = 'structure'; + public const TEMPLATE_STRUCTURE_EXTRA = 'structure_extra'; + /** * @var mixed[] */ @@ -71,6 +76,19 @@ final class KeySet extends AbstractWrapper implements NonNegatable return parent::validate($input); } + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if (count($this->extraKeys)) { + return self::TEMPLATE_STRUCTURE_EXTRA; + } + + return KeySet::TEMPLATE_STRUCTURE; + } + /** * @throws ComponentException */ diff --git a/library/Rules/KeyValue.php b/library/Rules/KeyValue.php index dc08ff92..cd9e0a3c 100644 --- a/library/Rules/KeyValue.php +++ b/library/Rules/KeyValue.php @@ -16,9 +16,12 @@ use Respect\Validation\Validatable; use function array_keys; use function in_array; +use function Respect\Stringifier\stringify; final class KeyValue extends AbstractRule { + public const TEMPLATE_COMPONENT = 'component'; + public function __construct( private readonly int|string $comparedKey, private readonly string $ruleName, @@ -59,6 +62,25 @@ final class KeyValue extends AbstractRule return $rule->validate($input[$this->comparedKey]); } + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if (!isset($input[$this->comparedKey]) || !isset($input[$this->baseKey])) { + return self::TEMPLATE_STANDARD; + } + + try { + $this->createRule($input[$this->baseKey]); + } catch (ComponentException) { + return self::TEMPLATE_COMPONENT; + } + + return self::TEMPLATE_STANDARD; + } + /** * @param mixed[] $extraParameters */ @@ -74,21 +96,18 @@ final class KeyValue extends AbstractRule private function getRule(mixed $input): Validatable { if (!isset($input[$this->comparedKey])) { - throw parent::reportError($this->comparedKey); + throw parent::reportError($input, ['name' => stringify($this->comparedKey)]); } if (!isset($input[$this->baseKey])) { - throw parent::reportError($this->baseKey); + throw parent::reportError($input, ['name' => stringify($this->baseKey)]); } try { - $rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input[$this->baseKey]]); - $rule->setName((string) $this->comparedKey); - } catch (ComponentException $exception) { - throw parent::reportError($input, ['component' => true]); + return $this->createRule($input[$this->baseKey]); + } catch (ComponentException) { + throw parent::reportError($input); } - - return $rule; } private function overwriteExceptionParams(ValidationException $exception): ValidationException @@ -107,4 +126,12 @@ final class KeyValue extends AbstractRule return $exception; } + + private function createRule(mixed $input): Validatable + { + $rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input]); + $rule->setName((string)$this->comparedKey); + + return $rule; + } } diff --git a/library/Rules/Length.php b/library/Rules/Length.php index d8d9a135..123e1f04 100644 --- a/library/Rules/Length.php +++ b/library/Rules/Length.php @@ -23,6 +23,13 @@ use function sprintf; final class Length extends AbstractRule { + public const TEMPLATE_LOWER = 'lower'; + public const TEMPLATE_GREATER = 'greater'; + public const TEMPLATE_GREATER_INCLUSIVE = 'greater_inclusive'; + public const TEMPLATE_EXACT = 'exact'; + public const TEMPLATE_LOWER_INCLUSIVE = 'lower_inclusive'; + public const TEMPLATE_BOTH = 'both'; + /** * @throws ComponentException */ @@ -47,6 +54,27 @@ final class Length extends AbstractRule return $this->validateMin($length) && $this->validateMax($length); } + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if (!$this->minValue) { + return $this->inclusive === true ? self::TEMPLATE_GREATER_INCLUSIVE : self::TEMPLATE_GREATER; + } + + if (!$this->maxValue) { + return $this->inclusive === true ? self::TEMPLATE_LOWER_INCLUSIVE : self::TEMPLATE_LOWER; + } + + if ($this->minValue == $this->maxValue) { + return self::TEMPLATE_EXACT; + } + + return self::TEMPLATE_BOTH; + } + private function extractLength(mixed $input): ?int { if (is_string($input)) { diff --git a/library/Rules/NoneOf.php b/library/Rules/NoneOf.php index d88f9669..f08286ee 100644 --- a/library/Rules/NoneOf.php +++ b/library/Rules/NoneOf.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Exceptions\NoneOfException; +use Respect\Validation\Exceptions\NestedValidationException; use function count; @@ -19,7 +19,7 @@ final class NoneOf extends AbstractComposite { try { parent::assert($input); - } catch (NoneOfException $exception) { + } catch (NestedValidationException $exception) { if (count($exception->getChildren()) !== count($this->getRules())) { throw $exception; } diff --git a/library/Rules/Not.php b/library/Rules/Not.php index 2155b4f5..f50f7da9 100644 --- a/library/Rules/Not.php +++ b/library/Rules/Not.php @@ -21,6 +21,9 @@ use function sprintf; final class Not extends AbstractRule { + public const TEMPLATE_NONE = 'none'; + public const TEMPLATE_SOME = 'some'; + private readonly Validatable $rule; public function __construct(Validatable $rule) diff --git a/library/Rules/NotBlank.php b/library/Rules/NotBlank.php index 9f9d39ec..fbb48a8d 100644 --- a/library/Rules/NotBlank.php +++ b/library/Rules/NotBlank.php @@ -19,6 +19,8 @@ use function trim; final class NotBlank extends AbstractRule { + public const TEMPLATE_NAMED = 'named'; + public function validate(mixed $input): bool { if (is_numeric($input)) { @@ -39,4 +41,17 @@ final class NotBlank extends AbstractRule return !empty($input); } + + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if ($input || $this->getName()) { + return self::TEMPLATE_NAMED; + } + + return self::TEMPLATE_STANDARD; + } } diff --git a/library/Rules/NotEmpty.php b/library/Rules/NotEmpty.php index 3e9c8902..3652eb09 100644 --- a/library/Rules/NotEmpty.php +++ b/library/Rules/NotEmpty.php @@ -14,6 +14,8 @@ use function trim; final class NotEmpty extends AbstractRule { + public const TEMPLATE_NAMED = 'named'; + public function validate(mixed $input): bool { if (is_string($input)) { @@ -22,4 +24,17 @@ final class NotEmpty extends AbstractRule return !empty($input); } + + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if ($input || $this->getName()) { + return self::TEMPLATE_NAMED; + } + + return self::TEMPLATE_STANDARD; + } } diff --git a/library/Rules/NotOptional.php b/library/Rules/NotOptional.php index a6817f0a..972690dd 100644 --- a/library/Rules/NotOptional.php +++ b/library/Rules/NotOptional.php @@ -15,8 +15,23 @@ final class NotOptional extends AbstractRule { use CanValidateUndefined; + public const TEMPLATE_NAMED = 'named'; + public function validate(mixed $input): bool { return $this->isUndefined($input) === false; } + + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if ($input || $this->getName()) { + return self::TEMPLATE_NAMED; + } + + return self::TEMPLATE_STANDARD; + } } diff --git a/library/Rules/Nullable.php b/library/Rules/Nullable.php index 1fee064f..161d0c5d 100644 --- a/library/Rules/Nullable.php +++ b/library/Rules/Nullable.php @@ -11,6 +11,8 @@ namespace Respect\Validation\Rules; final class Nullable extends AbstractWrapper { + public const TEMPLATE_NAMED = 'named'; + public function assert(mixed $input): void { if ($input === null) { @@ -37,4 +39,17 @@ final class Nullable extends AbstractWrapper return parent::validate($input); } + + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if ($input || $this->getName()) { + return self::TEMPLATE_NAMED; + } + + return self::TEMPLATE_STANDARD; + } } diff --git a/library/Rules/OneOf.php b/library/Rules/OneOf.php index 00925df1..564a2580 100644 --- a/library/Rules/OneOf.php +++ b/library/Rules/OneOf.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Exceptions\OneOfException; +use Respect\Validation\Exceptions\NestedValidationException; use Respect\Validation\Exceptions\ValidationException; use function array_shift; @@ -21,7 +21,7 @@ final class OneOf extends AbstractComposite { try { parent::assert($input); - } catch (OneOfException $exception) { + } catch (NestedValidationException $exception) { if (count($exception->getChildren()) !== count($this->getRules()) - 1) { throw $exception; } diff --git a/library/Rules/Optional.php b/library/Rules/Optional.php index 28b0b4f6..4d4ebd8c 100644 --- a/library/Rules/Optional.php +++ b/library/Rules/Optional.php @@ -15,6 +15,8 @@ final class Optional extends AbstractWrapper { use CanValidateUndefined; + public const TEMPLATE_NAMED = 'named'; + public function assert(mixed $input): void { if ($this->isUndefined($input)) { @@ -41,4 +43,17 @@ final class Optional extends AbstractWrapper return parent::validate($input); } + + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if ($this->getName()) { + return self::TEMPLATE_NAMED; + } + + return self::TEMPLATE_STANDARD; + } } diff --git a/library/Rules/Phone.php b/library/Rules/Phone.php index 4ab691e2..41f5b7b0 100644 --- a/library/Rules/Phone.php +++ b/library/Rules/Phone.php @@ -21,6 +21,9 @@ use function sprintf; final class Phone extends AbstractRule { + public const TEMPLATE_FOR_COUNTRY = 'for_country'; + public const TEMPLATE_INTERNATIONAL = 'international'; + private readonly ?string $countryName; public function __construct(private readonly ?string $countryCode = null) @@ -55,4 +58,9 @@ final class Phone extends AbstractRule return false; } } + + public function getTemplate(mixed $input): string + { + return $this->template ?? $this->countryName ? self::TEMPLATE_FOR_COUNTRY : self::TEMPLATE_INTERNATIONAL; + } } diff --git a/library/Rules/Size.php b/library/Rules/Size.php index 94f44fa2..7c018e23 100644 --- a/library/Rules/Size.php +++ b/library/Rules/Size.php @@ -23,6 +23,10 @@ use function sprintf; final class Size extends AbstractRule { + public const TEMPLATE_LOWER = 'lower'; + public const TEMPLATE_GREATER = 'greater'; + public const TEMPLATE_BOTH = 'both'; + private readonly ?float $minValue; private readonly ?float $maxValue; @@ -56,6 +60,23 @@ final class Size extends AbstractRule return false; } + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if (!$this->minValue) { + return self::TEMPLATE_GREATER; + } + + if (!$this->maxValue) { + return self::TEMPLATE_LOWER; + } + + return self::TEMPLATE_BOTH; + } + /** * @todo Move it to a trait */ diff --git a/library/Rules/Sorted.php b/library/Rules/Sorted.php index b8bcaf5a..b8bfee5d 100644 --- a/library/Rules/Sorted.php +++ b/library/Rules/Sorted.php @@ -20,6 +20,9 @@ use function str_split; final class Sorted extends AbstractRule { + public const TEMPLATE_ASCENDING = 'ascending'; + public const TEMPLATE_DESCENDING = 'descending'; + public const ASCENDING = 'ASC'; public const DESCENDING = 'DESC'; @@ -50,6 +53,19 @@ final class Sorted extends AbstractRule return true; } + public function getTemplate(mixed $input): string + { + if ($this->template !== null) { + return $this->template; + } + + if ($this->direction === Sorted::ASCENDING) { + return self::TEMPLATE_ASCENDING; + } + + return self::TEMPLATE_DESCENDING; + } + private function isSorted(mixed $current, mixed $last): bool { if ($this->direction === self::ASCENDING) { diff --git a/library/Rules/Uuid.php b/library/Rules/Uuid.php index 94b54115..e87f90d1 100644 --- a/library/Rules/Uuid.php +++ b/library/Rules/Uuid.php @@ -17,6 +17,8 @@ use function sprintf; final class Uuid extends AbstractRule { + public const TEMPLATE_VERSION = 'version'; + private const PATTERN_FORMAT = '/^[0-9a-f]{8}-[0-9a-f]{4}-%s[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i'; /** @@ -39,6 +41,11 @@ final class Uuid extends AbstractRule return preg_match($this->getPattern(), $input) > 0; } + public function getTemplate(mixed $input): string + { + return $this->template ?? ($this->version ? self::TEMPLATE_VERSION : self::TEMPLATE_STANDARD); + } + private function isSupportedVersion(int $version): bool { return $version >= 1 && $version <= 5 && $version !== 2; diff --git a/library/Rules/VideoUrl.php b/library/Rules/VideoUrl.php index 1a9a3ee6..3d8a6adf 100644 --- a/library/Rules/VideoUrl.php +++ b/library/Rules/VideoUrl.php @@ -19,6 +19,8 @@ use function sprintf; final class VideoUrl extends AbstractRule { + public const TEMPLATE_SERVICE = 'service'; + private const SERVICES = [ // phpcs:disable Generic.Files.LineLength.TooLong 'youtube' => '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i', @@ -59,6 +61,11 @@ final class VideoUrl extends AbstractRule return false; } + public function getTemplate(mixed $input): string + { + return $this->template ?? ($this->service ? self::TEMPLATE_SERVICE : self::TEMPLATE_STANDARD); + } + private function isSupportedService(string $service): bool { return isset(self::SERVICES[mb_strtolower($service)]); diff --git a/library/Rules/When.php b/library/Rules/When.php index 7aff1dbf..c5802949 100644 --- a/library/Rules/When.php +++ b/library/Rules/When.php @@ -9,7 +9,6 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use Respect\Validation\Exceptions\AlwaysInvalidException; use Respect\Validation\Validatable; final class When extends AbstractRule @@ -23,7 +22,7 @@ final class When extends AbstractRule ) { if ($else === null) { $else = new AlwaysInvalid(); - $else->setTemplate(AlwaysInvalidException::SIMPLE); + $else->setTemplate(AlwaysInvalid::TEMPLATE_SIMPLE); } $this->else = $else; diff --git a/library/Validatable.php b/library/Validatable.php index 8882fdc1..b6875297 100644 --- a/library/Validatable.php +++ b/library/Validatable.php @@ -13,6 +13,8 @@ use Respect\Validation\Exceptions\ValidationException; interface Validatable { + public const TEMPLATE_STANDARD = 'standard'; + public function assert(mixed $input): void; public function check(mixed $input): void; @@ -28,5 +30,7 @@ interface Validatable public function setTemplate(string $template): Validatable; + public function getTemplate(mixed $input): string; + public function validate(mixed $input): bool; } diff --git a/tests/library/Stubs/CompositeSub.php b/tests/library/Stubs/CompositeSub.php index a82d5e05..69e6130f 100644 --- a/tests/library/Stubs/CompositeSub.php +++ b/tests/library/Stubs/CompositeSub.php @@ -13,6 +13,7 @@ use Respect\Validation\Message\Formatter; use Respect\Validation\Message\Stringifier\KeepOriginalStringName; use Respect\Validation\Rules\AbstractComposite; use Respect\Validation\Test\Exceptions\CompositeStubException; +use Respect\Validation\Validatable; final class CompositeSub extends AbstractComposite { @@ -30,6 +31,7 @@ final class CompositeSub extends AbstractComposite input: $input, id: 'CompositeStub', params: $extraParameters, + template: Validatable::TEMPLATE_STANDARD, formatter: new Formatter(static fn ($value) => $value, new KeepOriginalStringName()) ); } diff --git a/tests/unit/Exceptions/NestedValidationExceptionTest.php b/tests/unit/Exceptions/NestedValidationExceptionTest.php index f8bce8c4..463ba84a 100644 --- a/tests/unit/Exceptions/NestedValidationExceptionTest.php +++ b/tests/unit/Exceptions/NestedValidationExceptionTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\Attributes\Test; use Respect\Validation\Message\Formatter; use Respect\Validation\Message\Stringifier\KeepOriginalStringName; use Respect\Validation\Test\TestCase; +use Respect\Validation\Validatable; #[Group('core')] #[CoversClass(NestedValidationException::class)] @@ -23,8 +24,8 @@ final class NestedValidationExceptionTest extends TestCase #[Test] public function getChildrenShouldReturnExceptionAddedByAddRelated(): void { - $composite = new PropertyException('input', 'id', [], new Formatter('strval', new KeepOriginalStringName())); - $node = new IntValException('input', 'id', [], new Formatter('strval', new KeepOriginalStringName())); + $composite = $this->createNestedValidationException(); + $node = $this->createValidationException(); $composite->addChild($node); self::assertCount(1, $composite->getChildren()); self::assertContainsOnly(IntValException::class, $composite->getChildren()); @@ -33,12 +34,34 @@ final class NestedValidationExceptionTest extends TestCase #[Test] public function addingTheSameInstanceShouldAddJustOneSingleReference(): void { - $composite = new PropertyException('input', 'id', [], new Formatter('strval', new KeepOriginalStringName())); - $node = new IntValException('input', 'id', [], new Formatter('strval', new KeepOriginalStringName())); + $composite = $this->createNestedValidationException(); + $node = $this->createValidationException(); $composite->addChild($node); $composite->addChild($node); $composite->addChild($node); self::assertCount(1, $composite->getChildren()); self::assertContainsOnly(IntValException::class, $composite->getChildren()); } + + public function createNestedValidationException(): NestedValidationException + { + return new NestedValidationException( + input: 'input', + id: 'id', + params: [], + template: Validatable::TEMPLATE_STANDARD, + formatter: new Formatter('strval', new KeepOriginalStringName()) + ); + } + + public function createValidationException(): IntValException + { + return new IntValException( + input: 'input', + id: 'id', + params: [], + template: Validatable::TEMPLATE_STANDARD, + formatter: new Formatter('strval', new KeepOriginalStringName()) + ); + } } diff --git a/tests/unit/Exceptions/ValidationExceptionTest.php b/tests/unit/Exceptions/ValidationExceptionTest.php index 1d1348f1..6c53fe66 100644 --- a/tests/unit/Exceptions/ValidationExceptionTest.php +++ b/tests/unit/Exceptions/ValidationExceptionTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\Attributes\Test; use Respect\Validation\Message\Formatter; use Respect\Validation\Message\Stringifier\KeepOriginalStringName; use Respect\Validation\Test\TestCase; +use Respect\Validation\Validatable; use function trim; @@ -25,7 +26,7 @@ final class ValidationExceptionTest extends TestCase #[Test] public function itShouldImplementException(): void { - $sut = new ValidationException('input', 'id', [], $this->createFormatter()); + $sut = $this->createValidationException(); self::assertInstanceOf(Exception::class, $sut); } @@ -34,7 +35,7 @@ final class ValidationExceptionTest extends TestCase public function itShouldRetrieveId(): void { $id = 'my id'; - $sut = new ValidationException('input', $id, [], $this->createFormatter()); + $sut = $this->createValidationException(id: $id); self::assertSame($id, $sut->getId()); } @@ -44,7 +45,7 @@ final class ValidationExceptionTest extends TestCase { $params = ['foo' => true, 'bar' => 23]; - $sut = new ValidationException('input', 'id', $params, $this->createFormatter()); + $sut = $this->createValidationException(params: $params); self::assertSame($params, $sut->getParams()); } @@ -55,7 +56,7 @@ final class ValidationExceptionTest extends TestCase $name = 'any name'; $value = 'any value'; - $sut = new ValidationException('input', 'id', [$name => $value], $this->createFormatter()); + $sut = $this->createValidationException(params: [$name => $value]); self::assertSame($value, $sut->getParam($name)); } @@ -63,23 +64,15 @@ final class ValidationExceptionTest extends TestCase #[Test] public function itShouldReturnNullWhenParameterCanNotBeFound(): void { - $sut = new ValidationException('input', 'id', [], $this->createFormatter()); + $sut = $this->createValidationException(); self::assertNull($sut->getParam('foo')); } - #[Test] - public function itShouldHaveTemplateByDefault(): void - { - $sut = new ValidationException('input', 'id', [], $this->createFormatter()); - - self::assertSame('"input" must be valid', $sut->getMessage()); - } - #[Test] public function itShouldUpdateMode(): void { - $sut = new ValidationException('input', 'id', [], $this->createFormatter()); + $sut = $this->createValidationException(); $sut->updateMode(ValidationException::MODE_NEGATIVE); self::assertSame('"input" must not be valid', $sut->getMessage()); @@ -90,7 +83,7 @@ final class ValidationExceptionTest extends TestCase { $template = 'This is my new template'; - $sut = new ValidationException('input', 'id', [], $this->createFormatter()); + $sut = $this->createValidationException(); $sut->updateTemplate($template); self::assertEquals($template, $sut->getMessage()); @@ -99,7 +92,7 @@ final class ValidationExceptionTest extends TestCase #[Test] public function itShouldTellWhenHasAsCustomUpdateTemplate(): void { - $sut = new ValidationException('input', 'id', [], $this->createFormatter()); + $sut = $this->createValidationException(); self::assertFalse($sut->hasCustomTemplate()); @@ -114,7 +107,7 @@ final class ValidationExceptionTest extends TestCase $template = ' This is my new template '; $expected = trim($template); - $sut = new ValidationException('input', 'id', [], new Formatter('trim', new KeepOriginalStringName())); + $sut = $this->createValidationException(formatter: new Formatter('trim', new KeepOriginalStringName())); $sut->updateTemplate($template); self::assertEquals($expected, $sut->getMessage()); @@ -123,11 +116,11 @@ final class ValidationExceptionTest extends TestCase #[Test] public function itShouldReplacePlaceholders(): void { - $sut = new ValidationException('foo', 'id', ['bar' => 1, 'baz' => 2], $this->createFormatter()); + $sut = $this->createValidationException(params: ['bar' => 1, 'baz' => 2]); $sut->updateTemplate('{{name}} {{bar}} {{baz}}'); self::assertEquals( - '"foo" 1 2', + '"input" 1 2', $sut->getMessage() ); } @@ -135,11 +128,11 @@ final class ValidationExceptionTest extends TestCase #[Test] public function itShouldKeepPlaceholdersThatCanNotReplace(): void { - $sut = new ValidationException('foo', 'id', ['foo' => 1], $this->createFormatter()); + $sut = $this->createValidationException(params: ['foo' => 1]); $sut->updateTemplate('{{name}} {{foo}} {{bar}}'); self::assertEquals( - '"foo" 1 {{bar}}', + '"input" 1 {{bar}}', $sut->getMessage() ); } @@ -147,7 +140,7 @@ final class ValidationExceptionTest extends TestCase #[Test] public function itShouldUpdateParams(): void { - $sut = new ValidationException('input', 'id', ['foo' => 1], $this->createFormatter()); + $sut = $this->createValidationException(params: ['foo' => 1]); $sut->updateTemplate('{{foo}}'); $sut->updateParams(['foo' => 2]); @@ -157,13 +150,21 @@ final class ValidationExceptionTest extends TestCase #[Test] public function itShouldConvertToString(): void { - $sut = new ValidationException('input', 'id', [], $this->createFormatter()); + $sut = $this->createValidationException(); self::assertSame('"input" must be valid', (string) $sut); } - private function createFormatter(): Formatter - { - return new Formatter('strval', new KeepOriginalStringName()); + /** + * @param array $params + */ + private function createValidationException( + mixed $input = 'input', + string $id = 'id', + array $params = [], + string $template = Validatable::TEMPLATE_STANDARD, + Formatter $formatter = new Formatter('strval', new KeepOriginalStringName()) + ): ValidationException { + return new ValidationException($input, $id, $params, $template, $formatter); } }