From ef7d9837304dc99f9f8660c0a9c2f524b66157ff Mon Sep 17 00:00:00 2001 From: Andreas Lemke Date: Sun, 18 Jan 2015 17:38:36 +0100 Subject: [PATCH] added function validAddressPieces added function validAddressPieces Update FormatTest.php Update FormatTest.php Update FormatTest.php Update FormatTest.php Update FormatTest.php --- src/Adamlc/AddressFormat/Format.php | 29 ++++++++++++++ tests/FormatTest.php | 61 ++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/Adamlc/AddressFormat/Format.php b/src/Adamlc/AddressFormat/Format.php index f3012b5..e7d07af 100644 --- a/src/Adamlc/AddressFormat/Format.php +++ b/src/Adamlc/AddressFormat/Format.php @@ -191,4 +191,33 @@ public function offsetUnset($offset) $this->offsetSet($offset, ''); } } + + /** + * Return the valid pieces + * + * @access public + * @return array + */ + public function validAddressPieces() + { + $return = array(); + + if (isset($this->locale['fmt'])) + { + $address_format_array = explode("%", $this->locale['fmt']); + foreach($address_format_array as $key => $value ) + { + $value = trim($value); + if( !empty($value) && isset($this->address_map[$value]) ) + { + $return[]=$this->address_map[$value]; + } + } + return $return; + } else { + throw new LocaleMissingFormatException('Locale missing format'); + } + } + + } diff --git a/tests/FormatTest.php b/tests/FormatTest.php index 39f6bc1..151e64f 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -233,4 +233,63 @@ public function testArrayAccess() 'Schulstrasse 4' ); } -} \ No newline at end of file + + /** + * Check that an exception is thrown for validAddressPieces by invlidate locale + * + * @expectedException Adamlc\AddressFormat\Exceptions\LocaleMissingFormatException + * @return void + */ + public function testValidAddressPiecesLocaleMissingFormatException() + { + //Clear any previously set attributes + $this->container->clearAttributes(); + + //Set expected Exception + $this->setExpectedException('Adamlc\AddressFormat\Exceptions\LocaleMissingFormatException'); + + $this->container->validAddressPieces(); + } + + /** + * Test get the ordered adress pieces for this locale + * + * @return void + */ + public function testValidAddressPieces() + { + //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" + ); + } +}