Пример #1
0
 /**
  * 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);
 }
 public function testIsFormatAddressByAddressCountry()
 {
     $this->configManager->expects($this->at(0))->method('get')->with('oro_locale.format_address_by_address_country')->will($this->returnValue(''));
     $this->configManager->expects($this->at(1))->method('get')->with('oro_locale.format_address_by_address_country')->will($this->returnValue('1'));
     $this->assertFalse($this->localeSettings->isFormatAddressByAddressCountry());
     $this->assertTrue($this->localeSettings->isFormatAddressByAddressCountry());
 }