Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/97768


Change subject: (bug 57589) option to force "+" in decimal values.
......................................................................

(bug 57589) option to force "+" in decimal values.

This can also be used to force the "+" when formattiong
QuantityValues.

Change-Id: I079f832ad4618421b2989912177fc9d99b7f63ad
---
M DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
M DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
M DataValuesCommon/tests/ValueFormatters/QuantityFormatterTest.php
3 files changed, 37 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues 
refs/changes/68/97768/1

diff --git a/DataValuesCommon/src/ValueFormatters/DecimalFormatter.php 
b/DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
index fb30e91..41a3cc5 100644
--- a/DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
+++ b/DataValuesCommon/src/ValueFormatters/DecimalFormatter.php
@@ -16,6 +16,19 @@
 class DecimalFormatter extends ValueFormatterBase {
 
        /**
+        * Option key for forcing the sign to be included in the
+        * formatter's output even if it's "+". The value must
+        * be a boolean.
+        */
+       const OPT_FORCE_SIGN = 'forceSign';
+
+       public function __construct( FormatterOptions $options ) {
+               $options->defaultOption( self::OPT_FORCE_SIGN, false );
+
+               parent::__construct( $options );
+       }
+
+       /**
         * Formats a QuantityValue data value
         *
         * @since 0.1
@@ -31,10 +44,13 @@
                }
 
                // TODO: Implement localization of decimal numbers!
+               // TODO: Implement optional rounding/padding
                $decimal = $dataValue->getValue();
 
-               // strip leading +
-               $decimal = ltrim( $decimal, '+' );
+               if ( !$this->getOption( self::OPT_FORCE_SIGN ) ) {
+                       // strip leading +
+                       $decimal = ltrim( $decimal, '+' );
+               }
 
                return $decimal;
        }
diff --git a/DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php 
b/DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
index 6c92d58..4eb7aa7 100644
--- a/DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
+++ b/DataValuesCommon/tests/ValueFormatters/DecimalFormatterTest.php
@@ -30,19 +30,24 @@
        public function validProvider() {
                $options = new FormatterOptions();
 
+               $optionsForceSign = new FormatterOptions( array(
+                       DecimalFormatter::OPT_FORCE_SIGN => true
+               ) );
+
                $decimals = array(
-                       '+0' => '0',
-                       '+0.0' => '0.0',
-                       '-0.0130' => '-0.0130',
-                       '+10000.013' => '10000.013',
-                       '-12' => '-12'
+                       '+0' => array( '0', $options ),
+                       '+0.0' => array( '0.0', $options ),
+                       '-0.0130' => array( '-0.0130', $options ),
+                       '+10000.013' => array( '10000.013', $options ),
+                       '+20000.4' => array( '+20000.4', $optionsForceSign ),
+                       '-12' => array( '-12', $options )
                );
 
                $argLists = array();
-               foreach ( $decimals as $input => $expected ) {
+               foreach ( $decimals as $input => $args ) {
                        $inputValue = new DecimalValue( $input );
 
-                       $argLists[$input] = array( $inputValue, $expected, 
$options );
+                       $argLists[$input] = array_merge( array( $inputValue ), 
$args );
                }
 
                return $argLists;
diff --git a/DataValuesCommon/tests/ValueFormatters/QuantityFormatterTest.php 
b/DataValuesCommon/tests/ValueFormatters/QuantityFormatterTest.php
index 0eefca8..2336974 100644
--- a/DataValuesCommon/tests/ValueFormatters/QuantityFormatterTest.php
+++ b/DataValuesCommon/tests/ValueFormatters/QuantityFormatterTest.php
@@ -7,7 +7,6 @@
 use ValueFormatters\QuantityFormatter;
 use ValueFormatters\FormatterOptions;
 use ValueFormatters\ValueFormatter;
-use Wikibase\Lib\Serializers\SerializationOptions;
 
 /**
  * @covers ValueFormatters\QuantityFormatter
@@ -48,6 +47,11 @@
                        QuantityFormatter::OPT_APPLY_ROUNDING => -2
                ) );
 
+               $forceSign= new FormatterOptions( array(
+                       QuantityFormatter::OPT_SHOW_UNCERTAINTY_MARGIN => false,
+                       DecimalFormatter::OPT_FORCE_SIGN => true,
+               ) );
+
                return array(
                        '+0/nm' => array( QuantityValue::newFromNumber( '+0', 
'1', '+0', '+0' ), '0', $noMargin ),
                        '+0/wm' => array( QuantityValue::newFromNumber( '+0', 
'1', '+0', '+0' ), '0', $withMargin ),
@@ -67,6 +71,8 @@
 
                        '+3.125/nr' => array( QuantityValue::newFromNumber( 
'+3.125', '1', '+3.2', '+3.0' ), '3.125±0.125', $noRounding ),
                        '+3.125/xr' => array( QuantityValue::newFromNumber( 
'+3.125', '1', '+3.2', '+3.0' ), '3.13±0.13', $exactRounding ),
+
+                       '+3.125/fs' => array( QuantityValue::newFromNumber( 
'+3.125', '1', '+3.2', '+3.0' ), '+3.13', $forceSign ),
                );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/97768
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I079f832ad4618421b2989912177fc9d99b7f63ad
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to