Skip to content

Commit

Permalink
Fix cities.json generation - Remove canton from cities' names
Browse files Browse the repository at this point in the history
  • Loading branch information
kira0269 committed Jun 21, 2024
1 parent a8b2518 commit d4b5aff
Show file tree
Hide file tree
Showing 3 changed files with 623 additions and 484 deletions.
10 changes: 10 additions & 0 deletions src/Console/UpdateZipcodeDatasetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Wnx\SwissCantons\Canton;
use Wnx\SwissCantons\Cantons;
use ZipArchive;

class UpdateZipcodeDatasetCommand extends Command
Expand Down Expand Up @@ -129,6 +131,8 @@ protected function generateZipcodesFiles(TabularDataReader $records): void
{
$data = [];

$cantonsAbbreviations = array_map(fn (Canton $canton) => $canton->getAbbreviation(), (new Cantons())->getAll());

foreach ($records as $zipcodeRecord) {
$city = $zipcodeRecord['Ortschaftsname'];
$zipcode = (int)$zipcodeRecord['PLZ'];
Expand All @@ -138,6 +142,12 @@ protected function generateZipcodesFiles(TabularDataReader $records): void
continue;
}

if (preg_match('/ ([A-Z]{2})$/', $city, $matches)) {
if (in_array($matches[1], $cantonsAbbreviations)) {
$city = substr($city, 0, -3);
}
}

$data[] = [
'city' => $city,
'zipcode' => $zipcode,
Expand Down
Loading

0 comments on commit d4b5aff

Please sign in to comment.