ValidationResponseFactory::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Stdlib\Validation;
5
6
use Psr\Http\Message\ResponseInterface;
7
use SlayerBirden\DataFlowServer\Notification\DangerMessage;
8
use Zend\Diactoros\Response\JsonResponse;
9
use Zend\InputFilter\InputFilterInterface;
10
11
final class ValidationResponseFactory
12
{
13
    /**
14
     * Create Validation response object
15
     *
16
     * @param string $dataObjectName
17
     * @param InputFilterInterface $inputFilter
18
     * @param mixed $value
19
     * @return ResponseInterface
20
     */
21 26
    public function __invoke(
22
        string $dataObjectName,
23
        InputFilterInterface $inputFilter,
24
        $value = null
25
    ): ResponseInterface {
26 26
        $validation = [];
27 26
        foreach ($inputFilter->getInvalidInput() as $key => $input) {
28 26
            $messages = $input->getMessages();
29 26
            $validation[] = [
30 26
                'field' => $key,
31 26
                'msg' => reset($messages)
32
            ];
33
        }
34
35 26
        return new JsonResponse([
36
            'data' => [
37 26
                $dataObjectName => $value,
38 26
                'validation' => $validation,
39
            ],
40 26
            'msg' => new DangerMessage('There were validation errors.'),
41 26
        ], 400);
42
    }
43
}
44