Skip to content

Commit

Permalink
Rewrite PhpFormatter::format method, to avoid add extra spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-martinez-interactiv4 committed Oct 14, 2017
1 parent cb93fe5 commit c23f4dc
Showing 1 changed file with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,48 @@ class PhpFormatter implements FormatterInterface
public function format($data, array $comments = [])
{
if (!empty($comments) && is_array($data)) {
$elements = [];
return "<?php\nreturn array (\n" . $this->formatData($data, $comments, ' ') . "\n);\n";
}
return "<?php\nreturn " . var_export($data, true) . ";\n";
}

/**
* Format supplied data
*
* @param $data
* @param $comments
* @param string $prefix
* @return string
*/
protected function formatData($data, $comments, $prefix = '')
{
$elements = [];

if (is_array($data)) {
foreach ($data as $key => $value) {
$comment = ' ';
if (!empty($comments[$key])) {
$section = " * For the section: " . $key . "\n";
$exportedComment = is_string($comments[$key])
? $comments[$key]
: var_export($comments[$key], true);
$comment = " /**\n" . $section . " * " . str_replace("\n", "\n * ", $exportedComment) . "\n */\n";
$elements[] = $prefix . '/**';
$elements[] = $prefix . ' * For the section: ' . $key;

foreach (explode("\n", $comments[$key]) as $commentLine) {
$elements[] = $prefix . ' * ' . $commentLine;
}

$elements[] = $prefix . " */";
}

if (is_array($value)) {
$elements[] = $prefix . var_export($key, true) . ' => ';
$elements[] = $prefix . 'array (';
$elements[] = $this->formatData($value, [], ' ' . $prefix);
$elements[] = $prefix . '),';
} else {
$elements[] = $prefix . var_export($key, true) . ' => ' . var_export($value, true) . ',';
}
$space = is_array($value) ? " \n" : ' ';
$elements[] = $comment . var_export($key, true) . ' =>' . $space . var_export($value, true);
}
return "<?php\nreturn array (\n" . implode(",\n", str_replace("\n", "\n ", $elements)) . "\n);\n";
return implode("\n", $elements);
}
return "<?php\nreturn " . var_export($data, true) . ";\n";

return var_export($data, true);
}
}

0 comments on commit c23f4dc

Please sign in to comment.