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/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index 59d2c54..e82313a 100644 --- a/src/Adamlc/AddressFormat/Format.php +++ b/src/Adamlc/AddressFormat/Format.php @@ -100,14 +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); - //Replace new lines! if ($html) { $formatted_address = htmlentities($formatted_address, ENT_QUOTES, 'UTF-8', false); diff --git a/tests/FormatTest.php b/tests/FormatTest.php index b3815c5..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,47 @@ 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" + ); + } + + /** + * 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" + ); } /** @@ -198,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' + ); } /** @@ -255,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" + ); } }