@@ -56,10 +56,11 @@ class Enviro
5656
5757 /**
5858 * Omit extra parenthesis in plural rule formulas.
59+ * If null: formulas will be exported with and without extra parenthesis (if supported by the exporter).
5960 *
60- * @var bool
61+ * @var bool|null
6162 */
62- public static $ noExtraParenthesis ;
63+ public static $ extraParenthesis ;
6364
6465 /**
6566 * Parse the command line options.
@@ -72,7 +73,7 @@ class Enviro
7273 self ::$ outputFilename = null ;
7374 self ::$ languages = null ;
7475 self ::$ reduce = null ;
75- self ::$ noExtraParenthesis = false ;
76+ self ::$ extraParenthesis = true ;
7677 $ exporters = Exporter::getExporters ();
7778 if (isset ($ argv ) && is_array ($ argv )) {
7879 foreach ($ argv as $ argi => $ arg ) {
@@ -96,10 +97,13 @@ class Enviro
9697 self ::$ reduce = false ;
9798 break ;
9899 case '--parenthesis=yes ' :
99- self ::$ noExtraParenthesis = false ;
100+ self ::$ extraParenthesis = true ;
100101 break ;
101102 case '--parenthesis=no ' :
102- self ::$ noExtraParenthesis = true ;
103+ self ::$ extraParenthesis = false ;
104+ break ;
105+ case '--parenthesis=both ' :
106+ self ::$ extraParenthesis = null ;
103107 break ;
104108 default :
105109 if (preg_match ('/^--output=.+$/ ' , $ argLC )) {
@@ -185,6 +189,8 @@ Where:
185189 plural rules formulas.
186190 Those extra parenthesis are needed to create a PHP-compatible
187191 formula.
192+ Some exporter may also export formulas both with and without
193+ The extra parenthesis: use --parenthesis=both in this case
188194 Defaults to 'yes'
189195 --output
190196 if specified, the output will be saved to <file name>. If not
@@ -265,7 +271,7 @@ try {
265271 if (Enviro::$ reduce ) {
266272 $ languages = Enviro::reduce ($ languages );
267273 }
268- if (Enviro::$ noExtraParenthesis ) {
274+ if (Enviro::$ extraParenthesis === false ) {
269275 $ languages = array_map (
270276 function (Language $ language ) {
271277 $ language ->formula = $ language ->buildFormula (true );
@@ -275,10 +281,18 @@ try {
275281 $ languages
276282 );
277283 }
284+ $ exporterClass = Exporter::getExporterClassName (Enviro::$ outputFormat );
285+ $ options = array (
286+ 'us-ascii ' => Enviro::$ outputUSAscii ,
287+ 'both-formulas ' => Enviro::$ extraParenthesis === null ,
288+ );
289+ if ($ options ['both-formulas ' ] && !call_user_func (array ($ exporterClass , 'supportsFormulasWithAndWithoutParenthesis ' ))) {
290+ throw new Exception ("The selected exporter doesn't support exporting data with and without extra paranthesis " );
291+ }
278292 if (isset (Enviro::$ outputFilename )) {
279- echo call_user_func (array (Exporter:: getExporterClassName (Enviro:: $ outputFormat ) , 'toFile ' ), $ languages , Enviro::$ outputFilename , array ( ' us-ascii ' => Enviro:: $ outputUSAscii ) );
293+ echo call_user_func (array ($ exporterClass , 'toFile ' ), $ languages , Enviro::$ outputFilename , $ options );
280294 } else {
281- echo call_user_func (array (Exporter:: getExporterClassName (Enviro:: $ outputFormat ) , 'toString ' ), $ languages , array ( ' us-ascii ' => Enviro:: $ outputUSAscii ) );
295+ echo call_user_func (array ($ exporterClass , 'toString ' ), $ languages , $ options );
282296 }
283297} catch (Exception $ x ) {
284298 fwrite (STDERR , $ x ->getMessage () . "\n" );
0 commit comments