<?php

namespace SLLH\IsoCodesValidator\Constraints;

use IsoCodes;
use SLLH\IsoCodesValidator\IsoCodesConstraintValidator;
use Symfony\Component\Validator\Constraint;

/**
 * @author Sullivan Senechal <soullivaneuh@gmail.com>
 */
class ZipCodeValidator extends IsoCodesConstraintValidator
{
    /**
     * @param mixed              $value
     * @param ZipCode|Constraint $constraint
     */
    public function validate($value, Constraint $constraint)
    {
        parent::validate($value, $constraint);

        if (!$value) {
            return;
        }

        if ($constraint->country == ZipCode::ALL) {
            $validated = false;
            foreach (ZipCode::$countries as $country) {
                if (IsoCodes\ZipCode::validate($value, $country)) {
                    $validated = true;
                    break;
                }
            }
            if ($validated === false) {
                $this->createViolation($constraint->message);
            }
        } elseif (!IsoCodes\ZipCode::validate($value, $constraint->country)) {
            $this->createViolation($constraint->message);
        }
    }
}

<?php

namespace SLLH\IsoCodesValidator\Constraints;

use IsoCodes;
use SLLH\IsoCodesValidator\IsoCodesConstraintValidator;
use Symfony\Component\Validator\Constraint;

/**
 * @author Sullivan Senechal <soullivaneuh@gmail.com>
 */
class ZipCodeValidator extends IsoCodesConstraintValidator
{
    /**
     * @param mixed              $value
     * @param ZipCode|Constraint $constraint
     */
    public function validate($value, Constraint $constraint)
    {
        parent::validate($value, $constraint);

        if (!$value) {
            return;
        }

        if ($constraint->country == ZipCode::ALL) {
            $validated = false;
            foreach (ZipCode::$countries as $country) {
                if ($this->validateCountry($value, $country)) {
                    $validated = true;
                    break;
                }
            }
            if ($validated === false) {
                $this->createViolation($constraint->message);
            }
        } elseif (!$this->validateCountry($value, $constraint->country)) {
            $this->createViolation($constraint->message);
        }
    }

    /**
     * @deprecated To be removed when bumping requirements to ronanguilloux/isocodes ~1.2
     *
     * @param mixed  $value
     * @param string $country
     *
     * @return bool
     */
    private function validateCountry($value, $country)
    {
        $deprecatedOptionsBridge = [
            'US'            => 'US',
            'Canada'        => 'CA',
            'France'        => 'FR',
            'Netherlands'   => 'NL',
        ];

        return IsoCodes\ZipCode::validate($value, method_exists('Isocodes\ZipCode', 'getAvailableCountries') ? $deprecatedOptionsBridge[$country] : $country);
    }
}

Fix deprecated ZipCode validator methods calls
Fix deprecated ZipCode validator methods calls
