Skip to content

Commit

Permalink
Dumped array made more readable.
Browse files Browse the repository at this point in the history
  • Loading branch information
hpolthof committed Sep 15, 2015
1 parent 7bab972 commit 7ccd93d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Console/Commands/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ protected function getFileTemplate($content, $locale, $group, $date)
$content = $this->convertToOrderedNestedArray($content);

$array_text = var_export($content, true);
$array_text = $this->getArrayText($content);


$data = <<<EOF
<?php
// Generated by Translations Manager - Hpolthof\Translation
Expand All @@ -134,6 +137,33 @@ protected function getFileTemplate($content, $locale, $group, $date)
return $data;
}

private function getArrayText($arr, $ident_size = 4, $level = 0)
{
$ident = str_repeat(' ', $ident_size*$level);
$result = "array(\n";

$max_key_length = $this->getMaxKeyLength($arr);

foreach($arr as $key => $value) {

$filler = str_repeat(' ', $max_key_length - strlen($key));

$result .= $ident.str_repeat(' ', $ident_size)."'{$key}'{$filler} => ";

if(is_array($value)) {
$result .= $this->getArrayText($value, $ident_size, $level+1);
} else {
$result .= var_export($value, true);
}
$result .= ",\n";
}
$result .= $ident.")";

//if($level > 0) $result .= ",\n";

return $result;
}

/**
* @param $content
* @return mixed
Expand Down Expand Up @@ -162,4 +192,18 @@ private function convertToOrderedNestedArray($content)
return $content;
}

/**
* @param $arr
* @return int
*/
private function getMaxKeyLength($arr)
{
$keys = array_keys($arr);
usort($keys, function ($a, $b) {
return strlen($a) < strlen($b);
});
$max_key_length = strlen(array_shift($keys));
return $max_key_length;
}

}

0 comments on commit 7ccd93d

Please sign in to comment.