|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace libphonenumber\buildtools; |
|
4
|
|
|
|
|
5
|
|
|
use libphonenumber\PhoneNumberToTimeZonesMapper; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class GenerateTimeZonesMapData |
|
9
|
|
|
* @package libphonenumber\buildtools |
|
10
|
|
|
* @internal |
|
11
|
|
|
*/ |
|
12
|
|
|
class GenerateTimeZonesMapData |
|
13
|
|
|
{ |
|
14
|
|
|
const GENERATION_COMMENT = <<<'EOT' |
|
15
|
|
|
/** |
|
16
|
|
|
* This file has been @generated by a phing task by {@link GenerateTimeZonesMapData}. |
|
17
|
|
|
* See [README.md](README.md#generating-data) for more information. |
|
18
|
|
|
* |
|
19
|
|
|
* Pull requests changing data in these files will not be accepted. See the |
|
20
|
|
|
* [FAQ in the README](README.md#problems-with-invalid-numbers] on how to make |
|
21
|
|
|
* metadata changes. |
|
22
|
|
|
* |
|
23
|
|
|
* Do not modify this file directly! |
|
24
|
|
|
|
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
EOT; |
|
29
|
|
|
private $inputTextFile; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($inputFile, $outputDir) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->inputTextFile = $inputFile; |
|
34
|
|
|
|
|
35
|
|
|
if (!\is_readable($this->inputTextFile)) { |
|
36
|
|
|
throw new \RuntimeException('The provided input text file does not exist.'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$data = $this->parseTextFile(); |
|
40
|
|
|
$this->writeMappingFile($outputDir, $data); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Reads phone prefix data from the provided input stream and returns a SortedMap with the |
|
45
|
|
|
* prefix to time zones mappings. |
|
46
|
|
|
*/ |
|
47
|
|
|
private function parseTextFile() |
|
48
|
|
|
{ |
|
49
|
|
|
$data = \file($this->inputTextFile); |
|
50
|
|
|
|
|
51
|
|
|
$timeZoneMap = array(); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($data as $line) { |
|
54
|
|
|
// Remove \n |
|
55
|
|
|
$line = \str_replace(array("\n", "\r"), '', $line); |
|
56
|
|
|
$line = \trim($line); |
|
57
|
|
|
|
|
58
|
|
|
if (\strlen($line) == 0 || \substr($line, 0, 1) == '#') { |
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
if (\strpos($line, '|')) { |
|
62
|
|
|
// Valid line |
|
63
|
|
|
$parts = \explode('|', $line); |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$prefix = $parts[0]; |
|
67
|
|
|
$timezone = $parts[1]; |
|
68
|
|
|
|
|
69
|
|
|
$timeZoneMap[$prefix] = $timezone; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $timeZoneMap; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function writeMappingFile($outputFile, $data) |
|
77
|
|
|
{ |
|
78
|
|
|
$phpSource = '<?php' . PHP_EOL |
|
79
|
|
|
. self::GENERATION_COMMENT |
|
80
|
|
|
. 'return ' . \var_export($data, true) . ';' |
|
81
|
|
|
. PHP_EOL; |
|
82
|
|
|
|
|
83
|
|
|
$outputPath = $outputFile . DIRECTORY_SEPARATOR . PhoneNumberToTimeZonesMapper::MAPPING_DATA_FILE_NAME; |
|
84
|
|
|
|
|
85
|
|
|
\file_put_contents($outputPath, $phpSource); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|