Validates a zipcode based on country

@since 1.0.0
@param string $zipCode A string containing the zipcode
@param string $country A string containing the ISO 3166-1 alpha-2 code of the country
@example validateZipCode('1122AA', 'NL')
@return boolean
321619
public static function validateZipCode($zipCode, $country) {
        switch (strtoupper($country)) {
            case 'NL':
                if (preg_match('/^[1-9]{1}[0-9]{3}[A-Z]{2}$/', $zipCode))
                    return true;
                break;
            case 'BE':
                if (preg_match('/^[1-9]{4}$/', $zipCode))
                    return true;
                break;
            case 'DE':
                if (preg_match('/^[1-9]{5}$/', $zipCode))
                    return true;
                break;
        }

        return false;
    }
1143595
public function validateCode($code)
    {
        $coupon = $this->_controller->getRepository()->find(array('code' => $code));

        if ($coupon && !$coupon->expired()) {
            $discount = $coupon->discount * 100;
            $this->setMessage(AnTranslator::sprintf('COM-SUBSCRIPTIONS-VALID-COUPON', $discount));
            return true;
        } else {
            $this->setMessage(AnTranslator::_('COM-SUBSCRIPTIONS-INVALID-COUPON'));
            return false;
        }
    }
1140213
public static function validate($code = '', $key = -1)
    {
        if (strlen($key) < 1) {
            return false;
        }

        if (!is_numeric($key)) {
            return false;
        }

        if (!is_string($code)) {
            return false;
        }

        if (strlen($code) < 2) {
            return false;
        }

        $numerals = str_split($code);
        $rank = array_reverse(array_keys($numerals));
        $orderedNumerals = array();
        foreach ($rank as $i => $rankValue) {
            $orderedNumerals[$rankValue + 1] = $numerals[$i];
        }
        $results = array();
        foreach ($orderedNumerals as $cle => $value) {
            $results[$value] = ($cle % 2 == 0) ? ((int) $value * 1) : ((int) $value * 2);
        }
        $sum = 0;
        foreach ($results as $cle => $value) {
            $sum += array_sum(str_split($value));
        }
        $validKey = str_split($sum);
        $validKey = 10 - array_pop($validKey);

        return $key === $validKey;
    }
830341
private function validatePluginZip(string $prefix, \ZipArchive $archive): void
    {
        for ($i = 2; $i < $archive->numFiles; ++$i) {
            $stat = $archive->statIndex($i);

            $this->assertNoDirectoryTraversal($stat['name']);
            $this->assertPrefix($stat['name'], $prefix);
        }
    }
1251166
public function isValid($value = null, $context = null)
    {
        if (!is_string($value)) {
            $this->error(self::INVALID);

            return false;
        }

        $country = $this->getCountry();

        if (!array_key_exists($country, $this->iban)) {
            if (array_key_exists($country, $context)) {
                $country = $context[$country];
            } else {
                $country = substr($value, 0, 2);
            }
        }

        if (!array_key_exists($country, $this->iban)) {
            $this->error(self::UNSUPPORTED);

            return false;
        }

        if (!preg_match($this->iban[$country]['iban'], $value)) {
            if ($this->allowBban && $this->iban[$country]['bban']) {
                return true;
            }

            $this->error(self::NO_MATCH);

            return false;
        }

        return true;
    }
780355
public function isValid($value)
    {
        $value = strtoupper($value);
        $this->_setValue($value);

        if (empty($this->_locale)) {
            $region = substr($value, 0, 2);
        } else {
            $region = new Zend_Locale($this->_locale);
            $region = $region->getRegion();
        }

        if (!array_key_exists($region, $this->_ibanregex)) {
            $this->_setValue($region);
            $this->_error(self::NOTSUPPORTED);
            return false;
        }

        if (!preg_match($this->_ibanregex[$region], $value)) {
            $this->_error(self::FALSEFORMAT);
            return false;
        }

        $format = substr($value, 4) . substr($value, 0, 4);
        $format = str_replace(
            array('A',  'B',  'C',  'D',  'E',  'F',  'G',  'H',  'I',  'J',  'K',  'L',  'M',
                  'N',  'O',  'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',  'X',  'Y',  'Z'),
            array('10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22',
                  '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35'),
            $format);

        $temp = intval(substr($format, 0, 1));
        $len  = strlen($format);
        for ($x = 1; $x < $len; ++$x) {
            $temp *= 10;
            $temp += intval(substr($format, $x, 1));
            $temp %= 97;
        }

        if ($temp != 1) {
            $this->_error(self::CHECKFAILED);
            return false;
        }

        return true;
    }
739163
public static function validate($countries)
    {
        $valid = array();

        //If data is not passed as an array, do so.
        if (!is_array($countries)) {
            $countries = \explode(' ', $countries);
            $countries = \array_filter($countries);
        }

        //Foreach value, check if it is a valid self::$iso3166 value
        foreach ($countries as $country) {
            $country = \preg_replace('/[^a-z]/i', '', $country);
            $country = \strtoupper($country);
            if (\in_array($country, self::$iso3166, true)) {
                $valid[] = $country;
            }
        }

        $data = \implode(' ', $valid);

        return (\strlen($data) > 0) ? $data : false;
    }
766069
public static String validateCountry(String country) {
        if (country.length() > DEFAULT_COUNTRY_LEN) {
            return country.substring(0, DEFAULT_COUNTRY_LEN);
        }

        return country;
    }
1228760
public function isValid($value = null, $context = null)
    {
        if (!is_string($value)) {
            $this->error(self::INVALID);

            return false;
        }

        if (!preg_match($this->pattern, $value, $matches)) {
            $this->error(self::NO_MATCH);

            return false;
        }

        $country = $this->getCountry();

        if ($country !== null) {
            if (array_key_exists($country, $context)) {
                $country = $context[$country];
            }

            if (strcasecmp($matches[2], $country) !== 0) {
                $this->error(self::NO_MATCH);

                return false;
            }
        }

        return true;
    }
599805
@Override
  public final boolean isValid(final Object pvalue, final ConstraintValidatorContext pcontext) {
    if (pvalue == null) {
      return true;
    }
    try {
      String countryCode =
          BeanPropertyReaderUtil.getNullSaveStringProperty(pvalue, fieldCountryCode);
      final String postalCode =
          BeanPropertyReaderUtil.getNullSaveStringProperty(pvalue, fieldPostalCode);
      if (StringUtils.isEmpty(postalCode)) {
        return true;
      }

      if (allowLowerCaseCountryCode) {
        countryCode = StringUtils.upperCase(countryCode);
      }

      final String regExCheck = POSTAL_CODE_MAP.postalCodes().get(countryCode);
      if (regExCheck == null) {
        return true;
      }
      if (postalCode.matches(regExCheck)) {
        return true;
      }
      switchContext(pcontext);
      return false;
    } catch (final Exception ignore) {
      switchContext(pcontext);
      return false;
    }
  }
733133
public function validate($code): bool
    {
        $codeLength = strlen($code);

        return ctype_digit($code) && $codeLength == $this->config['total_digits'] && strlen($code) <= 6;
    }
177630
public static function countryAlpha2(string $country) : string
    {
        $alpha2 = static::simpleCountries()->filter(function ($c) use ($country) {
            return $c === $country;
        })->keys()->first();

        return $alpha2 ? $alpha2 : 'Unknown';
    }
276601
public function isCodeValid($code)
    {
        $codeFromConfig = $this->getWebsiteConfig(
            \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_PASSCODE,
            $this->getWebsite()
        );

        return $codeFromConfig === $code;
    }
1143754
public static function validate($phoneNumber, $country = null)
    {
        $phoneNumber = trim($phoneNumber);
        if (empty($phoneNumber)) {
            return false;
        }
        $country = strtoupper($country);
        $phoneUtil = PhoneNumberUtil::getInstance();
        try {
            $numberProto = $phoneUtil->parse($phoneNumber, $country);
        } catch (NumberParseException $e) {
            return false;
        }

        return $phoneUtil->isValidNumber($numberProto);
    }
1022919
public function formatForMobileDialingInCountry($country, $removeFormatting = false)
    {
        if (! static::isValidCountryCode($country)) {
            throw CountryCodeException::invalid($country);
        }

        return $this->lib->formatNumberForMobileDialing(
            $this->getPhoneNumberInstance(),
            $country,
            $removeFormatting
        );
    }
194252
protected function validateFormatCountry()
    {
        $field = 'format';
        $format = $this->getSubmitted($field);

        if (!isset($format)) {
            $this->unsetSubmitted($field);
            return null;
        }

        $label = $this->translation->text('Format');

        if (!is_array($format)) {
            $this->setErrorInvalid($field, $label);
            return false;
        }

        $default = $this->country->getDefaultFormat();

        if (!array_intersect_key($format, $default)) {
            $this->setErrorInvalid($field, $label);
            return false;
        }

        foreach ($format as $key => $value) {

            if (!is_array($value) || !array_intersect_key($value, $default[$key])) {
                $this->setErrorInvalid($field, $label);
                return false;
            }

            foreach ($value as $v) {
                if (!in_array(gettype($v), array('string', 'integer', 'boolean'))) {
                    $this->setErrorInvalid($field, $label);
                    return false;
                }
            }
        }

        return true;
    }
418197
protected function validate_language_pack() {

        if (!isset($this->assertions['plugintype'])) {
            throw new coding_exception('Required plugin type must be set before calling this');
        }

        if (!isset($this->extractfiles[$this->rootdir.'/lang/en/'])
                or $this->extractfiles[$this->rootdir.'/lang/en/'] !== true
                or !is_dir($this->extractdir.'/'.$this->rootdir.'/lang/en')) {
            $this->add_message(self::ERROR, 'missinglangenfolder');
            return false;
        }

        $langfiles = array();
        foreach (array_keys($this->extractfiles) as $extractfile) {
            $matches = array();
            if (preg_match('#^'.preg_quote($this->rootdir).'/lang/en/([^/]+).php?$#i', $extractfile, $matches)) {
                $langfiles[] = $matches[1];
            }
        }

        if (empty($langfiles)) {
            $this->add_message(self::ERROR, 'missinglangenfile');
            return false;
        } else if (count($langfiles) > 1) {
            $this->add_message(self::WARNING, 'multiplelangenfiles');
        } else {
            $this->langfilename = $langfiles[0];
            $this->add_message(self::DEBUG, 'foundlangfile', $this->langfilename);
        }

        if ($this->assertions['plugintype'] === 'mod') {
            $expected = $this->rootdir.'.php';
        } else {
            $expected = $this->assertions['plugintype'].'_'.$this->rootdir.'.php';
        }

        if (!isset($this->extractfiles[$this->rootdir.'/lang/en/'.$expected])
                or $this->extractfiles[$this->rootdir.'/lang/en/'.$expected] !== true
                or !is_file($this->extractdir.'/'.$this->rootdir.'/lang/en/'.$expected)) {
            $this->add_message(self::ERROR, 'missingexpectedlangenfile', $expected);
            return false;
        }

        return true;
    }