From 594e0439d7ac7fa6cf4965f07ddbf80c6f145b93 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Tue, 26 Mar 2024 09:49:57 +0400 Subject: [PATCH] fix(spa): Support `Illuminate\Validation\InvokableValidationRule` in `WithValueProvider`. --- packages/spa/src/Http/WithValueProvider.php | 16 ++++++++++---- .../spa/src/Http/WithValueProviderTest.php | 21 +++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/packages/spa/src/Http/WithValueProvider.php b/packages/spa/src/Http/WithValueProvider.php index 8008cd79a..4085da24d 100644 --- a/packages/spa/src/Http/WithValueProvider.php +++ b/packages/spa/src/Http/WithValueProvider.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Validation\Validator as ValidatorContract; use Illuminate\Support\Arr; +use Illuminate\Validation\InvokableValidationRule; use Illuminate\Validation\Validator; use function data_get; @@ -25,11 +26,18 @@ public function validated(mixed $key = null, mixed $default = null): mixed { // Replace values foreach ($validator->getRules() as $attribute => $rules) { - /** @var ValueProvider|null $provider */ - $provider = Arr::last($rules, static function ($rule): bool { - return $rule instanceof ValueProvider; - }); $attribute = (string) $attribute; + $provider = null; + + foreach ($rules as $rule) { + if ($rule instanceof InvokableValidationRule) { + $rule = $rule->invokable(); + } + + if ($rule instanceof ValueProvider) { + $provider = $rule; + } + } if ($provider && Arr::has($validated, $attribute)) { $value = Arr::get($validated, $attribute); diff --git a/packages/spa/src/Http/WithValueProviderTest.php b/packages/spa/src/Http/WithValueProviderTest.php index 549d98c6b..904710837 100644 --- a/packages/spa/src/Http/WithValueProviderTest.php +++ b/packages/spa/src/Http/WithValueProviderTest.php @@ -2,10 +2,11 @@ namespace LastDragon_ru\LaraASP\Spa\Http; +use Closure; use Illuminate\Container\Container; use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Validation\Factory; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Contracts\Validation\Validator; use Illuminate\Routing\Router; use LastDragon_ru\LaraASP\Spa\Routing\Resolver; @@ -46,18 +47,12 @@ protected function resolve(mixed $value, array $parameters): mixed { } }, ); - $rule = new class() implements Rule { - /** - * @inheritDoc - */ - #[Override] - public function passes($attribute, $value): bool { - return (bool) $value; - } - + $rule = new class() implements ValidationRule { #[Override] - public function message(): string { - return static::class; + public function validate(string $attribute, mixed $value, Closure $fail): void { + if (!$value) { + $fail(static::class); + } } }; $data = [ @@ -85,7 +80,7 @@ public function message(): string { * @param array $data */ public function __construct( - private readonly Rule $rule, + private readonly ValidationRule $rule, private readonly ResolverRule $resolverRuleA, private readonly ResolverRule $resolverRuleB, private readonly array $data,