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

PropertyNameValidator::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 1
rs 10
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