Skip to content

Commit

Permalink
Remove internal validator
Browse files Browse the repository at this point in the history
The package thus becomes an exclusive extension of Laravel and
can no longer be used individually.
  • Loading branch information
olivervogel committed Dec 10, 2023
1 parent f6b6fbc commit b917683
Show file tree
Hide file tree
Showing 67 changed files with 188 additions and 745 deletions.
2 changes: 1 addition & 1 deletion src/AbstractRegexRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 19 in src/AbstractRegexRule.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.1

PHPDoc tag @param references unknown parameter: $attribute

Check failure on line 19 in src/AbstractRegexRule.php

View workflow job for this annotation

GitHub Actions / Testing on PHP 8.2

PHPDoc tag @param references unknown parameter: $attribute
{
return (bool) preg_match($this->pattern(), $value);
}
Expand Down
22 changes: 5 additions & 17 deletions src/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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;
}
}
108 changes: 0 additions & 108 deletions src/CallDelegator.php

This file was deleted.

28 changes: 23 additions & 5 deletions src/Laravel/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down
14 changes: 14 additions & 0 deletions src/Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Intervention\Validation;

interface Rule
{
/**
* Checks if the given value is valid in the scope of the current rule
*
* @param mixed $value
* @return bool
*/
public function isValid(mixed $value): bool;
}
6 changes: 2 additions & 4 deletions src/Rules/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace Intervention\Validation\Rules;

use Illuminate\Contracts\Validation\ValidationRule;
use Intervention\Validation\AbstractRule;

class Base64 extends AbstractRule implements ValidationRule
class Base64 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 base64_encode(base64_decode($value, true)) === $value;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Bic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Intervention\Validation\Rules;

use Illuminate\Contracts\Validation\ValidationRule;
use Intervention\Validation\AbstractRegexRule;

class Bic extends AbstractRegexRule implements ValidationRule
class Bic extends AbstractRegexRule
{
protected function pattern(): string
{
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Camelcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Intervention\Validation\Rules;

use Illuminate\Contracts\Validation\ValidationRule;
use Intervention\Validation\AbstractRegexRule;

class Camelcase extends AbstractRegexRule implements ValidationRule
class Camelcase extends AbstractRegexRule
{
protected function pattern(): string
{
Expand Down
6 changes: 2 additions & 4 deletions src/Rules/Cidr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace Intervention\Validation\Rules;

use Illuminate\Contracts\Validation\ValidationRule;
use Intervention\Validation\AbstractRule;

class Cidr extends AbstractRule implements ValidationRule
class Cidr 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
{
// A CIDR should consist of an IP part and a mask bit number delimited by a `/`

Expand Down
9 changes: 3 additions & 6 deletions src/Rules/Creditcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

namespace Intervention\Validation\Rules;

use Illuminate\Contracts\Validation\ValidationRule;

class Creditcard extends Luhn implements ValidationRule
class Creditcard 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);
}

/**
Expand Down
17 changes: 6 additions & 11 deletions src/Rules/DataUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Rules/Domainname.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
Expand Down
Loading

0 comments on commit b917683

Please sign in to comment.