Completed
Push — develop ( 0660be...71868c )
by Freddie
04:14
created

PropertyNameValidator   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
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 9
cts 9
cp 1
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 1
1
<?php
2
3
namespace FlexPHP\Schema\Validators;
4
5
use Symfony\Component\Validator\Constraints\Length;
6
use Symfony\Component\Validator\Constraints\NotBlank;
7
use Symfony\Component\Validator\Constraints\Regex;
8
use Symfony\Component\Validator\ConstraintViolationListInterface;
9
use Symfony\Component\Validator\Validation;
10
11
/**
12
 * @Annotation
13
 */
14
class PropertyNameValidator
15
{
16
    /**
17
     * @var integer
18
     */
19
    private $minLength = 1;
20
21
    /**
22
     * @var integer
23
     */
24
    private $maxLength = 64;
25
26 102
    public function validate(string $name): ConstraintViolationListInterface
27
    {
28 102
        $validator = Validation::createValidator();
29
30 102
        return $validator->validate($name, [
31 102
            new NotBlank(),
32 102
            new Length([
33 102
                'min' => $this->minLength,
34 102
                'max' => $this->maxLength,
35
            ]),
36 102
            new Regex([
37 102
                'pattern' => '/^[a-zA-Z_][a-zA-Z0-9_]*$/',
38
            ]),
39
        ]);
40
    }
41
}
42