Skip to content

Commit

Permalink
fix(spa): Support Illuminate\Validation\InvokableValidationRule in …
Browse files Browse the repository at this point in the history
…`WithValueProvider`.
  • Loading branch information
LastDragon-ru committed Mar 26, 2024
1 parent 4979a4e commit 594e043
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
16 changes: 12 additions & 4 deletions packages/spa/src/Http/WithValueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
21 changes: 8 additions & 13 deletions packages/spa/src/Http/WithValueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -85,7 +80,7 @@ public function message(): string {
* @param array<string, mixed> $data
*/
public function __construct(
private readonly Rule $rule,
private readonly ValidationRule $rule,
private readonly ResolverRule $resolverRuleA,
private readonly ResolverRule $resolverRuleB,
private readonly array $data,
Expand Down

0 comments on commit 594e043

Please sign in to comment.