From 0ad0c77883663ac99f504f0c6bdb17ba82176533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez?= Date: Sun, 15 Sep 2024 23:36:46 +0200 Subject: [PATCH 1/6] Remove %n in front and back of string So if `RECIPIENT` or `ORGANIZATION` are not filled or empty, then this results in only a `STREET_ADDRESS` that's prefix with an `%n` which will be replace by a `
` which results in an address that's starts a line lower then needed. Fixes #23 --- src/Adamlc/AddressFormat/Format.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index 59d2c54..0e65dee 100644 --- a/src/Adamlc/AddressFormat/Format.php +++ b/src/Adamlc/AddressFormat/Format.php @@ -107,6 +107,8 @@ public function formatAddress($html = false) //Remove blank lines from the resulting address $formatted_address = preg_replace('((\%n)+)', '%n', $formatted_address); + //Remove %n in front and back of string + $formatted_address = trim($formatted_address, '%n'); //Replace new lines! if ($html) { From 79ec62ec3d160880241ac25fec7c93306f8de474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez?= Date: Fri, 10 Jan 2025 14:24:45 +0100 Subject: [PATCH 2/6] Remove unused %n when input_map[$value] is empty Otherwise you could end up with 2 `%n%n` after each other that get converted to `

` --- src/Adamlc/AddressFormat/Format.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index 0e65dee..e82313a 100644 --- a/src/Adamlc/AddressFormat/Format.php +++ b/src/Adamlc/AddressFormat/Format.php @@ -100,16 +100,20 @@ public function formatAddress($html = false) //Replace the street values foreach ($this->address_map as $key => $value) { - $replacement = empty($this->input_map[$value]) ? '' : $this->input_map[$value]; - $formatted_address = str_replace('%' . $key, $replacement, $formatted_address); + 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); } //Remove blank lines from the resulting address $formatted_address = preg_replace('((\%n)+)', '%n', $formatted_address); - //Remove %n in front and back of string - $formatted_address = trim($formatted_address, '%n'); - //Replace new lines! if ($html) { $formatted_address = htmlentities($formatted_address, ENT_QUOTES, 'UTF-8', false); From 2410afec166c006037cd80afcab345f7182a04f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez?= Date: Fri, 10 Jan 2025 14:26:21 +0100 Subject: [PATCH 3/6] Update test when keeping attributes empty So you don't end up with '%n%n' which gets converted to `

` --- tests/FormatTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/FormatTest.php b/tests/FormatTest.php index b3815c5..da1f9fc 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -155,6 +155,31 @@ public function testDeAddressFormat() ); } + /** + * Check the format of a DE address is expected even when missing attributes + * + * @return void + */ + public function testDeAddressFormatWithMissingAttributes() + { + //Clear any previously set attributes + $this->container->clearAttributes(); + + //Set Locale and attributes + $this->container->setLocale('DE'); + + $this->container->setAttribute('LOCALITY', 'Oyenhausen'); + $this->container->setAttribute('RECIPIENT', ''); + $this->container->setAttribute('ORGANIZATION', ''); + $this->container->setAttribute('POSTAL_CODE', '32547'); + $this->container->setAttribute('STREET_ADDRESS', 'Schulstrasse 4'); + + $this->assertEquals( + $this->container->formatAddress(), + "Schulstrasse 4\n32547 Oyenhausen" + ); + } + /** * Check that an exception is thrown for invlidate locale * From b042c814a366d76e68615c57f706886048acd779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez?= Date: Sat, 11 Jan 2025 00:10:03 +0100 Subject: [PATCH 4/6] Set assertEquals to 1 line --- tests/FormatTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/FormatTest.php b/tests/FormatTest.php index da1f9fc..315adaa 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -175,8 +175,7 @@ public function testDeAddressFormatWithMissingAttributes() $this->container->setAttribute('STREET_ADDRESS', 'Schulstrasse 4'); $this->assertEquals( - $this->container->formatAddress(), - "Schulstrasse 4\n32547 Oyenhausen" + $this->container->formatAddress(), "Schulstrasse 4\n32547 Oyenhausen" ); } From 3e1c4d40c117934b7f824bdb3a5388b3da88b80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez?= Date: Mon, 13 Jan 2025 15:32:26 +0100 Subject: [PATCH 5/6] Changer from tabs to spaces --- tests/FormatTest.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/FormatTest.php b/tests/FormatTest.php index 315adaa..9639743 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -162,21 +162,21 @@ public function testDeAddressFormat() */ public function testDeAddressFormatWithMissingAttributes() { - //Clear any previously set attributes - $this->container->clearAttributes(); + //Clear any previously set attributes + $this->container->clearAttributes(); - //Set Locale and attributes - $this->container->setLocale('DE'); + //Set Locale and attributes + $this->container->setLocale('DE'); - $this->container->setAttribute('LOCALITY', 'Oyenhausen'); - $this->container->setAttribute('RECIPIENT', ''); - $this->container->setAttribute('ORGANIZATION', ''); - $this->container->setAttribute('POSTAL_CODE', '32547'); - $this->container->setAttribute('STREET_ADDRESS', 'Schulstrasse 4'); + $this->container->setAttribute('LOCALITY', 'Oyenhausen'); + $this->container->setAttribute('RECIPIENT', ''); + $this->container->setAttribute('ORGANIZATION', ''); + $this->container->setAttribute('POSTAL_CODE', '32547'); + $this->container->setAttribute('STREET_ADDRESS', 'Schulstrasse 4'); - $this->assertEquals( - $this->container->formatAddress(), "Schulstrasse 4\n32547 Oyenhausen" - ); + $this->assertEquals( + $this->container->formatAddress(), "Schulstrasse 4\n32547 Oyenhausen" + ); } /** From 665cf1e3c94e138b1fbea70d7a9ddc1ab3b07505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Mart=C3=ADnez?= Date: Mon, 13 Jan 2025 21:32:35 +0100 Subject: [PATCH 6/6] Add .editorconfig and convert tabs to spaces --- .editorconfig | 16 ++++ tests/FormatTest.php | 217 ++++++++++++++++++++++--------------------- 2 files changed, 125 insertions(+), 108 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8791d9f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.yml] +indent_style = space +indent_size = 2 \ No newline at end of file diff --git a/tests/FormatTest.php b/tests/FormatTest.php index 9639743..06e52a6 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -60,10 +60,10 @@ public function testLocaleWithInvalidMetaData() */ public function testSetAttributeWithValidAttribute() { - $this->assertEquals( - $this->container->setAttribute('ADMIN_AREA', 'Foo Land'), - 'Foo Land' - ); + $this->assertEquals( + $this->container->setAttribute('ADMIN_AREA', 'Foo Land'), + 'Foo Land' + ); } /** @@ -84,12 +84,12 @@ public function testSetAttributeWithInvalidAttribute() */ public function testGetAttributeWithValidAttribute() { - $this->container->setAttribute('ADMIN_AREA', 'Foo Land'); + $this->container->setAttribute('ADMIN_AREA', 'Foo Land'); - $this->assertEquals( - $this->container->getAttribute('ADMIN_AREA'), - 'Foo Land' - ); + $this->assertEquals( + $this->container->getAttribute('ADMIN_AREA'), + 'Foo Land' + ); } /** @@ -110,24 +110,24 @@ public function testGetAttributeWithInvalidAttribute() */ public function testGbAddressFormat() { - //Clear any previously set attributes - $this->container->clearAttributes(); - - //Set Locale and attributes - $this->container->setLocale('GB'); - - $this->container->setAttribute('ADMIN_AREA', 'London'); - $this->container->setAttribute('LOCALITY', 'Greenwich'); - $this->container->setAttribute('RECIPIENT', 'Joe Bloggs'); - $this->container->setAttribute('ORGANIZATION', 'Novotel London'); - $this->container->setAttribute('POSTAL_CODE', 'SE10 8JA'); - $this->container->setAttribute('STREET_ADDRESS', '173-185 Greenwich High Road'); - $this->container->setAttribute('COUNTRY', 'United Kingdom'); - - $this->assertEquals( - $this->container->formatAddress(), - "Joe Bloggs\nNovotel London\n173-185 Greenwich High Road\nGreenwich\nSE10 8JA" - ); + //Clear any previously set attributes + $this->container->clearAttributes(); + + //Set Locale and attributes + $this->container->setLocale('GB'); + + $this->container->setAttribute('ADMIN_AREA', 'London'); + $this->container->setAttribute('LOCALITY', 'Greenwich'); + $this->container->setAttribute('RECIPIENT', 'Joe Bloggs'); + $this->container->setAttribute('ORGANIZATION', 'Novotel London'); + $this->container->setAttribute('POSTAL_CODE', 'SE10 8JA'); + $this->container->setAttribute('STREET_ADDRESS', '173-185 Greenwich High Road'); + $this->container->setAttribute('COUNTRY', 'United Kingdom'); + + $this->assertEquals( + $this->container->formatAddress(), + "Joe Bloggs\nNovotel London\n173-185 Greenwich High Road\nGreenwich\nSE10 8JA" + ); } /** @@ -137,22 +137,22 @@ public function testGbAddressFormat() */ public function testDeAddressFormat() { - //Clear any previously set attributes - $this->container->clearAttributes(); - - //Set Locale and attributes - $this->container->setLocale('DE'); - - $this->container->setAttribute('LOCALITY', 'Oyenhausen'); - $this->container->setAttribute('RECIPIENT', 'Eberhard Wellhausen'); - $this->container->setAttribute('ORGANIZATION', 'Wittekindshof'); - $this->container->setAttribute('POSTAL_CODE', '32547'); - $this->container->setAttribute('STREET_ADDRESS', 'Schulstrasse 4'); - - $this->assertEquals( - $this->container->formatAddress(), - "Eberhard Wellhausen\nWittekindshof\nSchulstrasse 4\n32547 Oyenhausen" - ); + //Clear any previously set attributes + $this->container->clearAttributes(); + + //Set Locale and attributes + $this->container->setLocale('DE'); + + $this->container->setAttribute('LOCALITY', 'Oyenhausen'); + $this->container->setAttribute('RECIPIENT', 'Eberhard Wellhausen'); + $this->container->setAttribute('ORGANIZATION', 'Wittekindshof'); + $this->container->setAttribute('POSTAL_CODE', '32547'); + $this->container->setAttribute('STREET_ADDRESS', 'Schulstrasse 4'); + + $this->assertEquals( + $this->container->formatAddress(), + "Eberhard Wellhausen\nWittekindshof\nSchulstrasse 4\n32547 Oyenhausen" + ); } /** @@ -175,7 +175,8 @@ public function testDeAddressFormatWithMissingAttributes() $this->container->setAttribute('STREET_ADDRESS', 'Schulstrasse 4'); $this->assertEquals( - $this->container->formatAddress(), "Schulstrasse 4\n32547 Oyenhausen" + $this->container->formatAddress(), + "Schulstrasse 4\n32547 Oyenhausen" ); } @@ -222,39 +223,39 @@ public function testNotGivenFormatThrowsException() */ public function testArrayAccess() { - //Clear any previously set attributes - $this->container->clearAttributes(); - - $this->container['LOCALITY'] = 'Oyenhausen'; - $this->container['RECIPIENT'] = 'Eberhard Wellhausen'; - $this->container['ORGANIZATION'] = 'Wittekindshof'; - $this->container['POSTAL_CODE'] = '32547'; - $this->container['STREET_ADDRESS'] = 'Schulstrasse 4'; - - $this->assertEquals( - $this->container['LOCALITY'], - 'Oyenhausen' - ); - - $this->assertEquals( - $this->container['RECIPIENT'], - 'Eberhard Wellhausen' - ); - - $this->assertEquals( - $this->container['ORGANIZATION'], - 'Wittekindshof' - ); - - $this->assertEquals( - $this->container['POSTAL_CODE'], - '32547' - ); - - $this->assertEquals( - $this->container['STREET_ADDRESS'], - 'Schulstrasse 4' - ); + //Clear any previously set attributes + $this->container->clearAttributes(); + + $this->container['LOCALITY'] = 'Oyenhausen'; + $this->container['RECIPIENT'] = 'Eberhard Wellhausen'; + $this->container['ORGANIZATION'] = 'Wittekindshof'; + $this->container['POSTAL_CODE'] = '32547'; + $this->container['STREET_ADDRESS'] = 'Schulstrasse 4'; + + $this->assertEquals( + $this->container['LOCALITY'], + 'Oyenhausen' + ); + + $this->assertEquals( + $this->container['RECIPIENT'], + 'Eberhard Wellhausen' + ); + + $this->assertEquals( + $this->container['ORGANIZATION'], + 'Wittekindshof' + ); + + $this->assertEquals( + $this->container['POSTAL_CODE'], + '32547' + ); + + $this->assertEquals( + $this->container['STREET_ADDRESS'], + 'Schulstrasse 4' + ); } /** @@ -279,38 +280,38 @@ public function testValidAddressPiecesLocaleMissingFormatException() */ public function testValidAddressPieces() { - //Clear any previously set attributes - $this->container->clearAttributes(); + //Clear any previously set attributes + $this->container->clearAttributes(); //Set Locale - $this->container->setLocale('DE'); - - //get the ordered adress pieces for this locale - $validAddressPieces = $this->container->validAddressPieces(); - - $this->assertEquals( - $validAddressPieces[0], - "RECIPIENT" - ); - - $this->assertEquals( - $validAddressPieces[1], - "ORGANIZATION" - ); - - $this->assertEquals( - $validAddressPieces[2], - "STREET_ADDRESS" - ); - - $this->assertEquals( - $validAddressPieces[3], - "POSTAL_CODE" - ); - - $this->assertEquals( - $validAddressPieces[4], - "LOCALITY" - ); + $this->container->setLocale('DE'); + + //get the ordered adress pieces for this locale + $validAddressPieces = $this->container->validAddressPieces(); + + $this->assertEquals( + $validAddressPieces[0], + "RECIPIENT" + ); + + $this->assertEquals( + $validAddressPieces[1], + "ORGANIZATION" + ); + + $this->assertEquals( + $validAddressPieces[2], + "STREET_ADDRESS" + ); + + $this->assertEquals( + $validAddressPieces[3], + "POSTAL_CODE" + ); + + $this->assertEquals( + $validAddressPieces[4], + "LOCALITY" + ); } }