Completed
Push — bridge-refactor ( 54fa11 )
by Sullivan
05:53
created

ZipCodeValidator::validate()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 23
rs 6.7272
cc 7
eloc 14
nc 9
nop 2
1
<?php
2
3
namespace SLLH\IsoCodesValidator\Constraints;
4
5
use IsoCodes;
6
use SLLH\IsoCodesValidator\AbstractIsoCodesConstraintValidator;
7
use Symfony\Component\Validator\Constraint;
8
9
/**
10
 * @author Sullivan Senechal <[email protected]>
11
 */
12
final class ZipCodeValidator extends AbstractIsoCodesConstraintValidator
13
{
14
    /**
15
     * @param mixed              $value
16
     * @param ZipCode|Constraint $constraint
17
     */
18
    public function validate($value, Constraint $constraint)
19
    {
20
        parent::validate($value, $constraint);
21
22
        if (!$value) {
23
            return;
24
        }
25
26
        if ($constraint->country == ZipCode::ALL) {
27
            $validated = false;
28
            foreach (IsoCodes\ZipCode::getAvailableCountries() as $country) {
29
                if (IsoCodes\ZipCode::validate($value, $country)) {
30
                    $validated = true;
31
                    break;
32
                }
33
            }
34
            if ($validated === false) {
35
                $this->createViolation($constraint->message);
36
            }
37
        } elseif (!IsoCodes\ZipCode::validate($value, $constraint->country)) {
38
            $this->createViolation($constraint->message);
39
        }
40
    }
41
}
42