18 public function parse( $inputFile, $outputFile ) {
21 $contents = file_get_contents( $inputFile );
22 $doc =
new SimpleXMLElement( $contents );
25 'languageNames' => [],
26 'currencyNames' => [],
27 'currencySymbols' => [],
32 foreach ( $doc->xpath(
'//languages/language' ) as $elem ) {
33 if ( (
string)$elem[
'alt'] !==
'' ) {
37 if ( (
string)$elem[
'type'] ===
'root' ) {
41 $key = str_replace(
'_',
'-', strtolower( $elem[
'type'] ) );
43 $data[
'languageNames'][$key] = (string)$elem;
46 foreach ( $doc->xpath(
'//currencies/currency' ) as $elem ) {
47 if ( (
string)$elem->displayName[0] ===
'' ) {
51 $data[
'currencyNames'][(string)$elem[
'type']] = (
string)$elem->displayName[0];
52 if ( (
string)$elem->symbol[0] !==
'' ) {
53 $data[
'currencySymbols'][(string)$elem[
'type']] = (
string)$elem->symbol[0];
57 foreach ( $doc->xpath(
'//territories/territory' ) as $elem ) {
58 if ( (
string)$elem[
'alt'] !==
'' && (string)$elem[
'alt'] !==
'short' ) {
62 if ( (
string)$elem[
'type'] ===
'ZZ' ||
63 !preg_match(
'/^[A-Z][A-Z]$/', $elem[
'type'] )
68 $data[
'countryNames'][(string)$elem[
'type']] = (
string)$elem;
70 foreach ( $doc->xpath(
'//units/unitLength' ) as $unitLength ) {
71 if ( (
string)$unitLength[
'type'] !==
'long' ) {
74 foreach ( $unitLength->unit as $elem ) {
75 $type = (string)$elem[
'type'];
76 $pos = strpos(
$type,
'duration' );
77 if ( $pos ===
false ) {
80 $type = substr(
$type, strlen(
'duration-' ) );
81 foreach ( $elem->unitPattern as $pattern ) {
82 $data[
'timeUnits'][
$type .
'-' . (string)$pattern[
'count']] = (
string)$pattern;
86 foreach ( $doc->xpath(
'//fields/field' ) as $field ) {
87 $fieldType = (string)$field[
'type'];
89 foreach ( $field->relativeTime as $relative ) {
90 $type = (string)$relative[
'type'];
91 foreach ( $relative->relativeTimePattern as $pattern ) {
92 $data[
'timeUnits'][$fieldType .
'-' .
$type
93 .
'-' . (string)$pattern[
'count']] = (
string)$pattern;
98 ksort( $data[
'timeUnits'] );
100 $this->savephp( $data, $outputFile );
108 public function parse_supplemental( $inputFile, $outputFile ) {
111 $contents = file_get_contents( $inputFile );
112 $doc =
new SimpleXMLElement( $contents );
115 'currencyFractions' => [],
116 'localeCurrencies' => [],
122 foreach ( $doc->xpath(
'//currencyData/fractions/info' ) as $elem ) {
123 if ( (
string)$elem[
'iso4217'] ===
'' ) {
127 $attributes = [
'digits',
'rounding',
'cashDigits',
'cashRounding' ];
128 foreach ( $attributes as $att ) {
129 if ( (
string)$elem[$att] !==
'' ) {
130 $data[
'currencyFractions'][(string)$elem[
'iso4217']][$att] = (
string)$elem[$att];
136 foreach ( $doc->xpath(
'//currencyData/region' ) as $elem ) {
137 if ( (
string)$elem[
'iso3166'] ===
'' ) {
141 $region = (string)$elem[
'iso3166'];
143 foreach ( $elem->currency as $currencynode ) {
144 if ( (
string)$currencynode[
'to'] ===
'' && (string)$currencynode[
'tender'] !==
'false' ) {
145 $data[
'localeCurrencies'][$region][] = (string)$currencynode[
'iso4217'];
150 $this->savephp( $data, $outputFile );
161 public function parse_currency_symbols( $inputDir, $outputFile ) {
162 if ( !file_exists( $inputDir ) ) {
165 $files = scandir( $inputDir );
168 'currencySymbols' => [],
172 foreach ( $files as $inputFile ) {
173 if ( strpos( $inputFile,
'.xml' ) < 1 ) {
177 $contents = file_get_contents( $inputDir .
'/' . $inputFile );
178 $doc =
new SimpleXMLElement( $contents );
181 $languages = $doc->xpath(
'//identity/language/@type' );
182 $language = $languages
183 ? (string)$languages[0]
184 : pathinfo( $inputFile, PATHINFO_FILENAME );
187 $territories = $doc->xpath(
'//identity/territory/@type' );
188 $territory = $territories ? (string)$territories[0] :
'DEFAULT';
190 foreach ( $doc->xpath(
'//currencies/currency' ) as $elem ) {
191 if ( (
string)$elem->symbol[0] !==
'' ) {
192 $data[
'currencySymbols'][(string)$elem[
'type']][$language][$territory] =
193 (
string)$elem->symbol[0];
211 foreach ( $data[
'currencySymbols'] as $currency => $language ) {
214 $default = $language[
'root'][
'DEFAULT'] ?? $currency;
216 foreach ( $language as
$lang => $territories ) {
218 if ( is_array( $territories ) ) {
219 if ( count( $territories ) === 1 && array_key_exists(
'DEFAULT', $territories ) ) {
220 $data[
'currencySymbols'][$currency][
$lang] = $territories[
'DEFAULT'];
221 if ( $territories[
'DEFAULT'] === $default &&
$lang !==
'root' ) {
222 unset( $data[
'currencySymbols'][$currency][
$lang] );
225 ksort( $data[
'currencySymbols'][$currency][
$lang] );
230 ksort( $data[
'currencySymbols'][$currency] );
233 ksort( $data[
'currencySymbols'] );
235 $this->savephp( $data, $outputFile );
244 protected function savephp( $data, $location ) {
246 foreach ( $data as $v ) {
260 foreach ( $data as $varname => $values ) {
261 if ( !count( $values ) ) {
265 $output .=
"\n\$$varname = [\n";
266 if ( $this->isAssoc( $values ) ) {
267 foreach ( $values as $key => $value ) {
268 if ( is_array( $value ) ) {
269 $output .= $this->makePrettyArrayOuts( $key, $value, 1 );
271 $key = addcslashes( $key,
"'" );
272 $value = addcslashes( $value,
"'" );
273 if ( !is_numeric( $key ) ) {
276 $output .=
"\t$key => '$value',\n";
280 foreach ( $values as $value ) {
281 if ( is_array( $value ) ) {
282 $output .= $this->makePrettyArrayOuts(
null, $value, 1 );
284 $value = addcslashes( $value,
"'" );
285 $output .=
"\t'$value',\n";
292 file_put_contents( $location, $output );
302 protected function makePrettyArrayOuts( $key, $value, $level = 1 ) {
304 $isAssoc = $this->isAssoc( $value );
305 $tabs = str_repeat(
"\t", $level );
307 foreach ( $value as $subkey => $subvalue ) {
308 $subkey = $isAssoc ? $subkey :
null;
310 if ( is_array( $subvalue ) ) {
311 $subKeys .= $this->makePrettyArrayOuts( $subkey, $subvalue, $level + 1 );
313 $subkey = $isAssoc ? $this->formatKey( $subkey ) :
'';
314 $subvalue = addcslashes( $subvalue,
"'" );
315 $subKeys .=
"$tabs\t$subkey'$subvalue',\n";
319 if ( $subKeys ===
'' ) {
323 $key = $key !==
null ? $this->formatKey( $key ) :
'';
324 return "$tabs$key" .
"[\n$subKeys$tabs],\n";
332 protected function formatKey( $key ) {
333 $key = addcslashes( $key,
"'" );
334 if ( !is_numeric( $key ) ) {
347 protected function isAssoc( array $arr ) {
348 return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
if(!isset( $args[0])) $lang