1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\DataValues; |
4
|
|
|
|
5
|
|
|
use SMW\NumberFormatter; |
6
|
|
|
use SMWNumberValue as NumberValue; |
7
|
|
|
use SMWDINumber as DINumber; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This datavalue implements unit support for measuring temperatures. This is |
11
|
|
|
* mostly an example implementation of how to realise custom unit types easily. |
12
|
|
|
* |
13
|
|
|
* @license GNU GPL v2+ |
14
|
|
|
* @since 2.4 |
15
|
|
|
* |
16
|
|
|
* @author Markus Krötzsch |
17
|
|
|
* @author mwjames |
18
|
|
|
*/ |
19
|
|
|
class TemperatureValue extends NumberValue { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $typeid |
23
|
|
|
*/ |
24
|
|
|
public function __construct( $typeid = '' ) { |
25
|
|
|
parent::__construct( '_tem' ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* NumberValue::convertToMainUnit |
30
|
|
|
*/ |
31
|
|
|
protected function convertToMainUnit( $number, $unit ) { |
32
|
|
|
|
33
|
|
|
$this->m_unitin = $this->getUnitID( $unit ); |
|
|
|
|
34
|
|
|
|
35
|
|
|
if ( ( $value = $this->convertToKelvin( $number, $this->m_unitin ) ) === false ) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->m_dataitem = new DINumber( $value ); |
40
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* NumberValue::makeConversionValues |
46
|
|
|
*/ |
47
|
|
|
protected function makeConversionValues() { |
48
|
|
|
|
49
|
|
|
if ( $this->m_unitvalues !== false ) { |
50
|
|
|
return; // do this only once |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->m_unitvalues = array(); |
54
|
|
|
|
55
|
|
|
if ( !$this->isValid() ) { |
56
|
|
|
return $this->m_unitvalues; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$displayUnit = $this->getPreferredDisplayUnit(); |
60
|
|
|
$number = $this->m_dataitem->getNumber(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$unitvalues = array( |
63
|
|
|
'K' => $number, |
64
|
|
|
'°C' => $number - 273.15, |
65
|
|
|
'°F' => ( $number - 273.15 ) * 1.8 + 32, |
66
|
|
|
'°R' => ( $number ) * 1.8 |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
if ( isset( $unitvalues[$displayUnit] ) ) { |
70
|
|
|
$this->m_unitvalues[$displayUnit] = $unitvalues[$displayUnit]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->m_unitvalues += $unitvalues; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* NumberValue::makeUserValue |
78
|
|
|
*/ |
79
|
|
|
protected function makeUserValue() { |
80
|
|
|
|
81
|
|
|
$printunit = ''; |
|
|
|
|
82
|
|
|
|
83
|
|
|
if ( ( $this->m_outformat ) && ( $this->m_outformat != '-' ) && |
84
|
|
|
( $this->m_outformat != '-n' ) && ( $this->m_outformat != '-u' ) ) { // first try given output unit |
85
|
|
|
$printunit = NumberValue::normalizeUnit( $this->m_outformat ); |
86
|
|
|
$this->m_unitin = $this->getUnitID( $printunit ); |
|
|
|
|
87
|
|
|
} else { |
88
|
|
|
$this->m_unitin = $this->getPreferredDisplayUnit(); |
|
|
|
|
89
|
|
|
$printunit = $this->m_unitin; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$value =$this->convertToUnit( |
93
|
|
|
$this->m_dataitem->getNumber(), |
|
|
|
|
94
|
|
|
$this->m_unitin |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
// -u is the format for displaying the unit only |
98
|
|
|
if ( $this->m_outformat == '-u' ) { |
99
|
|
|
$this->m_caption = ''; |
100
|
|
|
} elseif ( ( $this->m_outformat != '-' ) && ( $this->m_outformat != '-n' ) ) { |
101
|
|
|
$this->m_caption = $this->getLocalizedFormattedNumberByPrecision( $value ); |
102
|
|
|
$this->m_caption .= ' '; |
103
|
|
|
} else { |
104
|
|
|
$this->m_caption = $this->getUnformattedNumberByPrecision( $value ); |
105
|
|
|
$this->m_caption .= ' '; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// -n is the format for displaying the number only |
109
|
|
|
if ( $this->m_outformat == '-n' ) { |
110
|
|
|
$printunit = ''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->m_caption .= $printunit; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Helper method to find the main representation of a certain unit. |
118
|
|
|
*/ |
119
|
|
|
protected function getUnitID( $unit ) { |
120
|
|
|
/// TODO possibly localise some of those strings |
121
|
|
|
switch ( $unit ) { |
122
|
|
|
case '': |
123
|
|
|
case 'K': |
124
|
|
|
case 'Kelvin': |
125
|
|
|
case 'kelvin': |
126
|
|
|
case 'kelvins': |
127
|
|
|
return 'K'; |
128
|
|
|
// There's a dedicated Unicode character (℃, U+2103) for degrees C. |
129
|
|
|
// Your font may or may not display it; do not be alarmed. |
130
|
|
|
case '°C': |
131
|
|
|
case '℃': |
132
|
|
|
case 'Celsius': |
133
|
|
|
case 'centigrade': |
134
|
|
|
return '°C'; |
135
|
|
|
case '°F': |
136
|
|
|
case 'Fahrenheit': |
137
|
|
|
return '°F'; |
138
|
|
|
case '°R': |
139
|
|
|
case 'Rankine': |
140
|
|
|
return '°R'; |
141
|
|
|
default: |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* NumberValue::getUnitList |
148
|
|
|
*/ |
149
|
|
|
public function getUnitList() { |
150
|
|
|
return array( 'K', '°C', '°F', '°R' ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* NumberValue::getUnit |
155
|
|
|
*/ |
156
|
|
|
public function getUnit() { |
157
|
|
|
return 'K'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function getPreferredDisplayUnit() { |
161
|
|
|
|
162
|
|
|
$unit = $this->getUnit(); |
163
|
|
|
|
164
|
|
|
if ( $this->getProperty() === null ) { |
165
|
|
|
return $unit; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$units = $this->getPropertySpecificationLookup()->getDisplayUnitsFor( |
169
|
|
|
$this->getProperty() |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
if ( $units !== null && $units !== array() ) { |
173
|
|
|
$unit = $this->getUnitID( end( $units ) ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->getUnitID( $unit ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function convertToKelvin( $number, $unit ) { |
180
|
|
|
|
181
|
|
|
switch ( $unit ) { |
182
|
|
|
case 'K': |
183
|
|
|
return $number; |
184
|
|
|
break; |
|
|
|
|
185
|
|
|
case '°C': |
186
|
|
|
return $number + 273.15; |
187
|
|
|
break; |
|
|
|
|
188
|
|
|
case '°F': |
189
|
|
|
return ( $number - 32 ) / 1.8 + 273.15; |
190
|
|
|
break; |
|
|
|
|
191
|
|
|
case '°R': |
192
|
|
|
return ( $number ) / 1.8; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return false; // unsupported unit |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
private function convertToUnit( $number, $unit ) { |
199
|
|
|
|
200
|
|
|
switch ( $unit ) { |
201
|
|
|
case 'K': |
202
|
|
|
return $number; |
203
|
|
|
break; |
|
|
|
|
204
|
|
|
case '°C': |
205
|
|
|
return $number - 273.15; |
206
|
|
|
break; |
|
|
|
|
207
|
|
|
case '°F': |
208
|
|
|
return ( $number - 273.15 ) * 1.8 + 32; |
209
|
|
|
break; |
|
|
|
|
210
|
|
|
case '°R': |
211
|
|
|
return ( $number ) * 1.8; |
212
|
|
|
break; |
|
|
|
|
213
|
|
|
// default: unit not supported |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return 0; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.