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
|
<?php
declare(strict_types=1);
namespace libphonenumber\buildtools;
use Nette\PhpGenerator\PhpFile;
use Nette\PhpGenerator\PsrPrinter;
use RuntimeException;
use function explode;
use function file;
use function file_put_contents;
use function is_readable;
use function str_replace;
use function strpos;
use function trim;
/**
* Class GenerateTimeZonesMapData
* @package libphonenumber\buildtools
* @internal
*/
class GenerateTimeZonesMapData
{
public const GENERATION_COMMENT = <<<'EOT'
/**
* libphonenumber-for-php data file
* This file has been @generated from libphonenumber data
* Do not modify!
* @internal
*/
EOT;
private string $inputTextFile;
public function __construct(string $inputFile, string $outputDir, string $outputNamespace)
{
$this->inputTextFile = $inputFile;
if (!is_readable($this->inputTextFile)) {
throw new RuntimeException('The provided input text file does not exist.');
}
$data = $this->parseTextFile();
$this->writeMappingFile($outputDir, $outputNamespace, $data);
}
/**
* Reads phone prefix data from the provided input stream and returns a SortedMap with the
* prefix to time zones mappings.
* @return array<string,string>
*/
private function parseTextFile(): array
{
$data = file($this->inputTextFile);
if ($data === false) {
throw new RuntimeException('The provided input text file could not be read.');
}
$timeZoneMap = [];
foreach ($data as $line) {
// Remove \n
$line = str_replace(["\n", "\r"], '', $line);
$line = trim($line);
if ($line === '' || str_starts_with($line, '#')) {
continue;
}
if (strpos($line, '|') > 0) {
// Valid line
$parts = explode('|', $line);
[$prefix, $timezone] = $parts;
$timeZoneMap[$prefix] = $timezone;
}
}
return $timeZoneMap;
}
/**
* @param array<string,string> $data
*/
private function writeMappingFile(string $outputFile, string $outputNamespace, array $data): void
{
$mappingClass = 'Map';
$file = new PhpFile();
$file->setStrictTypes();
$file->addComment(self::GENERATION_COMMENT);
$namespace = $file->addNamespace($outputNamespace);
$class = $namespace->addClass($mappingClass);
$class->addComment('@internal');
$constant = $class->addConstant('DATA', $data);
$constant->setPublic();
$printer = new PsrPrinter();
$outputPath = $outputFile . DIRECTORY_SEPARATOR . $mappingClass . '.php';
file_put_contents($outputPath, $printer->printFile($file));
}
}
|