Skip to content

Commit

Permalink
Fix backwards compatibility with Laravel 9
Browse files Browse the repository at this point in the history
  • Loading branch information
remipelhate committed Dec 27, 2024
1 parent f0ddbeb commit 2cf8ced
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/Bind/Validation/BindingExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

namespace Nuwave\Lighthouse\Bind\Validation;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\InvokableRule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Validation\Validator;
use Nuwave\Lighthouse\Bind\BindDirective;

use function is_array;

class BindingExists implements ValidationRule, ValidatorAwareRule
class BindingExists implements InvokableRule, ValidatorAwareRule
{
private ?Validator $validator = null;

public function __construct(
private BindDirective $directive,
) {}

public function setValidator(Validator $validator): self
{
$this->validator = $validator;

return $this;
}

/**
* Because of backwards compatibility with Laravel 9, typehints for this method
* must be set through PHPDoc as the interface did not include typehints.
* @link https://laravel.com/docs/9.x/validation#using-rule-objects
*
* @param string $attribute
* @parent mixed $value
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
public function __invoke(mixed $attribute, mixed $value, mixed $fail): void
{
$binding = $this->directive->transform($value);

Expand All @@ -50,4 +48,18 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
$this->validator?->addFailure("$attribute.$key", 'exists');
}
}

/**
* Because of backwards compatibility with Laravel 9, typehints for this method
* must be set through PHPDoc as the interface did not include typehints.
* @link https://laravel.com/docs/9.x/validation#custom-validation-rules
*
* @param \Illuminate\Validation\Validator $validator
*/
public function setValidator(mixed $validator): self
{
$this->validator = $validator;

return $this;
}
}
1 change: 1 addition & 0 deletions tests/Integration/Bind/BindDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,7 @@ public function testMultipleBindingsInSameRequest(): void

private function assertThrowsMultipleRecordsFoundException(Closure $makeRequest, int $count): void
{
// @phpstan-ignore-next-line assertThrows does accept a Closure as second parameter
$this->assertThrows($makeRequest, function (Error $exception) use ($count): bool {
$expected = new MultipleRecordsFoundException($count);

Expand Down

0 comments on commit 2cf8ced

Please sign in to comment.