/**
* Creates an address format definition from Google's raw definition.
*/
function create_address_format_definition($countryCode, $rawDefinition)
{
// Avoid notices.
$rawDefinition += ['lang' => null, 'fmt' => null, 'require' => null, 'upper' => null, 'state_name_type' => null, 'locality_name_type' => null, 'sublocality_name_type' => null, 'zip_name_type' => null];
// ZZ holds the defaults for all address formats, and these are missing.
if ($countryCode == 'ZZ') {
$rawDefinition['state_name_type'] = AdministrativeAreaType::getDefault();
$rawDefinition['sublocality_name_type'] = DependentLocalityType::getDefault();
$rawDefinition['zip_name_type'] = PostalCodeType::getDefault();
}
$addressFormat = ['locale' => process_locale($rawDefinition['lang']), 'format' => null, 'local_format' => null, 'required_fields' => convert_fields($rawDefinition['require'], 'required'), 'uppercase_fields' => convert_fields($rawDefinition['upper'], 'uppercase')];
if (isset($rawDefinition['lfmt']) && $rawDefinition['lfmt'] != $rawDefinition['fmt']) {
$addressFormat['format'] = convert_format($countryCode, $rawDefinition['lfmt']);
$addressFormat['local_format'] = convert_format($countryCode, $rawDefinition['fmt']);
} else {
$addressFormat['format'] = convert_format($countryCode, $rawDefinition['fmt']);
// We don't need the locale if there's no local format.
unset($addressFormat['locale']);
}
$addressFormat['administrative_area_type'] = $rawDefinition['state_name_type'];
$addressFormat['locality_type'] = $rawDefinition['locality_name_type'];
$addressFormat['dependent_locality_type'] = $rawDefinition['sublocality_name_type'];
$addressFormat['postal_code_type'] = $rawDefinition['zip_name_type'];
if (isset($rawDefinition['zip'])) {
$addressFormat['postal_code_pattern'] = $rawDefinition['zip'];
}
if (isset($rawDefinition['postprefix'])) {
// Workaround for https://github.com/googlei18n/libaddressinput/issues/72.
if ($rawDefinition['postprefix'] == 'PR') {
$rawDefinition['postprefix'] = 'PR ';
} elseif ($rawDefinition['postprefix'] == 'SI-') {
$rawDefinition['postprefix'] = 'SI- ';
}
$addressFormat['postal_code_prefix'] = $rawDefinition['postprefix'];
// Remove the prefix from the format strings.
// Workaround for https://github.com/googlei18n/libaddressinput/issues/71.
$addressFormat['format'] = str_replace($addressFormat['postal_code_prefix'], '', $addressFormat['format']);
$addressFormat['local_format'] = str_replace($addressFormat['postal_code_prefix'], '', $addressFormat['local_format']);
}
// Add the subdivision_depth to the end of the ZZ definition.
if ($countryCode == 'ZZ') {
$addressFormat['subdivision_depth'] = 0;
}
// Apply any customizations.
$customizations = get_address_format_customizations($countryCode);
foreach ($customizations as $key => $values) {
$addressFormat[$key] = $values;
}
// Denote the end of the format string for file_put_php().
if (!empty($addressFormat['format'])) {
$addressFormat['format'] .= ';;;';
}
if (!empty($addressFormat['local_format'])) {
$addressFormat['local_format'] .= ';;;';
}
// Remove NULL keys.
$addressFormat = array_filter($addressFormat, function ($value) {
return !is_null($value);
});
// Remove empty local formats.
if (empty($addressFormat['local_format'])) {
unset($addressFormat['local_format']);
}
return $addressFormat;
}