/** * Format address * * @param AddressInterface $address * @param null|string $country * @param string $newLineSeparator * @return string */ public function format(AddressInterface $address, $country = null, $newLineSeparator = "\n") { if (!$country) { $country = null; if ($this->localeSettings->isFormatAddressByAddressCountry()) { $country = $address->getCountryIso2(); } else { $country = $this->localeSettings->getCountry(); } if (!$country) { $country = LocaleConfiguration::DEFAULT_COUNTRY; } } $format = $this->getAddressFormat($country); $countryLocale = $this->localeSettings->getLocaleByCountry($country); $formatted = preg_replace_callback('/%(\\w+)%/', function ($data) use($address, $countryLocale, $newLineSeparator) { $key = $data[1]; $lowerCaseKey = strtolower($key); if ('name' === $lowerCaseKey) { $value = $this->nameFormatter->format($address, $countryLocale); } elseif ('street' === $lowerCaseKey) { $value = trim($this->getValue($address, 'street') . ' ' . $this->getValue($address, 'street2')); } elseif ('street1' === $lowerCaseKey) { $value = $this->getValue($address, 'street'); } elseif ('country' === $lowerCaseKey) { $value = $this->getValue($address, 'countryName'); } elseif ('region' === $lowerCaseKey) { $value = $this->getValue($address, 'regionName'); } elseif ('region_code' === $lowerCaseKey) { $value = $this->getValue($address, 'regionCode'); if (!$value) { $value = $this->getValue($address, 'regionName'); } } else { $value = $this->getValue($address, $lowerCaseKey); } if ($value) { if ($key !== $lowerCaseKey) { $value = strtoupper($value); } return $value; } return ''; }, $format); $formatted = str_replace($newLineSeparator . $newLineSeparator, $newLineSeparator, str_replace('\\n', $newLineSeparator, $formatted)); $formatted = trim($formatted, $newLineSeparator); $formatted = preg_replace('/ +/', ' ', $formatted); $formatted = preg_replace('/ +\\n/', "\n", $formatted); return trim($formatted); }
/** * @dataProvider getLocaleByCountryDataProvider */ public function testGetLocaleByCountry(array $localeData, $countryCode, $expectedLocale, $defaultLocale = null) { $this->localeSettings->addLocaleData($localeData); if (null !== $defaultLocale) { $this->configManager->expects($this->once())->method('get')->with('oro_locale.locale')->will($this->returnValue($defaultLocale)); } else { $this->configManager->expects($this->never())->method($this->anything()); } $this->assertEquals($expectedLocale, $this->localeSettings->getLocaleByCountry($countryCode)); }