Skip to content

Commit 52e637b

Browse files
committed
Change ResultTransformerInterface
- Added optional ValidationException to extract the error message and error code from the exception
1 parent b3f5231 commit 52e637b

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

src/Exception/ValidationException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Throwable;
88

99
/**
10-
* Exception.
10+
* Validation Exception.
1111
*/
1212
final class ValidationException extends DomainException
1313
{
@@ -19,14 +19,14 @@ final class ValidationException extends DomainException
1919
/**
2020
* Construct the exception.
2121
*
22-
* @param ValidationResult $validationResult The validation result object
2322
* @param string $message The Exception message to throw
23+
* @param ValidationResult $validationResult The validation result object
2424
* @param int $code The Exception code
2525
* @param Throwable|null $previous The previous throwable used for the exception chaining
2626
*/
2727
public function __construct(
28+
string $message,
2829
ValidationResult $validationResult,
29-
string $message = '',
3030
int $code = 0,
3131
Throwable $previous = null
3232
) {

src/Middleware/ValidationExceptionMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6565
->withStatus(422)
6666
->withHeader('Content-Type', 'application/json');
6767

68-
$data = $this->transformer->transform($exception->getValidationResult());
68+
$data = $this->transformer->transform($exception->getValidationResult(), $exception);
6969
$content = $this->encoder->encode($data);
7070
$response->getBody()->write($content);
7171

src/Transformer/ErrorDetailsResultTransformer.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Selective\Validation\Transformer;
44

5+
use Selective\Validation\Exception\ValidationException;
56
use Selective\Validation\ValidationError;
67
use Selective\Validation\ValidationResult;
78

@@ -29,21 +30,22 @@ public function __construct(string $detailsName = 'details')
2930
* Transform the given ValidationResult into an array.
3031
*
3132
* @param ValidationResult $validationResult The validation result
33+
* @param ValidationException|null $exception The validation exception
3234
*
3335
* @return array<mixed> The transformed result
3436
*/
35-
public function transform(ValidationResult $validationResult): array
37+
public function transform(ValidationResult $validationResult, ValidationException $exception = null): array
3638
{
3739
$error = [];
3840

39-
$code = $validationResult->getCode();
40-
if ($code !== null) {
41-
$error['code'] = $code;
42-
}
41+
if ($exception !== null) {
42+
if ($exception->getMessage()) {
43+
$error['message'] = $exception->getMessage();
44+
}
4345

44-
$message = $validationResult->getMessage();
45-
if ($message !== null) {
46-
$error['message'] = $message;
46+
if ($exception->getCode()) {
47+
$error['code'] = $exception->getCode();
48+
}
4749
}
4850

4951
$errors = $validationResult->getErrors();

src/Transformer/ResultTransformerInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Selective\Validation\Transformer;
44

5+
use Selective\Validation\Exception\ValidationException;
56
use Selective\Validation\ValidationResult;
67

78
/**
@@ -13,8 +14,9 @@ interface ResultTransformerInterface
1314
* Transform the given ValidationResult into an array.
1415
*
1516
* @param ValidationResult $validationResult The validation result
17+
* @param ValidationException|null $exception The validation exception
1618
*
1719
* @return array<mixed> The transformed result
1820
*/
19-
public function transform(ValidationResult $validationResult): array;
21+
public function transform(ValidationResult $validationResult, ValidationException $exception = null): array;
2022
}

0 commit comments

Comments
 (0)