Skip to content

Commit

Permalink
Compact output for ranges.php
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Aug 4, 2018
1 parent 5053fc6 commit bd03802
Show file tree
Hide file tree
Showing 2 changed files with 2,893 additions and 9,171 deletions.
69 changes: 68 additions & 1 deletion convertRangeMessage.php
Expand Up @@ -51,5 +51,72 @@
$groupCount++;
}

file_put_contents('data/ranges.php', '<?php return ' . var_export($rangeData, true) . ';');
/**
* Compact & pretty alternative to var_export().
*
* @param mixed $variable
* @param int $indent
*
* @return string
*/
function export($variable, int $indent = 0)
{
if (! is_array($variable)) {
return var_export($variable, true);
}

$isNumeric = (array_values($variable) === $variable);

$hasArray = false;

foreach ($variable as $value) {
if (is_array($value)) {
$hasArray = true;
break;
}
}

$spaces = str_repeat("\t", $indent + 1);

$result = '[';

if ($hasArray) {
$result .= "\n";
}

$count = count($variable);
$current = 0;

foreach ($variable as $key => $value) {
if ($hasArray) {
$result .= $spaces;
}

if (! $isNumeric) {
$result .= export($key) . ' => ';
}

$result .= export($value, $indent + 1);

if (++$current !== $count) {
$result .= ',';
}

if ($hasArray) {
$result .= "\n";
} elseif ($current !== $count) {
$result .= ' ';
}
}

if ($hasArray) {
$result .= str_repeat("\t", $indent);
}

$result .= ']';

return $result;
}

file_put_contents('data/ranges.php', '<?php return ' . export($rangeData) . ";\n");
printf('Successfully converted %d groups and %d ranges.' . PHP_EOL, $groupCount, $rangeCount);

0 comments on commit bd03802

Please sign in to comment.