BaseRequest::eachItemValidation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\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
//todo How to inject TranslatorInterface ?
13
class BaseRequest extends RequestObject implements ErrorResponseProvider
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 7
    public function getErrorResponse(ConstraintViolationListInterface $errors)
19
    {
20 7
        return new JsonResponse([
21 7
            'message' => 'Validation error. Please check your data.', //todo translate this message
22
            'errors' => array_map(function (ConstraintViolation $violation) {
23
                // todo find the way to show correct path to property.
24
                // Assert\* will return path like "[registration][username]"
25
                // But UniqueEntity will return path like "username"
26
                return [
27 7
                    'path' => $violation->getPropertyPath(),
28 7
                    'message' => $violation->getMessage(),
29
                ];
30 7
            }, iterator_to_array($errors)),
31 7
        ], 400);
32
    }
33
34 11
    public function eachItemValidation(array $constraints)
35
    {
36 11
        return new Assert\All([new Assert\Collection($constraints)]);
37
    }
38
}
39