diff --git a/src/Validation/ValidationExceptionBuilder.php b/src/Validation/ValidationExceptionBuilder.php index 0ed53e5..58bcabc 100644 --- a/src/Validation/ValidationExceptionBuilder.php +++ b/src/Validation/ValidationExceptionBuilder.php @@ -11,16 +11,27 @@ class ValidationExceptionBuilder { protected string $message; + protected string $redirectUrl; + protected ?string $fragment = null; + protected array $queryParameters = []; + protected ?string $routeName = null; + protected array $routeParameters = []; + protected ?string $errorBag = null; + protected bool $shouldFlash = false; + protected string $flashKey = 'error'; + protected ?string $flashMessage = null; + protected string $exceptionClass = ValidationException::class; + protected mixed $exceptionCallback = null; /** @@ -55,6 +66,7 @@ public function redirectTo(string $url): self { $this->redirectUrl = $url; $this->routeName = null; // Reset route if URL is set directly + return $this; } @@ -67,6 +79,7 @@ public function redirectBack(): self { $this->redirectUrl = URL::previous(); $this->routeName = null; // Reset route if redirecting back + return $this; } @@ -81,6 +94,7 @@ public function redirectRoute(string $name, array $parameters = []): self { $this->routeName = $name; $this->routeParameters = $parameters; + return $this; } @@ -93,6 +107,7 @@ public function redirectRoute(string $name, array $parameters = []): self public function fragment(string $fragment): self { $this->fragment = $fragment; + return $this; } @@ -105,6 +120,7 @@ public function fragment(string $fragment): self public function withQueryParameters(array $params): self { $this->queryParameters = $params; + return $this; } @@ -117,6 +133,7 @@ public function withQueryParameters(array $params): self public function errorBag(string $errorBag): self { $this->errorBag = $errorBag; + return $this; } @@ -132,6 +149,7 @@ public function flash(?string $message = null, string $key = 'error'): self $this->shouldFlash = true; $this->flashMessage = $message; $this->flashKey = $key; + return $this; } @@ -140,14 +158,16 @@ public function flash(?string $message = null, string $key = 'error'): self * * @param string $exceptionClass The fully qualified class name of the exception. * @return self + * * @throws \InvalidArgumentException If the class is not a subclass of Exception. */ public function withException(string $exceptionClass): self { if (!is_subclass_of($exceptionClass, Exception::class)) { - throw new \InvalidArgumentException("The provided class must be a subclass of Exception."); + throw new \InvalidArgumentException('The provided class must be a subclass of Exception.'); } $this->exceptionClass = $exceptionClass; + return $this; } @@ -160,6 +180,7 @@ public function withException(string $exceptionClass): self public function withExceptionCallback(callable $callback): self { $this->exceptionCallback = $callback; + return $this; } @@ -189,6 +210,7 @@ public function throwException(): void * Throw the configured exception if the given condition is true. * * @param bool|callable $condition A boolean value or a callback that returns a boolean. + * * @throws Exception */ public function throwExceptionIf($condition): void @@ -204,6 +226,7 @@ public function throwExceptionIf($condition): void * Throw the configured exception unless the given condition is true. * * @param bool|callable $condition A boolean value or a callback that returns a boolean. + * * @throws Exception */ public function throwExceptionUnless($condition): void @@ -252,6 +275,7 @@ protected function createDefaultException(string $url): Exception if ($this->errorBag) { $exception->errorBag($this->errorBag); } + return $exception; } diff --git a/tests/Validation/ValidationExceptionBuilderTest.php b/tests/Validation/ValidationExceptionBuilderTest.php index 2f25a6a..6e58f76 100644 --- a/tests/Validation/ValidationExceptionBuilderTest.php +++ b/tests/Validation/ValidationExceptionBuilderTest.php @@ -138,7 +138,7 @@ $builder = ValidationExceptionBuilder::message('Test message') ->withException(\RuntimeException::class); - expect(fn() => $builder->throwException())->toThrow(\RuntimeException::class, 'Test message'); + expect(fn () => $builder->throwException())->toThrow(\RuntimeException::class, 'Test message'); }); test('it uses custom exception callback', function () { @@ -147,25 +147,25 @@ return new \RuntimeException("Custom: $message"); }); - expect(fn() => $builder->throwException())->toThrow(\RuntimeException::class, 'Custom: Test message'); + expect(fn () => $builder->throwException())->toThrow(\RuntimeException::class, 'Custom: Test message'); }); test('it throws exception conditionally with if', function () { $builder = ValidationExceptionBuilder::message('Test message'); - expect(fn() => $builder->throwExceptionIf(true))->toThrow(ValidationException::class); - expect(fn() => $builder->throwExceptionIf(false))->not->toThrow(ValidationException::class); + expect(fn () => $builder->throwExceptionIf(true))->toThrow(ValidationException::class); + expect(fn () => $builder->throwExceptionIf(false))->not->toThrow(ValidationException::class); - expect(fn() => $builder->throwExceptionIf(fn() => true))->toThrow(ValidationException::class); - expect(fn() => $builder->throwExceptionIf(fn() => false))->not->toThrow(ValidationException::class); + expect(fn () => $builder->throwExceptionIf(fn () => true))->toThrow(ValidationException::class); + expect(fn () => $builder->throwExceptionIf(fn () => false))->not->toThrow(ValidationException::class); }); test('it throws exception conditionally with unless', function () { $builder = ValidationExceptionBuilder::message('Test message'); - expect(fn() => $builder->throwExceptionUnless(false))->toThrow(ValidationException::class); - expect(fn() => $builder->throwExceptionUnless(true))->not->toThrow(ValidationException::class); + expect(fn () => $builder->throwExceptionUnless(false))->toThrow(ValidationException::class); + expect(fn () => $builder->throwExceptionUnless(true))->not->toThrow(ValidationException::class); - expect(fn() => $builder->throwExceptionUnless(fn() => false))->toThrow(ValidationException::class); - expect(fn() => $builder->throwExceptionUnless(fn() => true))->not->toThrow(ValidationException::class); + expect(fn () => $builder->throwExceptionUnless(fn () => false))->toThrow(ValidationException::class); + expect(fn () => $builder->throwExceptionUnless(fn () => true))->not->toThrow(ValidationException::class); });