Choosing the source at runtime allows integrations (such as the symfony
bundle) to stay agnostic about the intl library they need.
* This scripts performs validation to ensure compatibility. */ include '../vendor/autoload.php'; use CommerceGuys\Addressing\Address; use CommerceGuys\Addressing\AddressFormat\AddressField; use CommerceGuys\Addressing\AddressFormat\AddressFormatRepository; use CommerceGuys\Addressing\Country\CountryRepository; use CommerceGuys\Addressing\Validator\Constraints\AddressFormatConstraint; use Symfony\Component\Validator\Validation; $addressFormatRepository = new AddressFormatRepository(); $address = new Address(); $address = $address->withAddressLine1('Address line1')->withAddressLine1('Address line2')->withLocality('Locality'); $validator = Validation::createValidator(); // Create a list of countries for which Google has definitions. $foundCountries = ['ZZ']; $countryRepository = new CountryRepository(); $countries = $countryRepository->getList(); $serviceUrl = 'http://i18napis.appspot.com/address'; $index = file_get_contents($serviceUrl); foreach ($countries as $countryCode => $countryName) { $link = "<a href='/address/data/{$countryCode}'>"; // This is still faster than running a file_exists() for each country code. if (strpos($index, $link) !== FALSE) { $foundCountries[] = $countryCode; } } foreach ($foundCountries as $countryCode) { $addressFormat = $addressFormatRepository->get($countryCode); if (!in_array(AddressField::POSTAL_CODE, $addressFormat->getUsedFields())) { continue; }