You are here

function postal_code_validation_validate in Postal Code Validation 7

Check if a postal code is valid for a given country.

Parameters

string $postal_code: The postal code to look up.

string $country: The country that the postal code is from. If FALSE, try to determine the country.

Return value

array Array with elements: country: Country code of the postal code. country_supported: boolean, whether the country is supported. province: The province or province code if it can be determined. postal_code: The formatted postal code. postal_code_changed: boolean, whether postal_code is different from the value sent in for validation. error: An error message if the code is invalid. Each is set to FALSE if a valid response is not possible.

File

./postal_code_validation.module, line 32
Module file.

Code

function postal_code_validation_validate($postal_code, $country = FALSE) {

  // Ensures variables are of the correct type.
  $postal_code = (string) $postal_code;
  if ($country !== FALSE) {
    $country = (string) $country;
  }
  if ($country) {

    // Return empty string as postal code for countries that don't use them.
    if (in_array($country, postal_code_validation_get_countries_without_codes())) {
      $return = array(
        'country' => $country,
        'postal_code' => '',
        'country_supported' => TRUE,
      );
      if ($postal_code) {
        include_once DRUPAL_ROOT . '/includes/locale.inc';
        $countries = country_get_list();
        $return['error'] = t('%country does not have a postal code system. Leave it blank.', array(
          '%country' => $countries[$country],
        ));
      }
    }
    else {

      // Check if the country is dependent on the postal code system of
      // another country. If so, use the host country's code.
      $dependencies = postal_code_validation_get_dependencies();
      if (isset($dependencies[$country])) {
        $country = $dependencies[$country];
      }
      if (in_array($country, postal_code_validation_get_supported_countries(), TRUE)) {
        require_once 'countries/' . drupal_strtolower($country) . '.inc';
        $validate_function = '_postal_code_validation_validate_' . $country;
        $return = $validate_function(drupal_strtoupper(trim(str_replace(' ', '', $postal_code))));
        $return['country_supported'] = TRUE;
      }
      else {
        $return = _postal_code_validation_validate_other($postal_code, $country);
      }
    }
  }
  else {
    $return = FALSE;
    foreach (postal_code_validation_get_supported_countries() as $country) {
      require_once 'countries/' . drupal_strtolower($country) . '.inc';
      $validate_function = '_postal_code_validation_validate_' . $country;
      $validate = $validate_function(drupal_strtoupper(trim(str_replace(' ', '', $postal_code))));
      if (empty($validate['error'])) {
        $return = $validate;
        break;
      }
    }
    if (!$return) {
      $return = _postal_code_validation_validate_other($postal_code, FALSE);
    }
  }
  $return += array(
    'country' => FALSE,
    'country_supported' => FALSE,
    'province' => FALSE,
    'postal_code' => FALSE,
    'postal_code_changed' => FALSE,
    'error' => FALSE,
  );
  if ($return['postal_code'] !== $postal_code) {
    $return['postal_code_changed'] = TRUE;
  }
  return $return;
}