Skip to content

Commit

Permalink
fix Laravel 9 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
miken32 committed Oct 19, 2023
1 parent 9361f43 commit 4ad4da1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
},
"require-dev": {
"fakerphp/faker": "^1.0",
"orchestra/testbench": "^8.0",
"phpunit/phpunit": "^10.1"
"orchestra/testbench": "^7.0|^8.0",
"phpunit/phpunit": "^9.6|^10.1"
},
"autoload": {
"psr-4": {
Expand Down
30 changes: 25 additions & 5 deletions src/Rules/BaseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
namespace Miken32\Validation\Network\Rules;

use Closure;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Translation\CreatesPotentiallyTranslatedStrings;
use Illuminate\Translation\PotentiallyTranslatedString;
use Illuminate\Validation\Validator;
use Stringable;

if (!interface_exists(ValidationRule::class)) {
// for Laravel 9 compatibility
class_alias(Rule::class, ValidationRule::class);
}

abstract class BaseRule implements ValidatorAwareRule, ValidationRule
{
use CreatesPotentiallyTranslatedStrings;
protected Validator $validator;

/** @var bool indicates whether we've been called as a result of Validator::extend() */
Expand All @@ -33,7 +37,11 @@ public function extend(string $attribute, string $value, array $parameters, Vali
return $this->doValidation($value, ...$parameters);
}

public function setValidator(Validator $validator): static
/**
* @param Validator $validator
* @return static
*/
public function setValidator($validator): static
{
$this->validator = $validator;

Expand All @@ -45,7 +53,7 @@ public function setValidator(Validator $validator): static
*
* @param string $attribute the field under validation
* @param mixed $value the value to be validated
* @param Closure(string $message):PotentiallyTranslatedString $fail passed an error message on failure
* @param Closure(string $message):string|Stringable $fail passed an error message on failure
* @return void
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
Expand All @@ -55,6 +63,18 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
}
}

/**
* Laravel 9 interface method
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value): bool
{
return $this->doValidation($value);
}

/**
* Do the actual validation; used by both instance and extend methods
*
Expand Down

0 comments on commit 4ad4da1

Please sign in to comment.