Completed
Push — develop ( 8bd504...58ca2f )
by Freddie
09:29
created

NameDatabaseValidator   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 8
cts 8
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 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 NameDatabaseValidator
21
{
22
    /**
23
     * @var int
24
     */
25
    private $minLength = 1;
26
27
    /**
28
     * @var int
29
     */
30
    private $maxLength = 63;
31
32 23
    public function validate(string $name): ConstraintViolationListInterface
33
    {
34 23
        $validator = Validation::createValidator();
35
36 23
        return $validator->validate($name, [
37 23
            new Length([
38 23
                'min' => $this->minLength,
39 23
                'max' => $this->maxLength,
40
                'allowEmptyString' => false,
41
            ]),
42 23
            new Regex([
43 23
                'pattern' => '/^[a-zA-Z][a-zA-Z0-9_]*$/',
44
            ]),
45
        ]);
46
    }
47
}
48