/** * @covers ::getAll * @covers ::processDefinition * @covers ::getGenericDefinition * @covers ::getDefinitions */ public function testGetAll() { $addressFormatRepository = new AddressFormatRepository(); $addressFormats = $addressFormatRepository->getAll(); $this->assertArrayHasKey('ES', $addressFormats); $this->assertArrayHasKey('RS', $addressFormats); $this->assertEquals('ES', $addressFormats['ES']->getCountryCode()); $this->assertEquals(LocalityType::CITY, $addressFormats['ES']->getLocalityType()); $this->assertEquals('RS', $addressFormats['RS']->getCountryCode()); $this->assertEquals(LocalityType::CITY, $addressFormats['RS']->getLocalityType()); }
/** * Checks whether predefined subdivisions exist for the provided parents. * * @param array $parents The parents (country code, subdivision codes). * * @return bool TRUE if predefined subdivisions exist for the provided * parents, FALSE otherwise. */ protected function hasData(array $parents) { $countryCode = $parents[0]; $addressFormat = $this->addressFormatRepository->get($countryCode); $depth = $addressFormat->getSubdivisionDepth(); if ($depth == 0) { return false; } // At least the first level has data. $hasData = true; if (count($parents) > 1) { // After the first level it is possible for predefined subdivisions // to exist at a given level, but not for that specific parent. // That's why the parent definition has the most precise answer. $grandparents = $parents; $parentId = array_pop($grandparents); $parentGroup = $this->buildGroup($grandparents); if (isset($this->definitions[$parentGroup]['subdivisions'][$parentId])) { $definition = $this->definitions[$parentGroup]['subdivisions'][$parentId]; $hasData = !empty($definition['has_children']); } else { // The parent definition wasn't loaded previously, fallback // to guessing based on depth. $neededDepth = count($parents); $hasData = $neededDepth <= $depth; } } return $hasData; }
<?php /** * Google's dataset includes regular expressions for validating postal codes. * These regular expressions are meant to be consumed by Google's Java library, * and compatibility with PHP's preg_match is not 100% guaranteed. * 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; }