1010
1111namespace Respect \Dev \Commands ;
1212
13+ use Respect \Dev \Helpers \DataSaver ;
1314use Symfony \Component \Console \Attribute \AsCommand ;
1415use Symfony \Component \Console \Command \Command ;
1516use Symfony \Component \Console \Input \InputInterface ;
1617use Symfony \Component \Console \Output \OutputInterface ;
1718use Symfony \Component \Console \Style \SymfonyStyle ;
18- use Symfony \Component \VarExporter \VarExporter ;
1919
20- use function basename ;
2120use function count ;
22- use function dirname ;
2321use function explode ;
2422use function file_get_contents ;
25- use function file_put_contents ;
26- use function implode ;
27- use function ksort ;
28- use function preg_replace ;
2923use function preg_replace_callback ;
3024use function sprintf ;
3125use function str_contains ;
3226use function str_starts_with ;
3327use function strlen ;
3428use function trim ;
3529
36- use const PHP_EOL ;
37-
3830#[AsCommand(
3931 name: 'update:postal-codes ' ,
4032 description: 'Update the list of postal codes in the PostalCode validator ' ,
@@ -43,6 +35,12 @@ final class UpdatePostalCodesCommand extends Command
4335{
4436 private const string LIST_URL = 'https://download.geonames.org/export/dump/countryInfo.txt ' ;
4537
38+ public function __construct (
39+ private readonly DataSaver $ dataSaver ,
40+ ) {
41+ parent ::__construct ();
42+ }
43+
4644 protected function execute (InputInterface $ input , OutputInterface $ output ): int
4745 {
4846 $ io = new SymfonyStyle ($ input , $ output );
@@ -104,28 +102,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
104102 $ postalCodes [$ countryCode ] = ['/^ ' . $ countryFormat . '$/ ' , '/ ' . $ countryRegex . '/ ' ];
105103 }
106104
107- ksort ($ postalCodes );
108-
109- // Create the data file
110- $ dataFilename = dirname (__DIR__ , 2 ) . '/data/postal-code.php ' ;
111-
112- $ SPDX = '// SPDX ' ;
113-
114- $ fileContent = implode (PHP_EOL , [
115- '<?php declare(strict_types=1); ' ,
116- $ SPDX . '-FileCopyrightText: (c) https://download.geonames.org/export/dump/countryInfo.txt ' ,
117- $ SPDX . '-License-Identifier: CC-BY-4.0 ' ,
118- 'return ' . preg_replace ('/ \\\([dws])/ ' , '\\1 ' , VarExporter::export ($ postalCodes )) . '; ' . PHP_EOL ,
119- ]);
120-
121- // Write the data file
122- if (file_put_contents ($ dataFilename , $ fileContent ) === false ) {
123- $ io ->error ('Failed to write data file ' );
124-
125- return Command::FAILURE ;
126- }
105+ $ this ->dataSaver ->save (
106+ $ postalCodes ,
107+ '(c) https://download.geonames.org/export/dump/countryInfo.txt ' ,
108+ 'CC-BY-4.0 ' ,
109+ 'postal-code.php ' ,
110+ );
127111
128- $ io ->success (sprintf ( 'Updated %s successfully ' , basename ( $ dataFilename )) );
112+ $ io ->success ('Updated successfully ' );
129113 $ io ->text (sprintf ('Total postal codes: %d ' , count ($ postalCodes )));
130114
131115 return Command::SUCCESS ;
0 commit comments