Failed Conditions
Push — issue#774 ( 057f04...ee9377 )
by Guilherme
05:15
created

E164PhoneNumberValidator::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 3
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\ValidationBundle\Validator\Constraints;
12
13
use Symfony\Component\Validator\Constraint;
14
use Symfony\Component\Validator\ConstraintValidator;
15
16
/**
17
 * @Annotation
18
 */
19
class E164PhoneNumberValidator extends ConstraintValidator
20
{
21
    const MAX_E164_LENGTH = 15;
22
23
    /**
24
     * Checks if the passed value is valid.
25
     *
26
     * @param mixed $value The value that should be validated
27
     * @param Constraint $constraint The constraint for the validation
28
     */
29 2
    public function validate($value, Constraint $constraint)
30
    {
31
        // Remove formatting
32 2
        $number = preg_replace('/[^0-9]/', '', $value);
33
34
        // Check length
35 2
        if (strlen($number) > self::MAX_E164_LENGTH
36 2
            && $constraint instanceof E164PhoneNumber
37
        ) {
38 1
            $this->context->addViolation($constraint->maxMessage);
39
        }
40 2
    }
41
}
42