Skip to content

Commit

Permalink
Merge pull request #8 from tubssp/master
Browse files Browse the repository at this point in the history
Added function validAddressPieces
  • Loading branch information
adamlc committed Jan 27, 2015
2 parents 47cb5bf + ef7d983 commit 919e469
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/Adamlc/AddressFormat/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}


}
61 changes: 60 additions & 1 deletion tests/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,63 @@ public function testArrayAccess()
'Schulstrasse 4'
);
}
}

/**
* 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"
);
}
}

0 comments on commit 919e469

Please sign in to comment.