public function format(AddressInterface $address)
{
$countryCode = $address->getCountryCode();
$addressFormat = $this->addressFormatRepository->get($countryCode);
// Add the country to the bottom or the top of the format string,
// depending on whether the format is minor-to-major or major-to-minor.
if (LocaleHelper::match($addressFormat->getLocale(), $address->getLocale())) {
$formatString = '%country' . "\n" . $addressFormat->getLocalFormat();
} else {
$formatString = $addressFormat->getFormat() . "\n" . '%country';
}
$view = $this->buildView($address, $addressFormat);
$view = $this->renderView($view);
// Insert the rendered elements into the format string.
$replacements = [];
foreach ($view as $key => $element) {
$replacements['%' . $key] = $element;
}
$output = strtr($formatString, $replacements);
$output = $this->cleanupOutput($output);
if (!empty($this->options['html'])) {
$output = nl2br($output, false);
// Add the HTML wrapper element.
$attributes = $this->renderAttributes($this->options['html_attributes']);
$prefix = '<' . $this->options['html_tag'] . ' ' . $attributes . '>' . "\n";
$suffix = "\n" . '</' . $this->options['html_tag'] . '>';
$output = $prefix . $output . $suffix;
}
return $output;
}