@@ -14,6 +14,21 @@ final class ArrayGenerator extends Generator
1414 */
1515 const PRETTY_INDENT = ' ' ;
1616
17+ /**
18+ * @private
19+ */
20+ const FORMAT_NORMAL = 'normal ' ;
21+
22+ /**
23+ * @private
24+ */
25+ const FORMAT_PRETTY = 'pretty ' ;
26+
27+ /**
28+ * @private
29+ */
30+ const FORMAT_COMPACT = 'compact ' ;
31+
1732 /**
1833 * @var bool
1934 */
@@ -25,34 +40,66 @@ final class ArrayGenerator extends Generator
2540 private $ strictTypes ;
2641
2742 /**
28- * @var bool
43+ * @var string
2944 */
30- private $ pretty ;
45+ private $ format ;
3146
3247 /**
3348 * Constructs a new ArrayGenerator
34- * @param array|null $options
35- *
36- * bool includeEmpty Controls whether empty translations should be included (default: false)
49+ * @param array {
50+ * includeEmpty?: bool, // Controls whether empty translations should be included (default: false)
51+ * strictTypes?: bool, // Add declare(strict_types=1) (default: false)
52+ * format?: 'normal'|'pretty'|'compact' // How to format the code (default: 'normal')
53+ * pretty?: bool, // Deprecated: use format instead (default: false)
54+ * } | null $options
3755 */
3856 public function __construct (?array $ options = null )
3957 {
4058 $ this ->includeEmpty = (bool ) ($ options ['includeEmpty ' ] ?? false );
4159 $ this ->strictTypes = (bool ) ($ options ['strictTypes ' ] ?? false );
42- $ this ->pretty = (bool ) ($ options ['pretty ' ] ?? false );
60+ if (!isset ($ options ['format ' ])) {
61+ $ this ->format = ($ options ['pretty ' ] ?? false ) ? self ::FORMAT_PRETTY : self ::FORMAT_NORMAL ;
62+ } elseif (in_array ($ options ['format ' ], [self ::FORMAT_PRETTY , self ::FORMAT_COMPACT ], true )) {
63+ $ this ->format = $ options ['format ' ];
64+ } else {
65+ $ this ->format = self ::FORMAT_NORMAL ;
66+ }
4367 }
4468
4569 public function generateString (Translations $ translations ): string
4670 {
4771 $ array = $ this ->generateArray ($ translations );
4872 $ result = '<?php ' ;
49- if ($ this ->pretty ) {
50- $ result .= $ this ->strictTypes ? "\n\ndeclare(strict_types=1); \n\n" : "\n\n" ;
73+ if ($ this ->strictTypes ) {
74+ switch ($ this ->format ) {
75+ case self ::FORMAT_PRETTY :
76+ $ result .= "\n\ndeclare(strict_types=1); \n\n" ;
77+ break ;
78+ case self ::FORMAT_COMPACT :
79+ $ result .= ' declare(strict_types=1); ' ;
80+ break ;
81+ case self ::FORMAT_NORMAL :
82+ default :
83+ $ result .= ' declare(strict_types=1); ' ;
84+ break ;
85+ }
5186 } else {
52- $ result .= $ this ->strictTypes ? ' declare(strict_types=1); ' : ' ' ;
87+ $ result .= $ this ->format === self ::FORMAT_PRETTY ? "\n\n" : ' ' ;
88+ }
89+ switch ($ this ->format ) {
90+ case self ::FORMAT_PRETTY :
91+ $ result .= self ::format ($ array , false );
92+ break ;
93+ case self ::FORMAT_COMPACT :
94+ $ result .= self ::format ($ array , true );
95+ break ;
96+ case self ::FORMAT_NORMAL :
97+ default :
98+ $ result .= 'return ' . var_export ($ array , true ) . '; ' ;
99+ break ;
53100 }
54101
55- return $ result . ' return ' . ( $ this -> pretty ? self :: prettyExport ( $ array ) : ( var_export ( $ array , true ) . ' ; ' )) ;
102+ return $ result ;
56103 }
57104
58105 public function generateArray (Translations $ translations ): array
@@ -95,34 +142,42 @@ private static function hasPluralTranslations(Translation $translation): bool
95142 return implode ('' , $ translation ->getPluralTranslations ()) !== '' ;
96143 }
97144
98- private static function prettyExport (array &$ array ): string
145+ private static function format (array &$ array, bool $ compact ): string
99146 {
100- return self ::prettyExportArray ($ array , 0 ) . "; \n" ;
147+ return ' return ' . self ::formatArray ($ array , $ compact , 0 ) . ( $ compact ? ' ; ' : "; \n" ) ;
101148 }
102149
103- private static function prettyExportArray (array &$ array , int $ depth ): string
150+ private static function formatArray (array &$ array, bool $ compact , int $ depth ): string
104151 {
105152 if ($ array === []) {
106153 return '[] ' ;
107154 }
108155 $ result = '[ ' ;
109156 $ isList = self ::isList ($ array );
110157 foreach ($ array as $ key => $ value ) {
111- $ result .= "\n" . str_repeat (self ::PRETTY_INDENT , $ depth + 1 );
158+ if (!$ compact ) {
159+ $ result .= "\n" . str_repeat (self ::PRETTY_INDENT , $ depth + 1 );
160+ }
112161 if (!$ isList ) {
113- $ result .= var_export ($ key , true ) . ' => ' ;
162+ $ result .= var_export ($ key , true ) . ( $ compact ? ' => ' : ' => ') ;
114163 }
115164 if (is_array ($ value )) {
116- $ result .= self ::prettyExportArray ($ value , $ depth + 1 );
165+ $ result .= self ::formatArray ($ value, $ compact , $ depth + 1 );
117166 } else {
118- $ result .= self ::prettyExportScalar ($ value );
167+ $ result .= self ::formatScalar ($ value );
119168 }
120169 $ result .= ', ' ;
121170 }
122- return $ result . "\n" . str_repeat (self ::PRETTY_INDENT , $ depth ) . '] ' ;
171+ if ($ compact ) {
172+ $ result = substr ($ result , 0 , -1 );
173+ } else {
174+ $ result .= "\n" . str_repeat (self ::PRETTY_INDENT , $ depth );
175+ }
176+
177+ return $ result . '] ' ;
123178 }
124179
125- private static function prettyExportScalar ($ value ): string
180+ private static function formatScalar ($ value ): string
126181 {
127182 return $ value === null ? 'null ' : var_export ($ value , true );
128183 }
0 commit comments