View source
<?php
namespace Drupal\address;
use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressFormat\AdministrativeAreaType;
use CommerceGuys\Addressing\AddressFormat\DependentLocalityType;
use CommerceGuys\Addressing\AddressFormat\LocalityType;
use CommerceGuys\Addressing\AddressFormat\PostalCodeType;
use CommerceGuys\Addressing\AddressFormat\AddressFormat;
class LabelHelper {
public static function getGenericFieldLabels() {
return [
AddressField::GIVEN_NAME => t('First name', [], [
'context' => 'Address label',
]),
AddressField::ADDITIONAL_NAME => t('Middle name', [], [
'context' => 'Address label',
]),
AddressField::FAMILY_NAME => t('Last name', [], [
'context' => 'Address label',
]),
AddressField::ORGANIZATION => t('Company', [], [
'context' => 'Address label',
]),
AddressField::ADDRESS_LINE1 => t('Address line 1', [], [
'context' => 'Address label',
]),
AddressField::ADDRESS_LINE2 => t('Address line 2', [], [
'context' => 'Address label',
]),
AddressField::POSTAL_CODE => t('Postal code', [], [
'context' => 'Address label',
]),
AddressField::SORTING_CODE => t('Sorting code', [], [
'context' => 'Address label',
]),
AddressField::DEPENDENT_LOCALITY => t('Dependent locality (e.g. Neighbourhood)', [], [
'context' => 'Address label',
]),
AddressField::LOCALITY => t('Locality (e.g. City)', [], [
'context' => 'Address label',
]),
AddressField::ADMINISTRATIVE_AREA => t('Administrative area (e.g. State or Province)', [], [
'context' => 'Address label',
]),
];
}
public static function getFieldLabels(AddressFormat $address_format) {
$administrative_area_type = $address_format
->getAdministrativeAreaType();
$locality_type = $address_format
->getLocalityType();
$dependent_locality_type = $address_format
->getDependentLocalityType();
$postal_code_type = $address_format
->getPostalCodeType();
return [
AddressField::GIVEN_NAME => t('First name', [], [
'context' => 'Address label',
]),
AddressField::ADDITIONAL_NAME => t('Middle name', [], [
'context' => 'Address label',
]),
AddressField::FAMILY_NAME => t('Last name', [], [
'context' => 'Address label',
]),
AddressField::ORGANIZATION => t('Company', [], [
'context' => 'Address label',
]),
AddressField::ADDRESS_LINE1 => t('Street address', [], [
'context' => 'Address label',
]),
AddressField::ADDRESS_LINE2 => t('Street address line 2', [], [
'context' => 'Address label',
]),
AddressField::POSTAL_CODE => self::getPostalCodeLabel($postal_code_type),
AddressField::SORTING_CODE => t('Cedex', [], [
'context' => 'Address label',
]),
AddressField::DEPENDENT_LOCALITY => self::getDependentLocalityLabel($dependent_locality_type),
AddressField::LOCALITY => self::getLocalityLabel($locality_type),
AddressField::ADMINISTRATIVE_AREA => self::getAdministrativeAreaLabel($administrative_area_type),
];
}
public static function getAdministrativeAreaLabel($administrative_area_type) {
if (!$administrative_area_type) {
return NULL;
}
AdministrativeAreaType::assertExists($administrative_area_type);
$labels = self::getAdministrativeAreaLabels();
return $labels[$administrative_area_type];
}
public static function getAdministrativeAreaLabels() {
return [
AdministrativeAreaType::AREA => t('Area', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::CANTON => t('Canton', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::COUNTY => t('County', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::DEPARTMENT => t('Department', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::DISTRICT => t('District', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::DO_SI => t('Do si', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::EMIRATE => t('Emirate', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::ISLAND => t('Island', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::OBLAST => t('Oblast', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::PARISH => t('Parish', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::PREFECTURE => t('Prefecture', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::PROVINCE => t('Province', [], [
'context' => 'Address label',
]),
AdministrativeAreaType::STATE => t('State', [], [
'context' => 'Address label',
]),
];
}
public static function getLocalityLabel($locality_type) {
if (!$locality_type) {
return NULL;
}
LocalityType::assertExists($locality_type);
$labels = self::getLocalityLabels();
return $labels[$locality_type];
}
public static function getLocalityLabels() {
return [
LocalityType::CITY => t('City', [], [
'context' => 'Address label',
]),
LocalityType::DISTRICT => t('District', [], [
'context' => 'Address label',
]),
LocalityType::POST_TOWN => t('Post town', [], [
'context' => 'Address label',
]),
LocalityType::SUBURB => t('Suburb', [], [
'context' => 'Address label',
]),
];
}
public static function getDependentLocalityLabel($dependent_locality_type) {
if (!$dependent_locality_type) {
return NULL;
}
DependentLocalityType::assertExists($dependent_locality_type);
$labels = self::getDependentLocalityLabels();
return $labels[$dependent_locality_type];
}
public static function getDependentLocalityLabels() {
return [
DependentLocalityType::DISTRICT => t('District', [], [
'context' => 'Address label',
]),
DependentLocalityType::NEIGHBORHOOD => t('Neighborhood', [], [
'context' => 'Address label',
]),
DependentLocalityType::VILLAGE_TOWNSHIP => t('Village township', [], [
'context' => 'Address label',
]),
DependentLocalityType::SUBURB => t('Suburb', [], [
'context' => 'Address label',
]),
DependentLocalityType::TOWNLAND => t('Townland', [], [
'context' => 'Address label',
]),
];
}
public static function getPostalCodeLabel($postal_code_type) {
if (!$postal_code_type) {
return NULL;
}
PostalCodeType::assertExists($postal_code_type);
$labels = self::getPostalCodeLabels();
return $labels[$postal_code_type];
}
public static function getPostalCodeLabels() {
return [
PostalCodeType::EIR => t('Eircode', [], [
'context' => 'Address label',
]),
PostalCodeType::PIN => t('Pin code', [], [
'context' => 'Address label',
]),
PostalCodeType::POSTAL => t('Postal code', [], [
'context' => 'Address label',
]),
PostalCodeType::ZIP => t('Zip code', [], [
'context' => 'Address label',
]),
];
}
}