Skip to content

Commit

Permalink
Merge pull request #24 from jmslbam/patch-1
Browse files Browse the repository at this point in the history
Remove %n in front and back of string
  • Loading branch information
adamlc authored Jan 14, 2025
2 parents 131080f + 665cf1e commit b7071fe
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 110 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
12 changes: 9 additions & 3 deletions src/Adamlc/AddressFormat/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
239 changes: 132 additions & 107 deletions tests/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

/**
Expand All @@ -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'
);
}

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

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

/**
Expand Down Expand Up @@ -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'
);
}

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

0 comments on commit b7071fe

Please sign in to comment.