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(' ', ' ', ' ', ' '), '', $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 != '-' ? ' ' : ' '; } $this->m_caption .= $printunit; } }
/** * 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 } }