ResponseProvidingRequest::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Fesor\RequestObject\Examples\Request;
4
5
use Fesor\RequestObject\ErrorResponseProvider;
6
use Fesor\RequestObject\RequestObject;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\Validator\Constraints as Assert;
9
use Symfony\Component\Validator\ConstraintViolation;
10
use Symfony\Component\Validator\ConstraintViolationListInterface;
11
12
class ResponseProvidingRequest extends RequestObject implements ErrorResponseProvider
13
{
14
    public function rules()
15
    {
16
        return new Assert\Collection([
17
            'test' => new Assert\NotBlank(),
18
        ]);
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getErrorResponse(ConstraintViolationListInterface $errors)
25
    {
26
        return new JsonResponse([
27
            'message' => 'Please check your data',
28
            'errors' => array_map(function (ConstraintViolation $violation) {
29
                return [
30
                    'path' => $violation->getPropertyPath(),
31
                    'message' => $violation->getMessage(),
32
                ];
33
            }, iterator_to_array($errors)),
34
        ], 400);
35
    }
36
}
37