Created
October 24, 2016 21:07
-
-
Save beatak/fc9bcf901a3cc1769f7f37d94d65d538 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Address_Formatter_DataGenerator { | |
const SHORT_OPTS = ['a', 'p', 'c', 'j', 'h', 'x', 'g',]; | |
public static function getShortOpts() { | |
return implode('', self::SHORT_OPTS); | |
} | |
public static function processArguments() { | |
// echo 'short opts: ' . Address_Formatter_DataGenerator::getShortOpts() . "\n"; | |
$options = getopt(Address_Formatter_DataGenerator::getShortOpts()); | |
echo 'options: ' . print_r($options, true) . "\n"; | |
$generate_all = isset($options['a']); | |
$generate_php = (isset($options['p']) || $generate_all); | |
$generate_csv = (isset($options['c']) || $generate_all); | |
$generate_js = (isset($options['j']) || $generate_all); | |
$download_raw = isset($options['x']); | |
$download_from_google = isset($options['g']); | |
return [ | |
'generate_all' => $generate_all, | |
'generate_php' => $generate_php, | |
'generate_csv' => $generate_csv, | |
'generate_js' => $generate_js, | |
'download_raw' => $download_raw, | |
'download_google' => $download_from_google, | |
'show_help' => (isset($options['h']) | |
|| !($generate_all || $generate_php || $generate_js || $generate_csv || $download_raw || $download_from_google)), | |
]; | |
} | |
public static function showHelp($script_path) { | |
return <<<TXT | |
Usage: $script_path [options] | |
Options: | |
-p If specified, output an updated version of phplib/Address/formatter/Data.php. | |
-c If specified, output a csv version of the address format data. | |
-j If specified, output json files for each country containing their respective | |
formatting data. | |
-a If specified, generate all data types (csv, php, json). | |
-h Show this message. | |
-x If specified, we'll re-download the data from github instead of using the | |
cached data to generate the various formats. | |
-g If specified, we'll re-download the data from google. | |
You almost certainly want to download data from github instead. | |
TXT; | |
} | |
const DEFAULT_INDENT = 4; | |
public static function serializeToPHPClass(string $className, string $dataName, array $dataArray, $spaceCount = null) { | |
$result = []; | |
if (is_null($spaceCount)) { | |
$spaceCount = self::DEFAULT_INDENT; | |
} | |
array_push($result, "class $className {"); | |
array_push($result, str_repeat(' ', $spaceCount) . "static $dataName = ["); | |
self::walkForPHP($result, $dataArray, $spaceCount, $depth = 2); | |
array_push($result, str_repeat(' ', $spaceCount) . '];'); | |
array_push($result, '}'); | |
return implode("\n", $result); | |
} | |
private static function isArraySequence(array $arr) { | |
if (array() === $arr) { | |
return true; | |
} | |
return array_keys($arr) === range(0, count($arr) - 1); | |
} | |
private static function walkForPHP(array &$resultArray, array $dataArray, int $spaceCount, int $depth) { | |
$mySpaceCount = $spaceCount * $depth; | |
foreach($dataArray as $k => $v) { | |
if (is_numeric($k)) { | |
// PHP turns the array key to `integer` if it looks like it | |
// otherwise the key is `string`, so the check above is | |
// `is_numeric`, instead of `is_int` | |
$key = $k; | |
} else { | |
$key = "'" . $k . "'"; | |
} | |
if (is_array($v)) { | |
if (self::isArraySequence($v)) { | |
$value = []; | |
foreach($v as $_v) { | |
if (is_int($_v)) { | |
array_push($value, $_v); | |
} else { | |
array_push($value, "'$_v'"); | |
} | |
} | |
array_push( | |
$resultArray, | |
str_repeat(' ', $mySpaceCount) | |
. $key . ' => [' . implode(', ', $value) . '],' | |
); | |
} else { | |
array_push( | |
$resultArray, | |
str_repeat(' ', $mySpaceCount) . $key . ' => [' | |
); | |
self::serializeClassWalker($resultArray, $v, $spaceCount, $depth + 1); | |
array_push( | |
$resultArray, | |
str_repeat(' ', $mySpaceCount) . '],' | |
); | |
} | |
} else { | |
if (is_int($v)) { | |
$value = $v; | |
} else { | |
$value = "'" . $v . "'"; | |
} | |
array_push( | |
$resultArray, | |
str_repeat(' ', $mySpaceCount) . $key . ' => ' . $value . ',' | |
); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment