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

CEPValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
ccs 12
cts 13
cp 0.9231
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isCEPValid() 0 5 1
A checkLength() 0 5 1
B validate() 0 10 6
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