Skip to content

Commit

Permalink
handle invalid address #1580
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinx13 committed Aug 5, 2023
1 parent ed8f19f commit cff7f7e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Utils/AddressParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,32 @@ public static function parse(string $address): array
{
$number = null;
$suffix = null;
$address = str_replace(',', '', $address);

if (str_contains($address, '/')) {
return [
'number' => $number,
'suffix' => $suffix,
'street' => $address,
];
}
// Match number and optional suffix at the beginning of the address
preg_match('/^(\d+)\s*(\w+)?\s*/', $address, $matches);
if (isset($matches[1])) {
$number = $matches[1];
if (isset($matches[2]) && \in_array(strtoupper($matches[2]), ExtensionAdresse::toArray())) {
$suffix = strtoupper($matches[2]);
// Remove the number and suffix from the street
$street = preg_replace('/^\d+\s*(?:\w+)?\s*/', '', $address);
} else {
preg_match('/^(\d+)\s+(.*)$/', $address, $matches);
$street = $matches[2];
if (!empty($matches)) {
$street = $matches[2];
} else {
// The number and the street are not separated by a space
// Remove number at the beginning of the address
$street = preg_replace('/^\d+\s*/', '', $address);
}
}
} else {
$street = $address;
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/Utils/AddressParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@ public function testAdressParser(string $address, ?string $number, ?string $suff

public function provideAdresse(): \Generator
{
yield '123Rue de Canteraine' => [
'123Rue de Canteraine',
'123',
null,
'Rue de Canteraine',
];

yield '61/63 Route de Calais' => [
'61/63 Route de Calais',
null,
null,
'61/63 Route de Calais',
];

yield '33RUE THOMAS HERISSON' => [
'33RUE THOMAS HERISSON',
'33',
null,
'RUE THOMAS HERISSON',
];

yield '53, rue de rû' => [
'53, rue de rû',
'53',
null,
'Rue de rû',
];

yield '40 Allée Gustave Cantelon' => [
'40 Allée Gustave Cantelon',
'40',
null,
'Allée Gustave Cantelon',
];

yield '141bis Rue du Pdt J Fitzgerald Kennedy' => [
'141bis Rue du Pdt J Fitzgerald Kennedy',
'141',
Expand Down

0 comments on commit cff7f7e

Please sign in to comment.