-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Fesor\RequestObject\Examples\Request; | ||
|
||
use Fesor\RequestObject\ErrorResponseProvider; | ||
use Fesor\RequestObject\RequestObject; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use \Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\ConstraintViolation; | ||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
|
||
class ResponseProvidingRequest extends RequestObject implements ErrorResponseProvider | ||
{ | ||
public function rules() | ||
{ | ||
return new Assert\Collection([ | ||
'test' => new Assert\NotBlank() | ||
]); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getErrorResponse(ConstraintViolationListInterface $errors) | ||
{ | ||
return new JsonResponse([ | ||
'message' => 'Please check your data', | ||
'errors' => array_map(function (ConstraintViolation $violation) { | ||
return [ | ||
'path' => $violation->getPropertyPath(), | ||
'message' => $violation->getMessage() | ||
]; | ||
}, iterator_to_array($errors)) | ||
], 400); | ||
} | ||
} |