|
4 | 4 |
|
5 | 5 | class PrettyVarExport |
6 | 6 | { |
7 | | - // code from https://gist.github.com/lithrel/a224edb1ed2975992c73 |
8 | | - public function call($var, array $opts = []) |
| 7 | + public function call($translations) |
9 | 8 | { |
10 | | - $opts = array_merge(['indent' => '', 'tab' => ' ', 'array-align' => false], $opts); |
11 | | - switch (gettype($var)) { |
12 | | - case 'array': |
13 | | - $r = []; |
14 | | - $indexed = array_keys($var) === range(0, count($var) - 1); |
15 | | - $lengths = array_map('strlen', array_map('trim', array_keys($var))); |
16 | | - $maxLength = ($opts['array-align'] && count($lengths) > 0) ? max($lengths) + 2 : 0; |
17 | | - foreach ($var as $key => $value) { |
18 | | - $key = str_replace("'' . \"\\0\" . '*' . \"\\0\" . ", "", $this->call($key)); |
19 | | - $r[] = $opts['indent'] . $opts['tab'] |
20 | | - . ($indexed ? '' : str_pad($key, $maxLength) . ' => ') |
21 | | - . $this->call($value, array_merge($opts, ['indent' => $opts['indent'] . $opts['tab']])); |
22 | | - } |
23 | | - return "[\n" . implode(",\n", $r) . "\n" . $opts['indent'] . "]"; |
24 | | - case 'boolean': |
25 | | - return $var ? 'true' : 'false'; |
26 | | - case 'NULL': |
27 | | - return 'null'; |
28 | | - default: |
29 | | - return var_export($var, true); |
| 9 | + $content = "["; |
| 10 | + |
| 11 | + $content .= $this->stringLineMaker($translations); |
| 12 | + |
| 13 | + $content .= "\n]"; |
| 14 | + |
| 15 | + return $content; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Write the lines of the inner array of the language file. |
| 20 | + * |
| 21 | + * @param $array |
| 22 | + * @return string |
| 23 | + */ |
| 24 | + private function stringLineMaker($array, $prepend = '') |
| 25 | + { |
| 26 | + $output = ''; |
| 27 | + |
| 28 | + foreach ($array as $key => $value) { |
| 29 | + if (is_array($value)) { |
| 30 | + $value = $this->stringLineMaker($value, $prepend.' '); |
| 31 | + |
| 32 | + $output .= "\n{$prepend} '{$key}' => [{$value}\n{$prepend} ],"; |
| 33 | + } else { |
| 34 | + $value = str_replace('\"', '"', addslashes($value)); |
| 35 | + |
| 36 | + $output .= "\n{$prepend} '{$key}' => '{$value}',"; |
| 37 | + } |
30 | 38 | } |
| 39 | + |
| 40 | + return $output; |
31 | 41 | } |
32 | 42 | } |
0 commit comments