Passed
Push — master ( 7438ce...69adc8 )
by Guilherme
01:29 queued 11s
created

CEPValidator::validateMask()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 2
dl 0
loc 17
ccs 0
cts 17
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace LoginCidadao\ValidationBundle\Validator\Constraints;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
8
/**
9
 * @Annotation
10
 */
11
class CEPValidator extends ConstraintValidator
12
{
13
    /**
14
     * Currently only checks if the value is numeric.
15
     * @param string $cep
16
     * @return boolean
17
     */
18 2
    public static function isCEPValid($cep)
19
    {
20 2
        $cep = preg_replace('/[^0-9]/', '', $cep);
21
22 2
        return is_numeric($cep);
23
    }
24
25 2
    public static function checkLength($cep)
26
    {
27 2
        $cep = preg_replace('/[^0-9]/', '', $cep);
28
29 2
        return strlen($cep) == 8;
30
    }
31
32 2
    public function validate($value, Constraint $constraint)
33
    {
34 2
        if (!isset($value) || $value === null || !strlen(trim($value))) {
35 1
            return;
36
        }
37 2
        if (!self::checkLength($value)) {
38 1
            $this->context->addViolation($constraint->lengthMessage);
39
        }
40 2
        if (!self::isCEPValid($value)) {
41
            $this->context->addViolation($constraint->message);
42
        }
43 2
    }
44
}
45