IsoCodes\ZipCode::validate PHP Method

validate() public static method

public static validate ( string $zipcode, string $country = null ) : boolean
$zipcode string
$country string
return boolean
    public static function validate($zipcode, $country = null)
    {
        $zipcode = trim($zipcode);
        $country = trim($country);
        if (empty($zipcode) || empty($country)) {
            return false;
        }
        $country = strtoupper($country);
        if (!isset(self::$patterns[$country])) {
            throw new \InvalidArgumentException("ERROR: The zipcode validator for {$country} does not exists yet: feel free to add it.");
        }
        return (bool) preg_match('/^(' . self::$patterns[$country] . ')$/', $zipcode);
    }

Usage Example

Beispiel #1
0
 public function testEmptyValuesByCountry()
 {
     foreach (ZipCode::getAvailableCountries() as $country) {
         foreach ($this->getEmptyValues() as $value) {
             $this->assertFalse(ZipCode::validate($value, $country), 'Empty value should be invalid.');
         }
     }
 }
All Usage Examples Of IsoCodes\ZipCode::validate