Skip to content

dnagirl/addressing

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

addressing

Build Status

A PHP 5.4+ addressing library, powered by Google's dataset.

Stores and manipulates postal addresses, meant to identify a precise recipient location for shipping or billing purposes.

Features:

  • Address formats for 200 countries
  • Subdivisions (administrative areas, localities, dependent localities) for 40 countries
  • Subdivision translations for all of the parent country's (i.e Canada, Switzerland) official languages.
  • Validation (via Symfony Validator)
  • Form generation (via Symfony Form)
  • Postal formatting
  • Zones via the commerceguys/zone library.

The dataset is stored locally in JSON format, generated from Google's Address Data Service.

The CLDR country list is used (via symfony/intl or commerceguys/intl), because it includes additional countries for addressing purposes, such as Canary Islands (IC).

Further backstory can be found in this blog post.

Data model

The address object represents a postal adddress, and has the following fields:

  • Country
  • Administrative area
  • Locality (City)
  • Dependent Locality
  • Postal code
  • Sorting code
  • Address line 1
  • Address line 2
  • Organization
  • Recipient

Field names follow the OASIS eXtensible Address Language (xAL) standard.

The address format object contains the following data for a country:

  • 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 subdivision object contains the following data:

  • 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 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.

use CommerceGuys\Addressing\Repository\AddressFormatRepository;
use CommerceGuys\Addressing\Repository\SubdivisionRepository;

$addressFormatRepository = new AddressFormatRepository();
$subdivisionRepository = new SubdivisionRepository();

// 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 Canada, in French.
$states = $subdivisionRepository->getAll('CA', 0, 'fr');
foreach ($states as $state) {
    echo $state->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.

use CommerceGuys\Addressing\Formatter\DefaultFormatter;
use CommerceGuys\Addressing\Repository\AddressFormatRepository;
use CommerceGuys\Addressing\Repository\CountryRepository;
use CommerceGuys\Addressing\Repository\SubdivisionRepository;

$addressFormatRepository = new AddressFormatRepository();
$countryRepository = new CountryRepository();
$subdivisionRepository = new SubdivisionRepository();
$formatter = new DefaultFormatter($addressFormatRepository, $countryRepository, $subdivisionRepository);
// Options passed to the constructor or setOption / setOptions allow turning
// off html rendering, customizing the wrapper element and its attributes.

$address = new Address();
$address
    ->setCountryCode('US')
    ->setAdministrativeArea('US-CA')
    ->setAddressLine1('1098 Alta Ave')
    ->setLocality('Mountain View');

echo $formatter->format($address);

/** Output:
<p translate="no">
<span class="address-line1">1098 Alta Ave</span><br>
<span class="locality">Mountain View</span>, <span class="administrative-area">CA</span><br>
<span class="country">United States</span>
</p>
**/

PostalLabelFormatter

Takes care of uppercasing fields where required by the format (to faciliate automated mail sorting).

Requires specifying the origin country code, allowing it to differentiate between domestic and international mail. In case of domestic mail, the country name is not displayed at all. In case of international mail:

  1. The postal code is prefixed with the destination's postal code prefix.
  2. The country name is added to the formatted address, in both the current locale and English. This matches the recommandation given by the Universal Postal Union, to avoid difficulties in countries of transit.
use CommerceGuys\Addressing\Formatter\PostalLabelFormatter;
use CommerceGuys\Addressing\Repository\AddressFormatRepository;
use CommerceGuys\Addressing\Repository\CountryRepository;
use CommerceGuys\Addressing\Repository\SubdivisionRepository;

$addressFormatRepository = new AddressFormatRepository();
$countryRepository = new CountryRepository();
$subdivisionRepository = new SubdivisionRepository();
// Defaults to text rendering. Requires setting the origin country code
// (e.g. 'FR') through the constructor or the setter, before calling format().
$formatter = new PostalLabelFormatter($addressFormatRepository, $countryRepository, $subdivisionRepository, 'FR', 'fr');

$address = new Address();
$address
    ->setCountryCode('US')
    ->setAdministrativeArea('US-CA')
    ->setAddressLine1('1098 Alta Ave')
    ->setLocality('Mountain View');

echo $formatter->format($address);

/** Output:
1098 Alta Ave
MOUNTAIN VIEW, CA 94043
ÉTATS-UNIS - UNITED STATES
**/

Validator

Address validation relies on the Symfony Validator library.

Checks performed:

  • All required fields are filled in.
  • All fields unused by the country's format are empty.
  • All subdivisions are valid (values matched against predefined subdivisions).
  • The postal code is valid (country and subdivision-level patterns).
use CommerceGuys\Addressing\Model\Address;
use CommerceGuys\Addressing\Validator\Constraints\AddressFormat;
use CommerceGuys\Addressing\Validator\Constraints\Country;
use Symfony\Component\Validator\Validation;

$address = new Address();
$address->setCountryCode('FR');

$validator = Validation::createValidator();
// Validate the country code, then validate the rest of the address.
$violations = $validator->validateValue($address->getCountryCode(), new Country());
if (!$violations->count()) {
  $violations = $validator->validateValue($address, new AddressFormat());
}

About

A PHP 5.4+ addressing library, powered by Google's dataset.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%