Skip to content

Commit ffd1849

Browse files
authored
Implement ErrorResponseProvider interface and add the error response
1 parent e9973ae commit ffd1849

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/RequestObject.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Fesor\RequestObject;
44

5+
use Symfony\Component\HttpFoundation\JsonResponse;
56
use Symfony\Component\Validator\Constraint;
7+
use Symfony\Component\Validator\ConstraintViolation;
8+
use Symfony\Component\Validator\ConstraintViolationListInterface;
69

7-
class RequestObject
10+
class RequestObject implements ErrorResponseProvider
811
{
912
private $payload;
1013

@@ -56,4 +59,20 @@ public function all()
5659
{
5760
return $this->payload;
5861
}
62+
63+
/**
64+
* @param ConstraintViolationListInterface $errors
65+
*
66+
* @return JsonResponse|\Symfony\Component\HttpFoundation\Response
67+
*/
68+
public function getErrorResponse(ConstraintViolationListInterface $errors)
69+
{
70+
return new JsonResponse([
71+
'errors' => array_map(function (ConstraintViolation $violation) {
72+
return [
73+
$violation->getPropertyPath() => $violation->getMessage(),
74+
];
75+
}, iterator_to_array($errors))
76+
], 422);
77+
}
5978
}

0 commit comments

Comments
 (0)