addressing ========== [](https://travis-ci.org/commerceguys/addressing) A PHP 7.3+ addressing library, powered by CLDR and Google's address data. Manipulates postal addresses, meant to identify a precise recipient location for shipping or billing purposes. Features: - Countries, with translations for over 250 locales - Address formats for over 200 countries - Subdivisions (administrative areas, localities, dependent localities) for 44 countries - Both latin and local subdivision names, when relevant (e.g: Okinawa / 沖縄県) - Formatting, in HTML or plain text. - Validation via symfony/validator - Zones The dataset is [stored locally](https://github.com/commerceguys/addressing/tree/master/resources) in JSON format. Countries are generated from [CLDR](http://cldr.unicode.org) v42. Address formats and subdivisions are generated from Google's [Address Data Service](https://chromium-i18n.appspot.com/ssl-address). Further backstory can be found in [this blog post](https://drupalcommerce.org/blog/16864/commerce-2x-stories-addressing). Also check out [commerceguys/intl](https://github.com/commerceguys/intl) for CLDR-powered languages/currencies/number formatting. # Data model The [address interface](https://github.com/commerceguys/addressing/blob/master/src/AddressInterface.php) represents a postal adddress, with getters for the following fields: - Country code - Administrative area - Locality (City) - Dependent Locality - Postal code - Sorting code - Address line 1 - Address line 2 - Organization - Given name (First name) - Additional name (Middle name / Patronymic) - Family name (Last name) Field names follow the OASIS [eXtensible Address Language (xAL)](http://www.oasis-open.org/committees/ciq/download.shtml) standard. The interface makes no assumptions about mutability. The implementing application can extend the interface to provide setters, or implement a value object that uses either [PSR-7 style with* mutators](https://github.com/commerceguys/addressing/blob/master/src/ImmutableAddressInterface) or relies on an AddressBuilder. A default [address value object](https://github.com/commerceguys/addressing/blob/master/src/Address.php) is provided that can be used as an example, or mapped by Doctrine (preferably as an embeddable). The [address format](https://github.com/commerceguys/addressing/blob/master/src/AddressFormat/AddressFormat.php) provides the following information: - Which fields are used, and in which order - Which fields are required - Which fields need to be uppercased for the actual mailing (to facilitate automated sorting of mail) - The labels for the administrative area (state, province, parish, etc.), locality (city/post town/district, etc.), dependent locality (neighborhood, suburb, district, etc) and the postal code (postal code or ZIP code) - The regular expression pattern for validating postal codes The [country](https://github.com/commerceguys/addressing/blob/master/src/Country/Country.php) provides the following information: - The country name. - The numeric and three-letter country codes. - The official currency code, when known. - The timezones which the country spans. The [subdivision](https://github.com/commerceguys/addressing/blob/master/src/Subdivision/Subdivision.php) provides the following information: - The subdivision code (used to represent the subdivison on a parcel/envelope, e.g. CA for California) - The subdivison name (shown to the user in a dropdown) - The local code and name, if the country uses a non-latin script (e.g. Cyrilic in Russia). - The postal code prefix (used to ensure that a postal code begins with the expected characters) Subdivisions are hierarchical and can have up to three levels: Administrative Area -> Locality -> Dependent Locality. ```php use CommerceGuys\Addressing\AddressFormat\AddressFormatRepository; use CommerceGuys\Addressing\Country\CountryRepository; use CommerceGuys\Addressing\Subdivision\SubdivisionRepository; $countryRepository = new CountryRepository(); $addressFormatRepository = new AddressFormatRepository(); $subdivisionRepository = new SubdivisionRepository(); // Get the country list (countryCode => name), in French. $countryList = $countryRepository->getList('fr-FR'); // Get the country object for Brazil. $brazil = $countryRepository->get('BR'); echo $brazil->getThreeLetterCode(); // BRA echo $brazil->getName(); // Brazil echo $brazil->getCurrencyCode(); // BRL print_r($brazil->getTimezones()); // Get all country objects. $countries = $countryRepository->getAll(); // Get the address format for Brazil. $addressFormat = $addressFormatRepository->get('BR'); // Get the subdivisions for Brazil. $states = $subdivisionRepository->getAll(['BR']); foreach ($states as $state) { $municipalities = $state->getChildren(); } // Get the subdivisions for Brazilian state Ceará. $municipalities = $subdivisionRepository->getAll(['BR', 'CE']); foreach ($municipalities as $municipality) { echo $municipality->getName(); } ``` # Formatters Addresses are formatted according to the address format, in HTML or text. ## DefaultFormatter Formats an address for display, always adds the localized country name. ```php use CommerceGuys\Addressing\Address; use CommerceGuys\Addressing\Formatter\DefaultFormatter; use CommerceGuys\Addressing\AddressFormat\AddressFormatRepository; use CommerceGuys\Addressing\Country\CountryRepository; use CommerceGuys\Addressing\Subdivision\SubdivisionRepository; $addressFormatRepository = new AddressFormatRepository(); $countryRepository = new CountryRepository(); $subdivisionRepository = new SubdivisionRepository(); $formatter = new DefaultFormatter($addressFormatRepository, $countryRepository, $subdivisionRepository); // Options passed to the constructor or format() allow turning off // html rendering, customizing the wrapper element and its attributes. $address = new Address(); $address = $address ->withCountryCode('US') ->withAdministrativeArea('CA') ->withLocality('Mountain View') ->withAddressLine1('1098 Alta Ave'); echo $formatter->format($address); /** Output:
1098 Alta Ave
Mountain View, CA
United States