/** * 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); }