@@ -21,21 +21,48 @@ class PhpFormatter implements FormatterInterface
21
21
public function format ($ data , array $ comments = [])
22
22
{
23
23
if (!empty ($ comments ) && is_array ($ data )) {
24
- $ elements = [];
24
+ return "<?php \nreturn array ( \n" . $ this ->formatData ($ data , $ comments , ' ' ) . "\n); \n" ;
25
+ }
26
+ return "<?php \nreturn " . var_export ($ data , true ) . "; \n" ;
27
+ }
28
+
29
+ /**
30
+ * Format supplied data
31
+ *
32
+ * @param $data
33
+ * @param $comments
34
+ * @param string $prefix
35
+ * @return string
36
+ */
37
+ protected function formatData ($ data , $ comments , $ prefix = '' )
38
+ {
39
+ $ elements = [];
40
+
41
+ if (is_array ($ data )) {
25
42
foreach ($ data as $ key => $ value ) {
26
- $ comment = ' ' ;
27
43
if (!empty ($ comments [$ key ])) {
28
- $ section = " * For the section: " . $ key . "\n" ;
29
- $ exportedComment = is_string ($ comments [$ key ])
30
- ? $ comments [$ key ]
31
- : var_export ($ comments [$ key ], true );
32
- $ comment = " /** \n" . $ section . " * " . str_replace ("\n" , "\n * " , $ exportedComment ) . "\n */ \n" ;
44
+ $ elements [] = $ prefix . '/** ' ;
45
+ $ elements [] = $ prefix . ' * For the section: ' . $ key ;
46
+
47
+ foreach (explode ("\n" , $ comments [$ key ]) as $ commentLine ) {
48
+ $ elements [] = $ prefix . ' * ' . $ commentLine ;
49
+ }
50
+
51
+ $ elements [] = $ prefix . " */ " ;
52
+ }
53
+
54
+ $ elements [] = $ prefix . var_export ($ key , true ) . ' => ' .
55
+ (!is_array ($ value ) ? var_export ($ value , true ) . ', ' : '' );
56
+
57
+ if (is_array ($ value )) {
58
+ $ elements [] = $ prefix . 'array ( ' ;
59
+ $ elements [] = $ this ->formatData ($ value , [], ' ' . $ prefix );
60
+ $ elements [] = $ prefix . '), ' ;
33
61
}
34
- $ space = is_array ($ value ) ? " \n" : ' ' ;
35
- $ elements [] = $ comment . var_export ($ key , true ) . ' => ' . $ space . var_export ($ value , true );
36
62
}
37
- return " <?php \n return array ( \n" . implode (", \n" , str_replace ( "\n" , "\n " , $ elements )) . "\n ); \n" ;
63
+ return implode ("\n" , $ elements );
38
64
}
39
- return "<?php \nreturn " . var_export ($ data , true ) . "; \n" ;
65
+
66
+ return var_export ($ data , true );
40
67
}
41
68
}
0 commit comments