NameUserValidator::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 1
b 0
f 0
ccs 8
cts 8
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Database\Validators;
11
12
use Symfony\Component\Validator\Constraints\Length;
13
use Symfony\Component\Validator\Constraints\Regex;
14
use Symfony\Component\Validator\ConstraintViolationListInterface;
15
use Symfony\Component\Validator\Validation;
16
17
/**
18
 * @Annotation
19
 */
20
class NameUserValidator
21
{
22
    private int $minLength = 1;
23
24
    private int $maxLength = 32;
25
26 119
    public function validate(string $name): ConstraintViolationListInterface
27
    {
28 119
        $validator = Validation::createValidator();
29
30 119
        return $validator->validate($name, [
31 119
            new Length([
32 119
                'min' => $this->minLength,
33 119
                'max' => $this->maxLength,
34
                'allowEmptyString' => false,
35
            ]),
36 119
            new Regex([
37 119
                'pattern' => '/^[a-zA-Z][a-zA-Z0-9_\-\.]*$/',
38
            ]),
39
        ]);
40
    }
41
}
42