From 3519d3e0fffef275996d0770a0d51c381821ccb4 Mon Sep 17 00:00:00 2001 From: Jesper Jacobsen Date: Wed, 22 Jan 2025 13:36:25 +0000 Subject: [PATCH 1/7] Add failing test (#26) --- tests/FormatTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/FormatTest.php b/tests/FormatTest.php index 06e52a6..d92df76 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -180,6 +180,31 @@ 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() + ); + } + + /** * Check that an exception is thrown for invlidate locale * From 93f12c55018743df82de7f790b670cfe38e4ad6d Mon Sep 17 00:00:00 2001 From: Jesper Jacobsen Date: Wed, 22 Jan 2025 13:41:29 +0000 Subject: [PATCH 2/7] Fix failing testcase (#26) --- src/Adamlc/AddressFormat/Format.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index e82313a..bc3674c 100644 --- a/src/Adamlc/AddressFormat/Format.php +++ b/src/Adamlc/AddressFormat/Format.php @@ -100,15 +100,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 From d251e2ca264cfb23f485789f3be7a3c9b7de1870 Mon Sep 17 00:00:00 2001 From: Jesper Jacobsen Date: Wed, 29 Jan 2025 18:57:37 +0100 Subject: [PATCH 3/7] Add failing test --- tests/FormatTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/FormatTest.php b/tests/FormatTest.php index d92df76..3b57dcd 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -204,6 +204,30 @@ public function testSpanishAddressDoesntLeaveMarkersHanging() ); } + /** + * 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 From 5d6783d57be580c4f69dd52dcb75d282e2f68c91 Mon Sep 17 00:00:00 2001 From: Jesper Jacobsen Date: Wed, 29 Jan 2025 19:00:59 +0100 Subject: [PATCH 4/7] Normalize addresses (#26) --- src/Adamlc/AddressFormat/Format.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index bc3674c..5634850 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 @@ -111,15 +112,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. * From 42a2050308d11070eda56b3eec5d404d3900b0ac Mon Sep 17 00:00:00 2001 From: Jesper Jacobsen Date: Thu, 30 Jan 2025 12:26:50 +0100 Subject: [PATCH 5/7] Allow lowercase locales --- src/Adamlc/AddressFormat/Format.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index 5634850..bef9eb3 100644 --- a/src/Adamlc/AddressFormat/Format.php +++ b/src/Adamlc/AddressFormat/Format.php @@ -67,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 From f89c5ebdf211fbc4ff2d8854bf1e7d727247e762 Mon Sep 17 00:00:00 2001 From: Jesper Jacobsen Date: Thu, 30 Jan 2025 12:54:21 +0100 Subject: [PATCH 6/7] Rename `Test.json` => `TEST.json` to follow uppercase taxonomy (#27) --- src/Adamlc/AddressFormat/i18n/{Test.json => TEST.json} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Adamlc/AddressFormat/i18n/{Test.json => TEST.json} (100%) diff --git a/src/Adamlc/AddressFormat/i18n/Test.json b/src/Adamlc/AddressFormat/i18n/TEST.json similarity index 100% rename from src/Adamlc/AddressFormat/i18n/Test.json rename to src/Adamlc/AddressFormat/i18n/TEST.json From 5878d452aa5b635eeaeebfc3db212a0571c8d22b Mon Sep 17 00:00:00 2001 From: Jesper Jacobsen Date: Thu, 30 Jan 2025 12:56:37 +0100 Subject: [PATCH 7/7] Remove test fixture from normal "ISO" namespace by prefixing it with `_` --- src/Adamlc/AddressFormat/i18n/{TEST.json => _INVALID.json} | 0 tests/FormatTest.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/Adamlc/AddressFormat/i18n/{TEST.json => _INVALID.json} (100%) 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 3b57dcd..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'); } /**