/**
  * @dataProvider numberValueProvider
  */
 public function testNumberFormat($numberUserValue, $type, $linker, $expected)
 {
     $numberValue = new NumberValue('_num');
     $numberValue->setUserValue($numberUserValue);
     $numberValue->setOption('user.language', 'en');
     $numberValue->setOption('content.language', 'en');
     $instance = new NumberValueFormatter($numberValue);
     $this->assertEquals($expected, $instance->format($type, $linker));
 }
 public function testGetInfolinkTextOnNumberValue()
 {
     $this->cachedPropertyValuesPrefetcher->expects($this->atLeastOnce())->method('getPropertyValues')->will($this->returnValue(array()));
     $numberValue = new NumberValue();
     $numberValue->setOption('user.language', 'en');
     $numberValue->setOption('content.language', 'en');
     $numberValue->setProperty($this->dataItemFactory->newDIProperty('Foo'));
     $numberValue->setUserValue('1000.42');
     $instance = new InfoLinksProvider($numberValue);
     $this->assertContains('/:Foo/1000.42|+]]</span>', $instance->getInfolinkText(SMW_OUTPUT_WIKI));
     $this->assertContains('/:Foo/1000.42">+</a></span>', $instance->getInfolinkText(SMW_OUTPUT_HTML));
 }
 /**
  * @dataProvider conversionDataProvider
  */
 public function testFetchConversionData($thousands, $decimal, $correspondsTo, $unitIds, $unitFactors, $mainUnit, $prefixalUnitPreference)
 {
     $cachedPropertyValuesPrefetcher = $this->getMockBuilder('\\SMW\\CachedPropertyValuesPrefetcher')->disableOriginalConstructor()->getMock();
     $cachedPropertyValuesPrefetcher->expects($this->once())->method('getPropertyValues')->will($this->returnValue(array($this->dataItemFactory->newDIBlob($correspondsTo))));
     $numberValue = new NumberValue();
     $numberValue->setOption('separator.thousands', $thousands);
     $numberValue->setOption('separator.decimal', $decimal);
     $instance = new UnitConversionFetcher($numberValue, $cachedPropertyValuesPrefetcher);
     $instance->fetchConversionData($this->dataItemFactory->newDIProperty('Foo'));
     $this->assertEmpty($instance->getErrors());
     $this->assertEquals($unitIds, $instance->getUnitIds());
     $this->assertEquals($unitFactors, $instance->getUnitFactors());
     $this->assertEquals($mainUnit, $instance->getMainUnit());
     $this->assertEquals($prefixalUnitPreference, $instance->getPrefixalUnitPreference());
 }
 private function matchUnitAliases($number, $asPrefix, array $unitAliases)
 {
     $first = true;
     foreach ($unitAliases as $unit) {
         $unit = $this->numberValue->normalizeUnit($unit);
         // Legacy match the preserve some behaviour where spaces where normalized
         // no matter what
         $normalizedUnit = $this->numberValue->normalizeUnit(str_replace(array('&nbsp;', '&#160;', '&thinsp;', ' '), '', $unit));
         if ($first) {
             $unitid = $unit;
             if ($number == 1) {
                 // add main unit to front of array (displayed first)
                 $this->mainUnit = $unit;
                 $this->unitFactors = array($unit => 1) + $this->unitFactors;
             } else {
                 // non-main units are not ordered (can be modified via display units)
                 $this->unitFactors[$unit] = $number;
             }
             $first = false;
         }
         // add all known units to m_unitids to simplify checking for them
         $this->unitIds[$unit] = $unitid;
         $this->unitIds[$normalizedUnit] = $unitid;
         $this->prefixalUnitPreference[$unit] = $asPrefix;
     }
 }
 protected function makeUserValue()
 {
     $value = false;
     if ($this->m_outformat && $this->m_outformat != '-' && $this->m_outformat != '-n' && $this->m_outformat != '-u') {
         // first try given output unit
         $printunit = SMWNumberValue::normalizeUnit($this->m_outformat);
         $this->m_unitin = $this->getUnitID($printunit);
         switch ($this->m_unitin) {
             case 'K':
                 $value = $this->m_dataitem->getNumber();
                 break;
             case '°C':
                 $value = $this->m_dataitem->getNumber() - 273.15;
                 break;
             case '°F':
                 $value = ($this->m_dataitem->getNumber() - 273.15) * 1.8 + 32;
                 break;
             case '°R':
                 $value = $this->m_dataitem->getNumber() * 1.8;
                 break;
                 // default: unit not supported
         }
     }
     if ($value === false) {
         // no valid output unit requested
         $value = $this->m_dataitem->getNumber();
         $this->m_unitin = 'K';
         $printunit = 'K';
     }
     $this->m_caption = '';
     if ($this->m_outformat != '-u') {
         // -u is the format for displaying the unit only
         $this->m_caption .= $this->m_outformat != '-' && $this->m_outformat != '-n' ? NumberFormatter::getInstance()->getLocalizedFormattedNumber($value, $this->getPrecision()) : NumberFormatter::getInstance()->getUnformattedNumberByPrecision($value, $this->getPrecision());
     }
     if ($printunit !== '' && $this->m_outformat != '-n') {
         // -n is the format for displaying the number only
         if ($this->m_outformat != '-u') {
             $this->m_caption .= $this->m_outformat != '-' ? '&#160;' : ' ';
         }
         $this->m_caption .= $printunit;
     }
 }
 /**
  * @param string $typeid
  */
 public function __construct($typeid = '')
 {
     parent::__construct('_tem');
 }
Exemple #7
0
 /**
  * This method initializes $m_displayunits.
  */
 protected function initDisplayData()
 {
     if ($this->m_displayunits !== false) {
         return;
         // do the below only once
     }
     $this->initConversionData();
     // needed to normalise unit strings
     $this->m_displayunits = array();
     if (is_null($this->m_property) || is_null($this->m_property->getDIWikiPage())) {
         return;
     }
     $dataItems = \SMW\StoreFactory::getStore()->getPropertyValues($this->m_property->getDIWikiPage(), new SMWDIProperty('_UNIT'));
     $units = array();
     foreach ($dataItems as $di) {
         // Join all if many annotations exist. Discouraged (random order) but possible.
         if ($di instanceof SMWDIBlob) {
             $units = $units + preg_split('/\\s*,\\s*/u', $di->getString());
         }
     }
     foreach ($units as $unit) {
         $unit = SMWNumberValue::normalizeUnit($unit);
         if (array_key_exists($unit, $this->m_unitids)) {
             $this->m_displayunits[] = $unit;
             // do not avoid duplicates, users can handle this
         }
         // note: we ignore unsuppported units -- no way to display them
     }
 }