00001 <?php
00017 class SMWTemperatureValue extends SMWNumberValue {
00018
00019 protected function convertToMainUnit( $number, $unit ) {
00020
00021
00022 $this->m_unitin = $this->getUnitID( $unit );
00023 switch ( $this->m_unitin ) {
00024 case 'K':
00025 $value = $number;
00026 break;
00027 case '°C':
00028 $value = $number + 273.15;
00029 break;
00030 case '°F':
00031 $value = ( $number - 32 ) / 1.8 + 273.15;
00032 break;
00033 case '°R':
00034 $value = ( $number ) / 1.8;
00035 break;
00036 default: return false;
00037 }
00038 $this->m_dataitem = new SMWDINumber( $value, $this->m_typeid );
00039 return true;
00040 }
00041
00042 protected function makeConversionValues() {
00044 if ( $this->m_unitvalues !== false ) return;
00045 if ( !$this->isValid() ) {
00046 $this->m_unitvalues = array();
00047 } else {
00048 $this->m_unitvalues = array( 'K' => $this->m_dataitem->getNumber(),
00049 '°C' => $this->m_dataitem->getNumber() - 273.15,
00050 '°F' => ( $this->m_dataitem->getNumber() - 273.15 ) * 1.8 + 32,
00051 '°R' => ( $this->m_dataitem->getNumber() ) * 1.8 );
00052 }
00053 }
00054
00055 protected function makeUserValue() {
00056 $value = false;
00057 if ( ( $this->m_outformat ) && ( $this->m_outformat != '-' ) &&
00058 ( $this->m_outformat != '-n' ) && ( $this->m_outformat != '-u' ) ) {
00059 $printunit = SMWNumberValue::normalizeUnit( $this->m_outformat );
00060 $this->m_unitin = $this->getUnitID( $printunit );
00061 switch ( $this->m_unitin ) {
00062 case 'K':
00063 $value = $this->m_dataitem->getNumber();
00064 break;
00065 case '°C':
00066 $value = $this->m_dataitem->getNumber() - 273.15;
00067 break;
00068 case '°F':
00069 $value = ( $this->m_dataitem->getNumber() - 273.15 ) * 1.8 + 32;
00070 break;
00071 case '°R':
00072 $value = ( $this->m_dataitem->getNumber() ) * 1.8;
00073 break;
00074
00075 }
00076 }
00077 if ( $value === false ) {
00078 $value = $this->m_dataitem->getNumber();
00079 $this->m_unitin = 'K';
00080 $printunit = 'K';
00081 }
00082
00083 $this->m_caption = '';
00084 if ( $this->m_outformat != '-u' ) {
00085 $this->m_caption .= ( ( $this->m_outformat != '-' ) && ( $this->m_outformat != '-n' ) ? smwfNumberFormat( $value ) : $value );
00086 }
00087 if ( ( $printunit !== '' ) && ( $this->m_outformat != '-n' ) ) {
00088 if ( $this->m_outformat != '-u' ) {
00089 $this->m_caption .= ( $this->m_outformat != '-' ? ' ' : ' ' );
00090 }
00091 $this->m_caption .= $printunit;
00092 }
00093 }
00094
00098 protected function getUnitID( $unit ) {
00100 switch ( $unit ) {
00101 case '': case 'K': case 'Kelvin': case 'kelvin': case 'kelvins': return 'K';
00102
00103
00104 case '°C': case '℃': case 'Celsius': case 'centigrade': return '°C';
00105 case '°F': case 'Fahrenheit': return '°F';
00106 case '°R': case 'Rankine': return '°R';
00107 default: return false;
00108 }
00109 }
00110
00111 public function getUnitList() {
00112 return array( 'K', '°C', '°F', '°R' );
00113 }
00114
00115 public function getUnit() {
00116 return 'K';
00117 }
00118
00119 }