Skip to content

Commit

Permalink
Merge pull request #27 from iMusicJJ/feature/fix-hanging-markers
Browse files Browse the repository at this point in the history
Fix hanging markers
  • Loading branch information
adamlc authored Jan 30, 2025
2 parents b7071fe + 5878d45 commit 34a1e65
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
38 changes: 27 additions & 11 deletions src/Adamlc/AddressFormat/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Adamlc\AddressFormat\Exceptions\LocaleNotSupportedException;
use Adamlc\AddressFormat\Exceptions\LocaleParseErrorException;
use Adamlc\AddressFormat\Exceptions\LocaleMissingFormatException;
use function explode;

/**
* Use this call to format a street address according to different locales
Expand Down Expand Up @@ -66,6 +67,7 @@ class Format implements \ArrayAccess
public function setLocale($locale)
{
//Check if we have information for this locale
$locale = strtoupper($locale);
$file = __DIR__ . '/i18n/' . $locale . '.json';
if (file_exists($file)) {
//Read the locale information from the file
Expand Down Expand Up @@ -100,15 +102,7 @@ public function formatAddress($html = false)

//Replace the street values
foreach ($this->address_map as $key => $value) {
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);
$formatted_address = str_replace('%' . $key, $this->input_map[$value] ?: '', $formatted_address);
}

//Remove blank lines from the resulting address
Expand All @@ -119,15 +113,37 @@ public function formatAddress($html = false)
$formatted_address = htmlentities($formatted_address, ENT_QUOTES, 'UTF-8', false);
$formatted_address = str_replace('%n', "\n" . '<br>', $formatted_address);
} else {
$formatted_address = trim(str_replace('%n', "\n", $formatted_address));
$formatted_address = str_replace('%n', "\n", $formatted_address);
}

return $formatted_address;
return $this->normalize($formatted_address, $html);
} else {
throw new LocaleMissingFormatException('Locale missing format');
}
}

/**
* Normalize the whitespace within the address
*
* @param string $address
* @param bool $html
*
* @return string
*/
protected function normalize($address, $html = false)
{
$separator = $html ? '<br>' : "\n";
$parts = explode($separator, $address);

$parts = array_filter($parts, 'strlen');
$parts = array_map('trim', $parts);

$address = implode($separator, $parts);

// Remove multiple spaces
return preg_replace('/ +/', ' ', $address);
}

/**
* Set an address attribute.
*
Expand Down
File renamed without changes.
51 changes: 50 additions & 1 deletion tests/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testSettingInvalidLocale()
public function testLocaleWithInvalidMetaData()
{
$this->expectException(Adamlc\AddressFormat\Exceptions\LocaleParseErrorException::class);
$this->container->setLocale('Test');
$this->container->setLocale('_Invalid');
}

/**
Expand Down Expand Up @@ -180,6 +180,55 @@ public function testDeAddressFormatWithMissingAttributes()
);
}

/**
* Ensure that addresses doesn't leave markers hanging
*
* @return void
*/
public function testSpanishAddressDoesntLeaveMarkersHanging()
{
//Clear any previously set attributes
$this->container->clearAttributes();

//Set Locale and attributes
$this->container->setLocale('es');

$this->container->setAttribute('LOCALITY', 'Girona');
$this->container->setAttribute('RECIPIENT', 'Jesper Jacobsen');
$this->container->setAttribute('POSTAL_CODE', '17001');
$this->container->setAttribute('STREET_ADDRESS', 'Gran Via De Jaume X, 123');

$this->assertEquals(
"Jesper Jacobsen\nGran Via De Jaume X, 123\n17001 Girona",
$this->container->formatAddress()
);
}

/**
* Ensure that addresses doesn't contain excess spaces
*
* @return void
*/
public function testAddressDoesntContainExcessSpaces()
{
//Clear any previously set attributes
$this->container->clearAttributes();

//Set Locale and attributes
$this->container->setLocale('es');

$this->container->setAttribute('LOCALITY', 'Girona');
$this->container->setAttribute('RECIPIENT', 'Jesper Jacobsen');
$this->container->setAttribute('POSTAL_CODE', '');
$this->container->setAttribute('STREET_ADDRESS', 'Gran Via De Jaume X, 123');

$this->assertEquals(
"Jesper Jacobsen\nGran Via De Jaume X, 123\nGirona",
$this->container->formatAddress()
);
}


/**
* Check that an exception is thrown for invlidate locale
*
Expand Down

0 comments on commit 34a1e65

Please sign in to comment.