CreatePasteFormValidator::create()   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 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nastoletni\Code\Application\Form;
6
7
use Symfony\Component\Validator\Constraints as Assert;
8
use Symfony\Component\Validator\ConstraintViolationListInterface;
9
use Symfony\Component\Validator\Validation;
10
11
class CreatePasteFormValidator
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
12
{
13
    /**
14
     * Self fabric method just for convenience.
15
     *
16
     * @return CreatePasteFormValidator
17
     */
18 8
    public static function create(): CreatePasteFormValidator
19
    {
20 8
        return new static();
21
    }
22
23
    /**
24
     * CreatePasteValidator constructor.
25
     */
26 8
    private function __construct()
27
    {
28 8
    }
29
30
    /**
31
     * Validates given data to comply with entity requirements.
32
     *
33
     * @param array $data
34
     *
35
     * @return ConstraintViolationListInterface
36
     */
37 7
    public function validate(array $data): ConstraintViolationListInterface
38
    {
39 7
        $validator = Validation::createValidator();
40
41 7
        $constraint = new Assert\Collection([
42 7
            'title' => new Assert\NotNull(['message' => 'validator.title.not_null']),
43 7
            'name'  => new Assert\All([
44 7
                new Assert\NotNull(['message' => 'validator.name.not_null']),
45
            ]),
46 7
            'content' => new Assert\All([
47 7
                new Assert\NotBlank(['message' => 'validator.content.not_blank']),
48
            ]),
49
        ]);
50
51 7
        return $validator->validate($data, $constraint);
52
    }
53
}
54