From 3342ef5055d8ff899b55792005fa167503131939 Mon Sep 17 00:00:00 2001 From: Jesper Jacobsen Date: Fri, 31 Jan 2025 14:36:54 +0100 Subject: [PATCH] Fix hanging newline (#28) --- src/Adamlc/AddressFormat/Format.php | 6 +++++- tests/FormatTest.php | 31 +++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index bef9eb3..c99ab70 100644 --- a/src/Adamlc/AddressFormat/Format.php +++ b/src/Adamlc/AddressFormat/Format.php @@ -6,7 +6,9 @@ use Adamlc\AddressFormat\Exceptions\LocaleNotSupportedException; use Adamlc\AddressFormat\Exceptions\LocaleParseErrorException; use Adamlc\AddressFormat\Exceptions\LocaleMissingFormatException; +use function array_filter; use function explode; +use function trim; /** * Use this call to format a street address according to different locales @@ -135,7 +137,9 @@ protected function normalize($address, $html = false) $separator = $html ? '
' : "\n"; $parts = explode($separator, $address); - $parts = array_filter($parts, 'strlen'); + $parts = array_filter($parts, function ($part) { + return ! empty(trim($part)); + }); $parts = array_map('trim', $parts); $address = implode($separator, $parts); diff --git a/tests/FormatTest.php b/tests/FormatTest.php index 2970c28..f938f6b 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -53,6 +53,29 @@ public function testLocaleWithInvalidMetaData() $this->container->setLocale('_Invalid'); } + /** + * Test setting a single attribute + * + * @return void + */ + public function testSettingASingleAttribute() + { + $this->container->clearAttributes(); + $this->container->setLocale('ES'); + + $this->container->setAttribute('RECIPIENT', ''); + $this->container->setAttribute('ORGANIZATION', ''); + $this->container->setAttribute('STREET_ADDRESS', ''); + $this->container->setAttribute('POSTAL_CODE', ''); + $this->container->setAttribute('LOCALITY', ''); + $this->container->setAttribute('RECIPIENT', 'Jesper Jacobsen'); + + $this->assertEquals( + $this->container->formatAddress(), + "Jesper Jacobsen" + ); + } + /** * Test setting a valid attribute * @@ -308,10 +331,10 @@ public function testArrayAccess() } /** - * Check that an exception is thrown for validAddressPieces by invlidate locale - * - * @return void - */ + * Check that an exception is thrown for validAddressPieces by invlidate locale + * + * @return void + */ public function testValidAddressPiecesLocaleMissingFormatException() { //Clear any previously set attributes