Skip to content

Commit

Permalink
Improve readability of address normalization code
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Jan 28, 2025
1 parent 9b5dbcb commit c1eb014
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Benzina/ProjectsPumpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
trait ProjectsPumpTrait
{
/**
* Cleans the project location field to obtain highly cacheable and improved search queries.
* Normalizes flexible address strings to obtain highly cacheable and improved search queries.
* Based on analysis of the Goteo v3 `project.project_location` values.
*
* @param int $detailLevel Desired number of remaining components in output address
Expand Down Expand Up @@ -39,7 +39,7 @@ public static function cleanProjectLocation(string $location, int $detailLevel =
// e.g: "Universidad Carlos III de Madrid: Campus de Getafe, Calle Madrid, Getafe, España" -> "Campus de Getafe, Calle Madrid, Getafe, España"
$location = \preg_replace('/^[\w ]+:/', '', $location);

// Clean non desired location pieces
// Process comma-separated address pieces
$location = \explode(',', $location);
$location = \array_map(function ($l) {
$l = trim($l);
Expand All @@ -53,23 +53,24 @@ public static function cleanProjectLocation(string $location, int $detailLevel =

return $l;
}, $location);

// Clean non desired location pieces
$location = \array_filter($location, function ($l) {
if (empty($l)) {
return false;
}

// Skip numeric only pieces: coordinates, street numbers, etc
if (\preg_match('/^[-\d.]*$/', $l)) {
return false;
}
if (\str_contains($l, 'º')) {
if (\preg_match('/^[-\d.]*$/', $l) || \str_contains($l, 'º')) {
return false;
}

return true;
});

$location = \join(', ', \array_slice($location, -1 * $detailLevel));

// Trim remaining numbers and punctuation marks
$location = \preg_replace('/^[\d\.\-;]+/', '', $location);
$location = \preg_replace('/[\d\.\-;]+$/', '', $location);

Expand Down

0 comments on commit c1eb014

Please sign in to comment.