forked from Respect/Validation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdatePostalCodesCommand.php
More file actions
117 lines (91 loc) · 3.28 KB
/
UpdatePostalCodesCommand.php
File metadata and controls
117 lines (91 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Dev\Commands;
use Respect\Dev\Helpers\DataSaver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function count;
use function explode;
use function file_get_contents;
use function preg_replace_callback;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function strlen;
use function trim;
#[AsCommand(
name: 'update:postal-codes',
description: 'Update the list of postal codes in the PostalCode validator',
)]
final class UpdatePostalCodesCommand extends Command
{
private const string LIST_URL = 'https://download.geonames.org/export/dump/countryInfo.txt';
public function __construct(
private readonly DataSaver $dataSaver,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Updating postal codes list');
// Download the list
$io->section('Downloading list');
$io->text(sprintf('Fetching from: %s', self::LIST_URL));
$listContent = file_get_contents(self::LIST_URL);
if ($listContent === false) {
$io->error('Failed to download postal codes list');
return Command::FAILURE;
}
$io->success('Downloaded successfully');
// Process the list
$io->section('Processing postal codes');
$lines = explode("\n", $listContent);
$postalCodes = [];
foreach ($lines as $line) {
$line = trim($line);
// Skip comments and empty lines
if ($line === '' || str_starts_with($line, '#')) {
continue;
}
// Split by tab
$parts = explode("\t", $line);
if (count($parts) < 15) {
continue;
}
$countryCode = $parts[0];
$countryFormat = $parts[13];
$countryRegex = trim($parts[14]);
if ($countryRegex === '') {
continue;
}
// Convert format
$countryFormat = preg_replace_callback('/(#+|@+)/', static function ($matches) {
$length = strlen($matches[0]);
$regex = str_contains($matches[0], '#') ? '\d' : '\w';
if ($length > 1) {
$regex .= '{' . $length . '}';
}
return $regex;
}, $countryFormat);
$postalCodes[$countryCode] = ['/^' . $countryFormat . '$/', '/' . $countryRegex . '/'];
}
$this->dataSaver->save(
$postalCodes,
'(c) https://download.geonames.org/export/dump/countryInfo.txt',
'CC-BY-4.0',
'postal-code.php',
);
$io->success('Updated successfully');
$io->text(sprintf('Total postal codes: %d', count($postalCodes)));
return Command::SUCCESS;
}
}