forked from mledoze/countries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countries.php
229 lines (199 loc) · 4.88 KB
/
countries.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* Tools to convert countries in different formats
* @author mledoze
* @see https://github.com/mledoze/countries
*/
/**
* Interface Converter
*/
interface Converter {
/**
* Convert into a new format
* @return string
*/
public function convert();
/**
* Save the converted data to the disk
* @return mixed
*/
public function save();
}
/**
* Class AbstractConverter
*/
abstract class AbstractConverter implements Converter {
/** @var array */
protected $aCountries;
/**
* @var string path of the output directory
*/
private $sOutputDirectory;
/** @var array defines the fields to keep */
private $aFields;
/**
* @param array $aCountries
*/
public function __construct(array $aCountries) {
$this->aCountries = $aCountries;
}
/**
* Save the data to disk
* @param string $sOutputFile name of the output file
* @return int|bool
*/
public function save($sOutputFile = '') {
if (empty($this->sOutputDirectory)) {
$this->setDefaultOutputDirectory();
}
if (!is_dir($this->sOutputDirectory)) {
mkdir($this->sOutputDirectory);
}
if (empty($sOutputFile)) {
$sTempFile = date('Ymd-His', time()) . '-countries';
$sOutputFile = $sTempFile;
}
if (!empty($this->aFields)) {
foreach ($this->aCountries as &$aCountry) {
foreach ($aCountry as $iKey => $value) {
if (!in_array($iKey, $this->aFields)) {
unset($aCountry[$iKey]);
}
}
}
}
return file_put_contents($this->sOutputDirectory . $sOutputFile, $this->convert());
}
/**
* Defines the fields to keep
* @param array $aFields
*/
public function setFields(array $aFields) {
$this->aFields = $aFields;
}
/**
* Converts arrays to comma-separated strings
* @param array $aInput
* @param string $sGlue
* @return array
*/
protected function convertArrays(array &$aInput, $sGlue = ',') {
$aInput = array_map(function ($value) use ($sGlue) {
if (is_array($value)) {
return implode($sGlue, $value);
}
return $value;
}, $aInput);
return $aInput;
}
/**
* Set the default output directory
*/
private function setDefaultOutputDirectory() {
$this->sOutputDirectory = __DIR__ . DIRECTORY_SEPARATOR . 'dist' . DIRECTORY_SEPARATOR;
}
}
/**
* Class JsonConverter
*/
class JsonConverter extends AbstractConverter {
/**
* @return string minified JSON, one country per line
*/
public function convert() {
return preg_replace("@},{@", "}," . PHP_EOL . "{", json_encode($this->aCountries) . PHP_EOL);
}
}
/**
* Class CsvConverter
*/
class CsvConverter extends AbstractConverter {
/**
* @var
*/
private $sGlue = '";"';
/**
* @var string
*/
private $sBody = '';
/**
* @return string data converted into CSV
*/
public function convert() {
array_walk($this->aCountries, array($this, 'processCountry'));
$sHeaders = '"' . implode($this->sGlue, array_keys($this->aCountries[0])) . '"';
return $sHeaders . PHP_EOL . $this->sBody;
}
/**
* @return string
*/
public function getGlue() {
return $this->sGlue;
}
/**
* @param string $sGlue
*/
public function setGlue($sGlue) {
$this->sGlue = $sGlue;
}
/**
* Processes a country.
* @param $array
*/
private function processCountry(&$array) {
$this->sBody .= '"' . implode($this->sGlue, $this->convertArrays($array)) . '"' . PHP_EOL;
}
}
/**
* Class XmlConverter
*/
class XmlConverter extends AbstractConverter {
/** @var DOMDocument $oDom */
private $oDom;
/**
* @param array $aCountries
*/
public function __construct(array $aCountries) {
$this->oDom = new DOMDocument('1.0', 'UTF-8');
$this->formatOutput();
$this->preserveWhiteSpace();
$this->oDom->appendChild($this->oDom->createElement('countries'));
parent::__construct($aCountries);
}
/**
* @return string data converted into XML
*/
public function convert() {
array_walk($this->aCountries, array($this, 'processCountry'));
return $this->oDom->saveXML();
}
/**
* @param bool $bFormatOutput
* @see \DOMDocument::$formatOutput
*/
public function formatOutput($bFormatOutput = true) {
$this->oDom->formatOutput = $bFormatOutput;
}
/**
* @param bool $bPreserveWhiteSpace
* @see \DOMDocument::$preserveWhiteSpace
*/
public function preserveWhiteSpace($bPreserveWhiteSpace = false) {
$this->oDom->preserveWhiteSpace = $bPreserveWhiteSpace;
}
/**
* @param $array
*/
private function processCountry(&$array) {
$oCountryNode = $this->oDom->createElement('country');
$array = $this->convertArrays($array);
array_walk($array, function ($value, $key) use ($oCountryNode) {
$oCountryNode->setAttribute($key, $value);
});
$this->oDom->documentElement->appendChild($oCountryNode);
}
}
$aCountriesSrc = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'countries.json'), true);
(new JsonConverter($aCountriesSrc))->save('countries.json');
(new CsvConverter($aCountriesSrc))->save('countries.csv');
(new XmlConverter($aCountriesSrc))->save('countries.xml');