/** * Format an address * * @param AddressInterface $address * @param null $locale * @param bool $html * @param string $htmlTag * @param array $htmlAttributes * @return string */ public function format(AddressInterface $address, $locale = null, $html = true, $htmlTag = "p", $htmlAttributes = []) { $locale = $this->normalizeLocale($locale); $addressFormatRepository = new AddressFormatRepository(); $countryRepository = new CountryRepository(); $subdivisionRepository = new SubdivisionRepository(); $formatter = new DefaultFormatter($addressFormatRepository, $countryRepository, $subdivisionRepository, $locale); $formatter->setOption('html', $html); $formatter->setOption('html_tag', $htmlTag); $formatter->setOption('html_attributes', $htmlAttributes); $addressFormatted = $formatter->format($address); return $addressFormatted; }
/** * @covers \CommerceGuys\Addressing\Formatter\DefaultFormatter * * @uses \CommerceGuys\Addressing\Model\Address * @uses \CommerceGuys\Addressing\Model\AddressFormat * @uses \CommerceGuys\Addressing\Model\FormatStringTrait * @uses \CommerceGuys\Addressing\Model\Subdivision * @uses \CommerceGuys\Addressing\Repository\AddressFormatRepository * @uses \CommerceGuys\Addressing\Repository\CountryRepository * @uses \CommerceGuys\Addressing\Repository\SubdivisionRepository * @uses \CommerceGuys\Addressing\Repository\DefinitionTranslatorTrait */ public function testUnitedStatesIncompleteAddress() { // Create a US address without a locality. $address = new Address(); $address = $address->withCountryCode('US')->withAdministrativeArea('US-CA')->withPostalCode('94043')->withAddressLine1('1098 Alta Ave'); $expectedHtmlLines = ['<p translate="no">', '<span class="address-line1">1098 Alta Ave</span><br>', '<span class="administrative-area">CA</span> <span class="postal-code">94043</span><br>', '<span class="country">United States</span>', '</p>']; $htmlAddress = $this->formatter->format($address); $this->assertFormattedAddress($expectedHtmlLines, $htmlAddress); $expectedTextLines = ['1098 Alta Ave', 'CA 94043', 'United States']; $this->formatter->setOption('html', false); $textAddress = $this->formatter->format($address); $this->assertFormattedAddress($expectedTextLines, $textAddress); // Now add the locality, but remove the administrative area. $address = $address->withLocality('Mountain View')->withAdministrativeArea(''); $expectedHtmlLines = ['<p translate="no">', '<span class="address-line1">1098 Alta Ave</span><br>', '<span class="locality">Mountain View</span>, <span class="postal-code">94043</span><br>', '<span class="country">United States</span>', '</p>']; $this->formatter->setOption('html', true); $htmlAddress = $this->formatter->format($address); $this->assertFormattedAddress($expectedHtmlLines, $htmlAddress); $expectedTextLines = ['1098 Alta Ave', 'Mountain View, 94043', 'United States']; $this->formatter->setOption('html', false); $textAddress = $this->formatter->format($address); $this->assertFormattedAddress($expectedTextLines, $textAddress); }
/** * {@inheritdoc} */ protected function buildView(AddressInterface $address, AddressFormatInterface $addressFormat) { $view = parent::buildView($address, $addressFormat); // Uppercase fields where required by the format. $uppercaseFields = $addressFormat->getUppercaseFields(); foreach ($uppercaseFields as $uppercaseField) { if (isset($view[$uppercaseField])) { $view[$uppercaseField]['value'] = mb_strtoupper($view[$uppercaseField]['value'], 'utf-8'); } } // Handle international mailing. if ($address->getCountryCode() != $this->originCountryCode) { // Prefix the postal code. $field = AddressField::POSTAL_CODE; if (isset($view[$field])) { $view[$field]['value'] = $addressFormat->getPostalCodePrefix() . $view[$field]['value']; } // Universal Postal Union says: "The name of the country of // destination shall be written preferably in the language of the // country of origin. To avoid any difficulty in the countries of // transit, it is desirable for the name of the country of // destination to be added in an internationally known language. $country = $view['country']['value']; $englishCountries = $this->countryRepository->getList('en'); $englishCountry = $englishCountries[$address->getCountryCode()]; if ($country != $englishCountry) { $country .= ' - ' . $englishCountry; } $view['country']['value'] = mb_strtoupper($country, 'utf-8'); } else { // The country is not written in case of domestic mailing. $view['country']['value'] = ''; } return $view; }