public function testAddAddressFormats()
 {
     $usFormat = [LocaleSettings::ADDRESS_FORMAT_KEY => '%name%\\n%organization%\\n%street%\\n%CITY% %REGION% %COUNTRY% %postal_code%'];
     $usFormatModified = [LocaleSettings::ADDRESS_FORMAT_KEY => '%name%\\n%organization%\\n%street%\\n%CITY% %REGION_CODE% %COUNTRY% %postal_code%'];
     $ruFormat = [LocaleSettings::ADDRESS_FORMAT_KEY => '%postal_code% %COUNTRY% %CITY%\\n%STREET%\\n%organization%\\n%name%'];
     $this->assertEmpty($this->localeSettings->getAddressFormats());
     $this->localeSettings->addAddressFormats(['US' => $usFormat]);
     $this->assertEquals(['US' => $usFormat], $this->localeSettings->getAddressFormats());
     $this->localeSettings->addAddressFormats(['US' => $usFormatModified, 'RU' => $ruFormat]);
     $this->assertEquals(['US' => $usFormatModified, 'RU' => $ruFormat], $this->localeSettings->getAddressFormats());
 }
 /**
  * Get address format based on locale or region, if argument is not passed locale from
  * system configuration will be used.
  *
  * @param string|null $localeOrRegion
  * @throws \RuntimeException
  */
 public function getAddressFormat($localeOrRegion = null)
 {
     if (!$localeOrRegion) {
         $localeOrRegion = $this->localeSettings->getLocale();
     }
     $addressFormats = $this->localeSettings->getAddressFormats();
     // matched by country (for example - "RU")
     if (isset($addressFormats[$localeOrRegion][LocaleSettings::ADDRESS_FORMAT_KEY])) {
         return $addressFormats[$localeOrRegion][LocaleSettings::ADDRESS_FORMAT_KEY];
     }
     // matched by locale region - "CA"
     $localeParts = \Locale::parseLocale($localeOrRegion);
     if (isset($localeParts[\Locale::REGION_TAG])) {
         $match = $localeParts[\Locale::REGION_TAG];
         if (isset($match, $addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY])) {
             return $addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY];
         }
     }
     // match by default country in system configuration settings
     $match = $this->localeSettings->getCountry();
     if ($match !== $localeOrRegion && isset($addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY])) {
         return $addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY];
     }
     // fallback to default country
     $match = LocaleConfiguration::DEFAULT_COUNTRY;
     if (isset($addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY])) {
         return $addressFormats[$match][LocaleSettings::ADDRESS_FORMAT_KEY];
     }
     throw new \RuntimeException(sprintf('Cannot get address format for "%s"', $localeOrRegion));
 }
 /**
  * Get address formats converted to simplified structure.
  *
  * @param LocaleSettings $localeSettings
  * @return array
  */
 protected function getAddressFormats(LocaleSettings $localeSettings)
 {
     $result = array();
     $formats = $localeSettings->getAddressFormats();
     foreach ($formats as $country => $formatData) {
         $result[$country] = $formatData[LocaleSettings::ADDRESS_FORMAT_KEY];
     }
     return $result;
 }