From b917683d54c5100a9b67f167329caef0414b62dd Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 10 Dec 2023 12:39:54 +0100 Subject: [PATCH] Remove internal validator The package thus becomes an exclusive extension of Laravel and can no longer be used individually. --- src/AbstractRegexRule.php | 2 +- src/AbstractRule.php | 22 +---- src/CallDelegator.php | 108 ---------------------- src/Laravel/ValidationServiceProvider.php | 28 +++++- src/Rule.php | 14 +++ src/Rules/Base64.php | 6 +- src/Rules/Bic.php | 3 +- src/Rules/Camelcase.php | 3 +- src/Rules/Cidr.php | 6 +- src/Rules/Creditcard.php | 9 +- src/Rules/DataUri.php | 17 ++-- src/Rules/Domainname.php | 8 +- src/Rules/Ean.php | 6 +- src/Rules/Gtin.php | 9 +- src/Rules/Hexadecimalcolor.php | 8 +- src/Rules/Htmlclean.php | 6 +- src/Rules/Iban.php | 5 +- src/Rules/Imei.php | 9 +- src/Rules/Isbn.php | 7 +- src/Rules/Isin.php | 9 +- src/Rules/Issn.php | 7 +- src/Rules/Jwt.php | 3 +- src/Rules/Kebabcase.php | 3 +- src/Rules/Lowercase.php | 6 +- src/Rules/Luhn.php | 6 +- src/Rules/MimeType.php | 3 +- src/Rules/Postalcode.php | 6 +- src/Rules/Semver.php | 3 +- src/Rules/Slug.php | 3 +- src/Rules/Snakecase.php | 3 +- src/Rules/Titlecase.php | 6 +- src/Rules/Ulid.php | 8 +- src/Rules/Uppercase.php | 6 +- src/Rules/Username.php | 3 +- src/Traits/CanValidate.php | 41 -------- src/Traits/HasCurrentLocale.php | 16 ---- src/Validator.php | 70 -------------- tests/Rules/Base64Test.php | 11 +-- tests/Rules/BicTest.php | 11 +-- tests/Rules/CamelcaseTest.php | 11 +-- tests/Rules/CidrTest.php | 10 +- tests/Rules/CreditcardTest.php | 11 +-- tests/Rules/DataUriTest.php | 10 +- tests/Rules/DomainnameTest.php | 10 +- tests/Rules/EanTest.php | 24 ++--- tests/Rules/GtinTest.php | 38 ++------ tests/Rules/HexadecimalcolorTest.php | 39 +++----- tests/Rules/HtmlcleanTest.php | 11 +-- tests/Rules/IbanTest.php | 10 +- tests/Rules/ImeiTest.php | 10 +- tests/Rules/IsbnTest.php | 26 ++---- tests/Rules/IsinTest.php | 10 +- tests/Rules/IssnTest.php | 10 +- tests/Rules/JwtTest.php | 10 +- tests/Rules/KebabcaseTest.php | 10 +- tests/Rules/LowercaseTest.php | 10 +- tests/Rules/LuhnTest.php | 10 +- tests/Rules/MimeTypeTest.php | 10 +- tests/Rules/PostalcodeTest.php | 31 +------ tests/Rules/SemverTest.php | 10 +- tests/Rules/SlugTest.php | 10 +- tests/Rules/SnakecaseTest.php | 10 +- tests/Rules/TitlecaseTest.php | 10 +- tests/Rules/UlidTest.php | 10 +- tests/Rules/UppercaseTest.php | 10 +- tests/Rules/UsernameTest.php | 11 +-- tests/ValidatorTest.php | 51 ---------- 67 files changed, 188 insertions(+), 745 deletions(-) delete mode 100644 src/CallDelegator.php create mode 100644 src/Rule.php delete mode 100644 src/Traits/CanValidate.php delete mode 100644 src/Traits/HasCurrentLocale.php delete mode 100644 src/Validator.php delete mode 100644 tests/ValidatorTest.php diff --git a/src/AbstractRegexRule.php b/src/AbstractRegexRule.php index 26458e5..04d0dd9 100644 --- a/src/AbstractRegexRule.php +++ b/src/AbstractRegexRule.php @@ -16,7 +16,7 @@ abstract protected function pattern(): string; * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { return (bool) preg_match($this->pattern(), $value); } diff --git a/src/AbstractRule.php b/src/AbstractRule.php index a25bb74..6b2b7b3 100644 --- a/src/AbstractRule.php +++ b/src/AbstractRule.php @@ -4,15 +4,11 @@ use ReflectionClass; use Closure; -use Illuminate\Filesystem\Filesystem; -use Illuminate\Translation\FileLoader; -use Illuminate\Translation\Translator; +use Illuminate\Contracts\Validation\ValidationRule; -abstract class AbstractRule +abstract class AbstractRule implements Rule, ValidationRule { - use Traits\HasCurrentLocale; - - abstract public function passes(string $attribute, mixed $value): bool; + abstract public function isValid(mixed $value): bool; /** * Laravel Framwork validation method @@ -24,7 +20,7 @@ abstract public function passes(string $attribute, mixed $value): bool; */ public function validate(string $attribute, mixed $value, Closure $fail): void { - if (!$this->passes($attribute, $value)) { + if (!$this->isValid($value)) { $fail($this->message())->translate(); } } @@ -56,16 +52,8 @@ public function message() if ($message === $key) { return trans('validation::' . $key); } - - return $message; } - return $this->translatorInstance()->get($key); - } - - protected function translatorInstance(): Translator - { - $loader = new FileLoader(new Filesystem(), __DIR__ . '/lang'); - return new Translator($loader, self::getCurrentLocale()); + return $key; } } diff --git a/src/CallDelegator.php b/src/CallDelegator.php deleted file mode 100644 index 798abb2..0000000 --- a/src/CallDelegator.php +++ /dev/null @@ -1,108 +0,0 @@ -parse('action')) { - 'assert' => 'assert', - default => 'validate', - }; - } - - public function isAssertion(): bool - { - return $this->getAction() === 'assert'; - } - - /** - * Get value to validate - * - * @return mixed - */ - public function getValue(): mixed - { - return isset($this->arguments[0]) ? $this->arguments[0] : null; - } - - /** - * Get validation rule from current call - * - * @return ValidationRule - */ - public function getRule(): ValidationRule - { - $classname = $this->getRuleClassname(); - - return new $classname(...$this->getValidationAttributes()); - } - - /** - * Get rule option attribtues - * - * @return array - */ - protected function getValidationAttributes(): array - { - $attributes = $this->arguments; - array_shift($attributes); - - return $attributes; - } - - /** - * Get classname according to current call - * - * @return string - */ - private function getRuleClassname(): string - { - $classname = sprintf('Intervention\Validation\Rules\%s', $this->parse('rule')); - - if (!class_exists($classname)) { - throw new NotExistingRuleException( - "Rule does not exist (" . $classname . ")" - ); - } - - return $classname; - } - - /** - * Parse information from current call - * - * @param string $key - * @return string - */ - protected function parse(string $key): ?string - { - $pattern = "/^(?Pis|assert)(?P.*)$/"; - $result = (bool) preg_match($pattern, $this->name, $matches); - - if ($result === false) { - trigger_error( - "Error: Call to undefined method " . $this->name, - E_USER_ERROR - ); - } - - return array_key_exists($key, $matches) ? $matches[$key] : null; - } -} diff --git a/src/Laravel/ValidationServiceProvider.php b/src/Laravel/ValidationServiceProvider.php index 17ac85f..536d158 100644 --- a/src/Laravel/ValidationServiceProvider.php +++ b/src/Laravel/ValidationServiceProvider.php @@ -3,6 +3,8 @@ namespace Intervention\Validation\Laravel; use Illuminate\Support\ServiceProvider; +use Intervention\Validation\Exceptions\NotExistingRuleException; +use Intervention\Validation\Rule; use Intervention\Validation\Validator; class ValidationServiceProvider extends ServiceProvider @@ -25,17 +27,33 @@ public function boot() $this->app['validator']->extend( $rulename, function ($attribute, $value, $parameters, $validator) use ($rulename) { - return forward_static_call( - [Validator::class, 'is' . ucfirst($rulename)], - $value, - data_get($parameters, 0) - ); + return $this->getInterventionRule($rulename, $parameters) + ->isValid($value); }, $this->getErrorMessage($rulename) ); } } + /** + * Return rule object for given shortname + * + * @param string $rulename + * @param array $parameters + * @return Rule + * @throws NotExistingRuleException + */ + private function getInterventionRule(string $rulename, array $parameters): Rule + { + $classname = sprintf("Intervention\Validation\Rules\%s", ucfirst($rulename)); + + if (!class_exists($classname)) { + throw new NotExistingRuleException("Rule " . $rulename . " does not exist."); + } + + return new $classname(...$parameters); + } + /** * Return error message of given rule shortname * diff --git a/src/Rule.php b/src/Rule.php new file mode 100644 index 0000000..670e388 --- /dev/null +++ b/src/Rule.php @@ -0,0 +1,14 @@ +hasValidLength($value) && parent::passes($attribute, $value); + return $this->hasValidLength($value) && parent::isValid($value); } /** diff --git a/src/Rules/DataUri.php b/src/Rules/DataUri.php index 391887e..1c16d50 100644 --- a/src/Rules/DataUri.php +++ b/src/Rules/DataUri.php @@ -2,22 +2,17 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -use Intervention\Validation\Traits\CanValidate; -class DataUri extends AbstractRule implements ValidationRule +class DataUri extends AbstractRule { - use CanValidate; - /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { $info = $this->dataUriInfo($value); if (! $info->isValid()) { @@ -35,14 +30,14 @@ public function passes(string $attribute, mixed $value): bool return true; } - protected function isValidMimeType($value): bool + protected function isValidMimeType(mixed $value): bool { - return $this->getValidator(['value' => $value], ['value' => ['required', new MimeType()]])->passes(); + return (new MimeType())->isValid($value); } - protected function isValidBase64EncodedValue($value): bool + protected function isValidBase64EncodedValue(mixed $value): bool { - return $this->getValidator(['value' => $value], ['value' => ['required', new Base64()]])->passes(); + return (new Base64())->isValid($value); } /** diff --git a/src/Rules/Domainname.php b/src/Rules/Domainname.php index 43c2fff..a0437ee 100644 --- a/src/Rules/Domainname.php +++ b/src/Rules/Domainname.php @@ -2,19 +2,17 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Domainname extends AbstractRule implements ValidationRule +class Domainname extends AbstractRule { /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { $labels = $this->getLabels($value); // get labels of domainname $tld = end($labels); // most right label of domainname is tld @@ -44,7 +42,7 @@ public function passes(string $attribute, mixed $value): bool * * @return array */ - private function getLabels($value): array + private function getLabels(mixed $value): array { return explode('.', $this->idnToAscii($value)); } diff --git a/src/Rules/Ean.php b/src/Rules/Ean.php index 567d205..9f2932b 100644 --- a/src/Rules/Ean.php +++ b/src/Rules/Ean.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Ean extends AbstractRule implements ValidationRule +class Ean extends AbstractRule { /** * Valid lengths @@ -33,11 +32,10 @@ public function __construct(?int $length = null) /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { return is_numeric($value) && $this->hasAllowedLength($value) && $this->checksumMatches($value); } diff --git a/src/Rules/Gtin.php b/src/Rules/Gtin.php index b9c8d49..739a3d4 100644 --- a/src/Rules/Gtin.php +++ b/src/Rules/Gtin.php @@ -2,9 +2,7 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; - -class Gtin extends Ean implements ValidationRule +class Gtin extends Ean { /** * Valid lengths @@ -28,11 +26,10 @@ class Gtin extends Ean implements ValidationRule * - GTIN-14 will be checked as EAN-13 after cropping first char * - GTIN-12 will be checked as EAN-13 after adding leading zero * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { if (!is_numeric($value)) { return false; @@ -45,7 +42,7 @@ public function passes(string $attribute, mixed $value): bool switch (strlen($value)) { case 8: case 13: - return parent::passes($attribute, $value); + return parent::isValid($value); case 14: return parent::checksumMatches(substr($value, 1)); diff --git a/src/Rules/Hexadecimalcolor.php b/src/Rules/Hexadecimalcolor.php index 6f30b3b..bfecc43 100644 --- a/src/Rules/Hexadecimalcolor.php +++ b/src/Rules/Hexadecimalcolor.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Hexadecimalcolor extends AbstractRegexRule implements ValidationRule +class Hexadecimalcolor extends AbstractRegexRule { /** * Allowed lengths of hexcolor @@ -40,17 +39,16 @@ protected function pattern(): string /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { if (!$this->hasAllowedLength($value)) { return false; } - return parent::passes($attribute, $value); + return parent::isValid($value); } /** diff --git a/src/Rules/Htmlclean.php b/src/Rules/Htmlclean.php index 2011e8e..bae245f 100644 --- a/src/Rules/Htmlclean.php +++ b/src/Rules/Htmlclean.php @@ -2,19 +2,17 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Htmlclean extends AbstractRule implements ValidationRule +class Htmlclean extends AbstractRule { /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { return (strip_tags($value) == $value); } diff --git a/src/Rules/Iban.php b/src/Rules/Iban.php index fb86054..3622715 100644 --- a/src/Rules/Iban.php +++ b/src/Rules/Iban.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Iban extends AbstractRule implements ValidationRule +class Iban extends AbstractRule { /** * IBAN lengths for countries @@ -127,7 +126,7 @@ class Iban extends AbstractRule implements ValidationRule * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { // normalize value $value = str_replace(' ', '', strtoupper($value)); diff --git a/src/Rules/Imei.php b/src/Rules/Imei.php index 0688810..093b78b 100644 --- a/src/Rules/Imei.php +++ b/src/Rules/Imei.php @@ -2,20 +2,17 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; - -class Imei extends Luhn implements ValidationRule +class Imei extends Luhn { /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { - return $this->hasValidLength($value) && parent::passes($attribute, $value); + return $this->hasValidLength($value) && parent::isValid($value); } /** diff --git a/src/Rules/Isbn.php b/src/Rules/Isbn.php index 1215088..4d73aa9 100644 --- a/src/Rules/Isbn.php +++ b/src/Rules/Isbn.php @@ -2,9 +2,7 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; - -class Isbn extends Ean implements ValidationRule +class Isbn extends Ean { /** * Valid lengths @@ -19,11 +17,10 @@ class Isbn extends Ean implements ValidationRule /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { // normalize value $value = preg_replace("/[^0-9x]/i", '', $value); diff --git a/src/Rules/Isin.php b/src/Rules/Isin.php index 2afdfd4..a7b556e 100644 --- a/src/Rules/Isin.php +++ b/src/Rules/Isin.php @@ -2,9 +2,7 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; - -class Isin extends Luhn implements ValidationRule +class Isin extends Luhn { /** * Chars to calculate checksum @@ -43,13 +41,12 @@ class Isin extends Luhn implements ValidationRule /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { - return parent::passes($attribute, $this->normalize($value)); + return parent::isValid($this->normalize($value)); } /** diff --git a/src/Rules/Issn.php b/src/Rules/Issn.php index 88f2168..6466c66 100644 --- a/src/Rules/Issn.php +++ b/src/Rules/Issn.php @@ -2,19 +2,18 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Issn extends AbstractRegexRule implements ValidationRule +class Issn extends AbstractRegexRule { protected function pattern(): string { return "/^[0-9]{4}-[0-9]{3}[0-9xX]$/"; } - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { - return parent::passes($attribute, $value) && $this->checkSumMatches($value); + return parent::isValid($value) && $this->checkSumMatches($value); } /** diff --git a/src/Rules/Jwt.php b/src/Rules/Jwt.php index 4f1a1d1..58f4f48 100644 --- a/src/Rules/Jwt.php +++ b/src/Rules/Jwt.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Jwt extends AbstractRegexRule implements ValidationRule +class Jwt extends AbstractRegexRule { protected function pattern(): string { diff --git a/src/Rules/Kebabcase.php b/src/Rules/Kebabcase.php index 0286c42..06c1011 100644 --- a/src/Rules/Kebabcase.php +++ b/src/Rules/Kebabcase.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Kebabcase extends AbstractRegexRule implements ValidationRule +class Kebabcase extends AbstractRegexRule { protected function pattern(): string { diff --git a/src/Rules/Lowercase.php b/src/Rules/Lowercase.php index 52e1e5c..16bc9ba 100644 --- a/src/Rules/Lowercase.php +++ b/src/Rules/Lowercase.php @@ -2,19 +2,17 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Lowercase extends AbstractRule implements ValidationRule +class Lowercase extends AbstractRule { /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { return $value === $this->getLowerCaseValue($value); } diff --git a/src/Rules/Luhn.php b/src/Rules/Luhn.php index 7dadd0f..4e56bb3 100644 --- a/src/Rules/Luhn.php +++ b/src/Rules/Luhn.php @@ -2,19 +2,17 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Luhn extends AbstractRule implements ValidationRule +class Luhn extends AbstractRule { /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { return $this->checksumIsValid($this->getChecksum($value)); } diff --git a/src/Rules/MimeType.php b/src/Rules/MimeType.php index 5071c12..7283275 100644 --- a/src/Rules/MimeType.php +++ b/src/Rules/MimeType.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class MimeType extends AbstractRegexRule implements ValidationRule +class MimeType extends AbstractRegexRule { protected function pattern(): string { diff --git a/src/Rules/Postalcode.php b/src/Rules/Postalcode.php index ebaf4a7..172ff83 100644 --- a/src/Rules/Postalcode.php +++ b/src/Rules/Postalcode.php @@ -3,10 +3,9 @@ namespace Intervention\Validation\Rules; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Postalcode extends AbstractRule implements ValidationRule, DataAwareRule +class Postalcode extends AbstractRule implements DataAwareRule { /** * Country code to match postal code @@ -73,11 +72,10 @@ public static function resolve(callable $callback): self /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { if ($pattern = $this->getPattern()) { return (bool) preg_match($pattern, $value); diff --git a/src/Rules/Semver.php b/src/Rules/Semver.php index 9eded95..de24a03 100644 --- a/src/Rules/Semver.php +++ b/src/Rules/Semver.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Semver extends AbstractRegexRule implements ValidationRule +class Semver extends AbstractRegexRule { protected function pattern(): string { diff --git a/src/Rules/Slug.php b/src/Rules/Slug.php index 50d0fa1..064dbfa 100644 --- a/src/Rules/Slug.php +++ b/src/Rules/Slug.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Slug extends AbstractRegexRule implements ValidationRule +class Slug extends AbstractRegexRule { protected function pattern(): string { diff --git a/src/Rules/Snakecase.php b/src/Rules/Snakecase.php index b237d11..cfa0b16 100644 --- a/src/Rules/Snakecase.php +++ b/src/Rules/Snakecase.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Snakecase extends AbstractRegexRule implements ValidationRule +class Snakecase extends AbstractRegexRule { protected function pattern(): string { diff --git a/src/Rules/Titlecase.php b/src/Rules/Titlecase.php index 755f70c..f4c741f 100644 --- a/src/Rules/Titlecase.php +++ b/src/Rules/Titlecase.php @@ -2,19 +2,17 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Titlecase extends AbstractRule implements ValidationRule +class Titlecase extends AbstractRule { /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { foreach ($this->getWords($value) as $word) { if (! $this->isValidWord($word)) { diff --git a/src/Rules/Ulid.php b/src/Rules/Ulid.php index 36b9913..512d8bb 100644 --- a/src/Rules/Ulid.php +++ b/src/Rules/Ulid.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Ulid extends AbstractRegexRule implements ValidationRule +class Ulid extends AbstractRegexRule { protected function pattern(): string { @@ -15,13 +14,12 @@ protected function pattern(): string /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { - if (! parent::passes($attribute, $value)) { + if (! parent::isValid($value)) { return false; } diff --git a/src/Rules/Uppercase.php b/src/Rules/Uppercase.php index d49bbb6..d84ab1e 100644 --- a/src/Rules/Uppercase.php +++ b/src/Rules/Uppercase.php @@ -2,19 +2,17 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRule; -class Uppercase extends AbstractRule implements ValidationRule +class Uppercase extends AbstractRule { /** * Determine if the validation rule passes. * - * @param string $attribute * @param mixed $value * @return bool */ - public function passes(string $attribute, mixed $value): bool + public function isValid(mixed $value): bool { return $value === $this->getUpperCaseValue($value); } diff --git a/src/Rules/Username.php b/src/Rules/Username.php index 9dd3e2e..5d98b93 100644 --- a/src/Rules/Username.php +++ b/src/Rules/Username.php @@ -2,10 +2,9 @@ namespace Intervention\Validation\Rules; -use Illuminate\Contracts\Validation\ValidationRule; use Intervention\Validation\AbstractRegexRule; -class Username extends AbstractRegexRule implements ValidationRule +class Username extends AbstractRegexRule { /** * Pattern for "valid" username diff --git a/src/Traits/CanValidate.php b/src/Traits/CanValidate.php deleted file mode 100644 index e64db68..0000000 --- a/src/Traits/CanValidate.php +++ /dev/null @@ -1,41 +0,0 @@ -extend( - $rulename, - function ($attribute, $value, $parameters, $validator) use ($rulename) { - return forward_static_call( - [InterventionValidator::class, 'is' . ucfirst($rulename)], - $value, - data_get($parameters, 0) - ); - }, - $translator->get('validation::validation.' . $rulename) - ); - } - - $factory->resolver(function ($translator, $data, $rules, $messages, $customAttributes) { - return new IlluminateValidator($translator, $data, $rules, $messages, $customAttributes); - }); - - return $factory->make($data, $rules); - } -} diff --git a/src/Traits/HasCurrentLocale.php b/src/Traits/HasCurrentLocale.php deleted file mode 100644 index 9c2020f..0000000 --- a/src/Traits/HasCurrentLocale.php +++ /dev/null @@ -1,16 +0,0 @@ -make($data, $rules); - } - - /** - * Magic method for static calls, to call single rules directly - * - * @param string $name - * @param array $arguments - * @return boolean - */ - public static function __callStatic(string $name, array $arguments): bool - { - $delegation = new CallDelegator($name, $arguments); - $rule = $delegation->getRule(); - $passes = self::make(['value' => $delegation->getValue()], ['value' => ['required', $rule]])->passes(); - - if ($delegation->isAssertion() && $passes === false) { - throw new ValidationException('Failed asserting that value applies to rule "' . get_class($rule) . '".'); - } - - return $passes; - } - - protected static function factory(): Factory - { - $loader = new FileLoader(new Filesystem(), __DIR__ . '/lang'); - $translator = new Translator($loader, self::getCurrentLocale()); - $factory = new Factory($translator, new Container()); - $factory->resolver(function ($translator, $data, $rules, $messages, $customAttributes) { - return new IlluminateValidator($translator, $data, $rules, $messages, $customAttributes); - }); - - foreach (self::getRuleShortnames() as $rulename) { - $factory->extend($rulename, function ($attribute, $value, $parameters, $validator) use ($rulename) { - return forward_static_call( - [self::class, 'is' . ucfirst($rulename)], - $value, - data_get($parameters, 0) - ); - }); - } - - return $factory; - } - - public static function getRuleShortnames(): array - { - return array_map(function ($filename) { - return mb_strtolower(substr($filename, 0, -4)); - }, array_diff(scandir(__DIR__ . '/Rules'), ['.', '..'])); - } -} diff --git a/tests/Rules/Base64Test.php b/tests/Rules/Base64Test.php index 988b29c..3a8c61c 100644 --- a/tests/Rules/Base64Test.php +++ b/tests/Rules/Base64Test.php @@ -3,24 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Base64; -use Intervention\Validation\Traits\CanValidate; -use Intervention\Validation\Validator; use PHPUnit\Framework\TestCase; class Base64Test extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Base64()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['base64']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Base64())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/BicTest.php b/tests/Rules/BicTest.php index a8a0656..d5785fd 100644 --- a/tests/Rules/BicTest.php +++ b/tests/Rules/BicTest.php @@ -3,24 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Bic; -use Intervention\Validation\Traits\CanValidate; -use Intervention\Validation\Validator; use PHPUnit\Framework\TestCase; class BicTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Bic()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['bic']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Bic())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/CamelcaseTest.php b/tests/Rules/CamelcaseTest.php index 057e2d8..23ad259 100644 --- a/tests/Rules/CamelcaseTest.php +++ b/tests/Rules/CamelcaseTest.php @@ -3,24 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Camelcase; -use Intervention\Validation\Traits\CanValidate; -use Intervention\Validation\Validator; use PHPUnit\Framework\TestCase; class CamelcaseTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Camelcase()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['camelcase']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Camelcase())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/CidrTest.php b/tests/Rules/CidrTest.php index 420a872..0b3a2f0 100644 --- a/tests/Rules/CidrTest.php +++ b/tests/Rules/CidrTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Cidr; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class CidrTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Cidr()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['cidr']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Cidr())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/CreditcardTest.php b/tests/Rules/CreditcardTest.php index 6acbfcd..4a4cda4 100644 --- a/tests/Rules/CreditcardTest.php +++ b/tests/Rules/CreditcardTest.php @@ -3,24 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Creditcard; -use Intervention\Validation\Traits\CanValidate; -use Intervention\Validation\Validator; use PHPUnit\Framework\TestCase; class CreditcardTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Creditcard()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['creditcard']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Creditcard())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/DataUriTest.php b/tests/Rules/DataUriTest.php index ce7b824..1b8f4af 100644 --- a/tests/Rules/DataUriTest.php +++ b/tests/Rules/DataUriTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\DataUri; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class DataUriTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new DataUri()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['datauri']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new DataUri())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/DomainnameTest.php b/tests/Rules/DomainnameTest.php index 9bb9de4..9c2095a 100644 --- a/tests/Rules/DomainnameTest.php +++ b/tests/Rules/DomainnameTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Domainname; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class DomainnameTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Domainname()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['domainname']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Domainname())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/EanTest.php b/tests/Rules/EanTest.php index 58b604e..876199c 100644 --- a/tests/Rules/EanTest.php +++ b/tests/Rules/EanTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Ean; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class EanTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Ean()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['ean']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Ean())->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -27,11 +21,8 @@ public function testValidation($result, $value) */ public function testValidationEan13($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Ean(13)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['ean:13']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Ean(13))->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -39,11 +30,8 @@ public function testValidationEan13($result, $value) */ public function testValidationEan8($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Ean(8)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['ean:8']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Ean(8))->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/GtinTest.php b/tests/Rules/GtinTest.php index 1cc73cc..de48747 100644 --- a/tests/Rules/GtinTest.php +++ b/tests/Rules/GtinTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Gtin; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class GtinTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Gtin()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['gtin']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Gtin())->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -27,11 +21,8 @@ public function testValidation($result, $value) */ public function testValidationGtin8($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Gtin(8)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['gtin:8']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Gtin(8))->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -39,11 +30,8 @@ public function testValidationGtin8($result, $value) */ public function testValidationGtin12($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Gtin(12)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['gtin:12']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Gtin(12))->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -51,11 +39,8 @@ public function testValidationGtin12($result, $value) */ public function testValidationGtin13($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Gtin(13)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['gtin:13']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Gtin(13))->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -63,11 +48,8 @@ public function testValidationGtin13($result, $value) */ public function testValidationGtin14($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Gtin(14)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['gtin:14']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Gtin(14))->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/HexadecimalcolorTest.php b/tests/Rules/HexadecimalcolorTest.php index 04305cc..70129dc 100644 --- a/tests/Rules/HexadecimalcolorTest.php +++ b/tests/Rules/HexadecimalcolorTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Hexadecimalcolor; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class HexadecimalcolorTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Hexadecimalcolor()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['hexadecimalcolor']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Hexadecimalcolor())->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -27,11 +21,8 @@ public function testValidation($result, $value) */ public function testValidationShort($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Hexadecimalcolor(3)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['hexadecimalcolor:3']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Hexadecimalcolor(3))->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -39,11 +30,8 @@ public function testValidationShort($result, $value) */ public function testValidationLongAlpha($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Hexadecimalcolor(8)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['hexadecimalcolor:8']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Hexadecimalcolor(8))->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -51,11 +39,8 @@ public function testValidationLongAlpha($result, $value) */ public function testValidationShortAlpha($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Hexadecimalcolor(4)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['hexadecimalcolor:4']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Hexadecimalcolor(4))->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -63,12 +48,10 @@ public function testValidationShortAlpha($result, $value) */ public function testValidationLong($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Hexadecimalcolor(6)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['hexadecimalcolor:6']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Hexadecimalcolor(6))->isValid($value); + $this->assertEquals($result, $valid); } + public function dataProvider() { return [ diff --git a/tests/Rules/HtmlcleanTest.php b/tests/Rules/HtmlcleanTest.php index 64396a4..463b0ad 100644 --- a/tests/Rules/HtmlcleanTest.php +++ b/tests/Rules/HtmlcleanTest.php @@ -3,24 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Htmlclean; -use Intervention\Validation\Traits\CanValidate; -use Intervention\Validation\Validator; use PHPUnit\Framework\TestCase; class HtmlcleanTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Htmlclean()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['htmlclean']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Htmlclean())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/IbanTest.php b/tests/Rules/IbanTest.php index ae1a0b1..c5b7c4c 100644 --- a/tests/Rules/IbanTest.php +++ b/tests/Rules/IbanTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Iban; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class IbanTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Iban()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['iban']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Iban())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/ImeiTest.php b/tests/Rules/ImeiTest.php index 505ecfb..56c3a03 100644 --- a/tests/Rules/ImeiTest.php +++ b/tests/Rules/ImeiTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Imei; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class ImeiTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Imei()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['imei']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Imei())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/IsbnTest.php b/tests/Rules/IsbnTest.php index cbb7364..950b91a 100644 --- a/tests/Rules/IsbnTest.php +++ b/tests/Rules/IsbnTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Isbn; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class IsbnTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Isbn()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['isbn']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Isbn())->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -27,11 +21,8 @@ public function testValidation($result, $value) */ public function testValidationShort($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Isbn(10)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['isbn:10']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Isbn(10))->isValid($value); + $this->assertEquals($result, $valid); } /** @@ -39,11 +30,8 @@ public function testValidationShort($result, $value) */ public function testValidationLong($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Isbn(13)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['isbn:13']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Isbn(13))->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() @@ -126,4 +114,4 @@ public function dataProviderLong() [false, '0123456789012'], ]; } -} \ No newline at end of file +} diff --git a/tests/Rules/IsinTest.php b/tests/Rules/IsinTest.php index 0e542b2..2f804bc 100644 --- a/tests/Rules/IsinTest.php +++ b/tests/Rules/IsinTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Isin; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class IsinTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Isin()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['isin']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Isin())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/IssnTest.php b/tests/Rules/IssnTest.php index 26d2314..c672c87 100644 --- a/tests/Rules/IssnTest.php +++ b/tests/Rules/IssnTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Issn; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class IssnTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Issn()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['issn']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Issn())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/JwtTest.php b/tests/Rules/JwtTest.php index 2916bb2..9470f74 100644 --- a/tests/Rules/JwtTest.php +++ b/tests/Rules/JwtTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Jwt; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class JwtTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Jwt()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['jwt']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Jwt())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/KebabcaseTest.php b/tests/Rules/KebabcaseTest.php index 1174945..8d6891c 100644 --- a/tests/Rules/KebabcaseTest.php +++ b/tests/Rules/KebabcaseTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Kebabcase; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class KebabcaseTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Kebabcase()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['kebabcase']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Kebabcase())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/LowercaseTest.php b/tests/Rules/LowercaseTest.php index f5fdb29..568e036 100644 --- a/tests/Rules/LowercaseTest.php +++ b/tests/Rules/LowercaseTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Lowercase; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class LowercaseTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Lowercase()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['lowercase']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Lowercase())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/LuhnTest.php b/tests/Rules/LuhnTest.php index ef0b59c..b979efe 100644 --- a/tests/Rules/LuhnTest.php +++ b/tests/Rules/LuhnTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Luhn; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class LuhnTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Luhn()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['luhn']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Luhn())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/MimeTypeTest.php b/tests/Rules/MimeTypeTest.php index 9282911..c1edfa8 100644 --- a/tests/Rules/MimeTypeTest.php +++ b/tests/Rules/MimeTypeTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\MimeType; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class MimeTypeTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new MimeType()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['mimetype']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new MimeType())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/PostalcodeTest.php b/tests/Rules/PostalcodeTest.php index dd37bef..07d4334 100644 --- a/tests/Rules/PostalcodeTest.php +++ b/tests/Rules/PostalcodeTest.php @@ -3,44 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Postalcode; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class PostalcodeTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidationConstructor($result, $countrycode, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Postalcode($countrycode)]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['postalcode:' . $countrycode]]); - $this->assertEquals($result, $validator->passes()); - } - - /** - * @dataProvider dataProvider - */ - public function testValidationStatic($result, $countrycode, $value) - { - $validator = $this->getValidator(['value' => $value], ['value' => [Postalcode::countrycode($countrycode)]]); - $this->assertEquals($result, $validator->passes()); - } - - /** - * @dataProvider dataProvider - */ - public function testValidationStaticCallback($result, $countrycode, $value) - { - - $validator = $this->getValidator(['value' => $value], ['value' => [Postalcode::resolve(function () use ($countrycode) { - return $countrycode; - })]]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Postalcode($countrycode))->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/SemverTest.php b/tests/Rules/SemverTest.php index cbc162b..bac0f5e 100644 --- a/tests/Rules/SemverTest.php +++ b/tests/Rules/SemverTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Semver; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class SemverTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Semver()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['semver']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Semver())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/SlugTest.php b/tests/Rules/SlugTest.php index 880c473..261c516 100644 --- a/tests/Rules/SlugTest.php +++ b/tests/Rules/SlugTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Slug; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class SlugTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Slug()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['slug']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Slug())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/SnakecaseTest.php b/tests/Rules/SnakecaseTest.php index d09882b..8a78e04 100644 --- a/tests/Rules/SnakecaseTest.php +++ b/tests/Rules/SnakecaseTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Snakecase; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class SnakecaseTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Snakecase()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['snakecase']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Snakecase())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/TitlecaseTest.php b/tests/Rules/TitlecaseTest.php index 35af4d0..10ead3c 100644 --- a/tests/Rules/TitlecaseTest.php +++ b/tests/Rules/TitlecaseTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Titlecase; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class TitlecaseTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Titlecase()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['titlecase']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Titlecase())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/UlidTest.php b/tests/Rules/UlidTest.php index 9d54f15..d680548 100644 --- a/tests/Rules/UlidTest.php +++ b/tests/Rules/UlidTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Ulid; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class UlidTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Ulid()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['ulid']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Ulid())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/UppercaseTest.php b/tests/Rules/UppercaseTest.php index f3f97cb..2f900b5 100644 --- a/tests/Rules/UppercaseTest.php +++ b/tests/Rules/UppercaseTest.php @@ -3,23 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Uppercase; -use Intervention\Validation\Traits\CanValidate; use PHPUnit\Framework\TestCase; class UppercaseTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Uppercase()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['uppercase']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Uppercase())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/Rules/UsernameTest.php b/tests/Rules/UsernameTest.php index 3a29003..48edfb1 100644 --- a/tests/Rules/UsernameTest.php +++ b/tests/Rules/UsernameTest.php @@ -3,24 +3,17 @@ namespace Intervention\Validation\Test\Rules; use Intervention\Validation\Rules\Username; -use Intervention\Validation\Traits\CanValidate; -use Intervention\Validation\Validator; use PHPUnit\Framework\TestCase; class UsernameTest extends TestCase { - use CanValidate; - /** * @dataProvider dataProvider */ public function testValidation($result, $value) { - $validator = $this->getValidator(['value' => $value], ['value' => [new Username()]]); - $this->assertEquals($result, $validator->passes()); - - $validator = $this->getValidator(['value' => $value], ['value' => ['username']]); - $this->assertEquals($result, $validator->passes()); + $valid = (new Username())->isValid($value); + $this->assertEquals($result, $valid); } public function dataProvider() diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php deleted file mode 100644 index e976e36..0000000 --- a/tests/ValidatorTest.php +++ /dev/null @@ -1,51 +0,0 @@ -assertInstanceOf(IlluminateValidator::class, $validator); - } - - public function testIsRule() - { - $this->assertTrue(Validator::isHexadecimalcolor('ccc')); - $this->assertFalse(Validator::isHexadecimalcolor('zzz')); - $this->assertFalse(Validator::isHexadecimalcolor('ccc', 6)); - $this->assertTrue(Validator::isHexadecimalcolor('cccccc', 6)); - } - - public function testAssertFail() - { - $this->expectException(ValidationException::class); - Validator::assertHexadecimalcolor('zzz'); - } - - public function testAssertSuccess() - { - $result = Validator::assertHexadecimalcolor('ccc'); - $this->assertTrue($result); - } - - public function testErrorMessages() - { - locale_set_default('en'); - $validator = Validator::make(['value' => 'zzz'], ['value' => new Hexadecimalcolor()]); - $message = 'The value must be a valid hexadecimal color code.'; - $this->assertEquals($message, $validator->errors()->first('value')); - - locale_set_default('de'); - $validator = Validator::make(['value' => 'zzz'], ['value' => new Hexadecimalcolor()]); - $message = 'Der Wert value muss einen gültigen hexadezimalen Farbwert enthalten.'; - $this->assertEquals($message, $validator->errors()->first('value')); - } -}