mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 06:45:44 +01:00
This commit introduces REUSE compliance by annotating all files with SPDX information and placing the reused licences in the LICENSES folder. We additionally removed the docheader tool which is made obsolete by this change. The main LICENSE and copyright text of the project is now not under my personal name anymore, and it belongs to "The Respect Project Contributors" instead. This change restores author names to several files, giving the appropriate attribution for contributions.
133 lines
3.9 KiB
PHP
133 lines
3.9 KiB
PHP
<?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 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 Symfony\Component\VarExporter\VarExporter;
|
|
|
|
use function basename;
|
|
use function count;
|
|
use function dirname;
|
|
use function explode;
|
|
use function file_get_contents;
|
|
use function file_put_contents;
|
|
use function implode;
|
|
use function ksort;
|
|
use function preg_replace;
|
|
use function preg_replace_callback;
|
|
use function sprintf;
|
|
use function str_contains;
|
|
use function str_starts_with;
|
|
use function strlen;
|
|
use function trim;
|
|
|
|
use const PHP_EOL;
|
|
|
|
#[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';
|
|
|
|
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 . '/'];
|
|
}
|
|
|
|
ksort($postalCodes);
|
|
|
|
// Create the data file
|
|
$dataFilename = dirname(__DIR__, 2) . '/data/postal-code.php';
|
|
|
|
$SPDX = '// SPDX';
|
|
|
|
$fileContent = implode(PHP_EOL, [
|
|
'<?php declare(strict_types=1);',
|
|
$SPDX . '-FileCopyrightText: (c) https://download.geonames.org/export/dump/countryInfo.txt',
|
|
$SPDX . '-License-Identifier: CC-BY-4.0',
|
|
'return ' . preg_replace('/\\\([dws])/', '\\1', VarExporter::export($postalCodes)) . ';' . PHP_EOL,
|
|
]);
|
|
|
|
// Write the data file
|
|
if (file_put_contents($dataFilename, $fileContent) === false) {
|
|
$io->error('Failed to write data file');
|
|
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$io->success(sprintf('Updated %s successfully', basename($dataFilename)));
|
|
$io->text(sprintf('Total postal codes: %d', count($postalCodes)));
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|