diff --git a/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index e82313a..bef9eb3 100644 --- a/src/Adamlc/AddressFormat/Format.php +++ b/src/Adamlc/AddressFormat/Format.php @@ -6,6 +6,7 @@ use Adamlc\AddressFormat\Exceptions\LocaleNotSupportedException; use Adamlc\AddressFormat\Exceptions\LocaleParseErrorException; use Adamlc\AddressFormat\Exceptions\LocaleMissingFormatException; +use function explode; /** * Use this call to format a street address according to different locales @@ -66,6 +67,7 @@ class Format implements \ArrayAccess public function setLocale($locale) { //Check if we have information for this locale + $locale = strtoupper($locale); $file = __DIR__ . '/i18n/' . $locale . '.json'; if (file_exists($file)) { //Read the locale information from the file @@ -100,15 +102,7 @@ public function formatAddress($html = false) //Replace the street values foreach ($this->address_map as $key => $value) { - if( empty( $this->input_map[$value] ) ) { - $key = '%' . $key . '%n'; // Also remove the %n newline otherwise it's being left there - $replacement = ''; - } else { - $key = '%' . $key; - $replacement = $this->input_map[$value]; - } - - $formatted_address = str_replace($key, $replacement, $formatted_address); + $formatted_address = str_replace('%' . $key, $this->input_map[$value] ?: '', $formatted_address); } //Remove blank lines from the resulting address @@ -119,15 +113,37 @@ public function formatAddress($html = false) $formatted_address = htmlentities($formatted_address, ENT_QUOTES, 'UTF-8', false); $formatted_address = str_replace('%n', "\n" . '
', $formatted_address); } else { - $formatted_address = trim(str_replace('%n', "\n", $formatted_address)); + $formatted_address = str_replace('%n', "\n", $formatted_address); } - return $formatted_address; + return $this->normalize($formatted_address, $html); } else { throw new LocaleMissingFormatException('Locale missing format'); } } + /** + * Normalize the whitespace within the address + * + * @param string $address + * @param bool $html + * + * @return string + */ + protected function normalize($address, $html = false) + { + $separator = $html ? '
' : "\n"; + $parts = explode($separator, $address); + + $parts = array_filter($parts, 'strlen'); + $parts = array_map('trim', $parts); + + $address = implode($separator, $parts); + + // Remove multiple spaces + return preg_replace('/ +/', ' ', $address); + } + /** * Set an address attribute. * diff --git a/src/Adamlc/AddressFormat/i18n/Test.json b/src/Adamlc/AddressFormat/i18n/_INVALID.json similarity index 100% rename from src/Adamlc/AddressFormat/i18n/Test.json rename to src/Adamlc/AddressFormat/i18n/_INVALID.json diff --git a/tests/FormatTest.php b/tests/FormatTest.php index 06e52a6..2970c28 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -50,7 +50,7 @@ public function testSettingInvalidLocale() public function testLocaleWithInvalidMetaData() { $this->expectException(Adamlc\AddressFormat\Exceptions\LocaleParseErrorException::class); - $this->container->setLocale('Test'); + $this->container->setLocale('_Invalid'); } /** @@ -180,6 +180,55 @@ public function testDeAddressFormatWithMissingAttributes() ); } + /** + * Ensure that addresses doesn't leave markers hanging + * + * @return void + */ + public function testSpanishAddressDoesntLeaveMarkersHanging() + { + //Clear any previously set attributes + $this->container->clearAttributes(); + + //Set Locale and attributes + $this->container->setLocale('es'); + + $this->container->setAttribute('LOCALITY', 'Girona'); + $this->container->setAttribute('RECIPIENT', 'Jesper Jacobsen'); + $this->container->setAttribute('POSTAL_CODE', '17001'); + $this->container->setAttribute('STREET_ADDRESS', 'Gran Via De Jaume X, 123'); + + $this->assertEquals( + "Jesper Jacobsen\nGran Via De Jaume X, 123\n17001 Girona", + $this->container->formatAddress() + ); + } + + /** + * Ensure that addresses doesn't contain excess spaces + * + * @return void + */ + public function testAddressDoesntContainExcessSpaces() + { + //Clear any previously set attributes + $this->container->clearAttributes(); + + //Set Locale and attributes + $this->container->setLocale('es'); + + $this->container->setAttribute('LOCALITY', 'Girona'); + $this->container->setAttribute('RECIPIENT', 'Jesper Jacobsen'); + $this->container->setAttribute('POSTAL_CODE', ''); + $this->container->setAttribute('STREET_ADDRESS', 'Gran Via De Jaume X, 123'); + + $this->assertEquals( + "Jesper Jacobsen\nGran Via De Jaume X, 123\nGirona", + $this->container->formatAddress() + ); + } + + /** * Check that an exception is thrown for invlidate locale *