Takes care of uppercasing fields where required by the format (to faciliate automated mail sorting). Requires specifying the origin country code, allowing it to differentiate between domestic and international mail. In case of domestic mail, the country name is not displayed at all. In case of international mail: 1. The postal code is prefixed with the destination's postal code prefix. 2. The country name is added to the formatted address, in both the current locale and English. This matches the recommandation given by the Universal Postal Union, to avoid difficulties in countries of transit.
Inheritance: extends DefaultFormatter, implements CommerceGuys\Addressing\Formatter\PostalLabelFormatterInterface
 /**
  * @covers \CommerceGuys\Addressing\Formatter\PostalLabelFormatter
  */
 public function testAddressLeadingPostPrefix()
 {
     $address = new Address();
     $address = $address->withCountryCode('CH')->withLocality('Herrliberg')->withPostalCode('8047');
     // Domestic mail shouldn't have the postal code prefix added.
     $expectedLines = ['8047 Herrliberg'];
     $this->formatter->setOriginCountryCode('CH');
     $formattedAddress = $this->formatter->format($address);
     $this->assertFormattedAddress($expectedLines, $formattedAddress);
     // International mail should have the postal code prefix added.
     $expectedLines = ['CH-8047 Herrliberg', 'SWITZERLAND'];
     $this->formatter->setOriginCountryCode('FR');
     $formattedAddress = $this->formatter->format($address);
     $this->assertFormattedAddress($expectedLines, $formattedAddress);
 }
Example #2
0
 /**
  * Format an address to a postal label
  *
  * @param AddressInterface $address
  * @param null $locale
  * @param null $originCountry
  * @param array $options
  * @return string
  */
 public function postalLabelFormat(AddressInterface $address, $locale = null, $originCountry = null, $options = [])
 {
     $locale = $this->normalizeLocale($locale);
     $addressFormatRepository = new AddressFormatRepository();
     $countryRepository = new CountryRepository();
     $subdivisionRepository = new SubdivisionRepository();
     if (null === $originCountry) {
         $countryId = Country::getShopLocation();
         if (null === ($country = CountryQuery::create()->findPk($countryId))) {
             $country = Country::getDefaultCountry();
         }
         $originCountry = $country->getIsoalpha2();
     }
     $formatter = new PostalLabelFormatter($addressFormatRepository, $countryRepository, $subdivisionRepository, $originCountry, $locale, $options);
     $addressFormatted = $formatter->format($address);
     return $addressFormatted;
 }