-
Notifications
You must be signed in to change notification settings - Fork 2
/
UnitConverter.php
executable file
·249 lines (234 loc) · 9.47 KB
/
UnitConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* Copyright 2020 MDS Technologies (Pty) Ltd and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0).
* It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0
*
* @author MDS Collivery <[email protected]>
* @copyright 2020 MDS Technologies (Pty) Ltd
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
namespace Mds;
class UnitConverter
{
public $conversion_table = array();
public $decimal_point;
public $thousand_separator;
public $bases = array();
public function __construct()
{
// Kilogramme, Gramme, Milligramme, Pounds, Ounce
// Metres, Centimetres, Millimetres, Yards, Foot, Inches
$conversions = array(
'Weight' => array(
'base' => 'kg',
'conv' => array(
'g' => 1000,
'mg' => 1000000,
't' => 0.001,
'oz' => 35.274,
'lb' => 2.2046,
)
),
'Distance' => array(
'base' => 'km',
'conv' => array(
'm' => 1000,
'cm' => 100000,
'mm' => 1000000,
'in' => 39370,
'ft' => 3280.8,
'yd' => 1093.6
)
)
);
foreach ($conversions as $val) {
$this->addConversion($val['base'], $val['conv']);
}
}
/**
* Constructor. Initializes the unitConverter object with the most important
* properties.
*
* @param string decimal point character
* @param string thousand separator character
* @return void
* @access public
*/
public function unitConverter($dec_point = '.', $thousand_sep = '')
{
$this->decimal_point = $dec_point;
$this->thousand_separator = $thousand_sep;
}
/**
* Adds a conversion ratio to the conversion table.
*
* @param string the name of unit from which to convert
* @param array array(
* "pound"=>array("ratio"=>'', "offset"=>'')
* )
* "pound" - name of unit to set conversion ration to
* "ratio" - 'double' conversion ratio which, when
* multiplied by the number of $from_unit units produces
* the result
* "offset" - an offset from 0 which will be added to
* the result when converting (needed for temperature
* conversions and defaults to 0).
* @return boolean true if successful, false otherwise
* @access public
*/
public function addConversion($from_unit, $to_array)
{
if (!isset($this->conversion_table[$from_unit])) {
while (list($key, $val) = each($to_array)) {
if (strstr($key, '/')) {
$to_units = explode('/', $key);
foreach ($to_units as $to_unit) {
$this->bases[$from_unit][] = $to_unit;
if (!is_array($val)) {
$this->conversion_table[$from_unit . "_" . $to_unit] = array(
"ratio" => $val,
"offset" => 0
);
} else {
$this->conversion_table[$from_unit . "_" . $to_unit] = array(
"ratio" => $val['ratio'],
"offset" => (isset($val['offset']) ? $val['offset'] : 0)
);
}
}
} else {
$this->bases[$from_unit][] = $key;
if (!is_array($val)) {
$this->conversion_table[$from_unit . "_" . $key] = array( "ratio" => $val, "offset" => 0 );
} else {
$this->conversion_table[$from_unit . "_" . $key] = array(
"ratio" => $val['ratio'],
"offset" => (isset($val['offset']) ? $val['offset'] : 0)
);
}
}
}
return true;
}
return false;
}
/**
* Converts from one unit to another using specified precision.
*
* @param double value to convert
* @param string name of the source unit from which to convert
* @param string name of the target unit to which we are converting
* @param integer double precision of the end result
* @return void
* @access public
*/
public function convert($value, $from_unit, $to_unit, $precision, $converted = null)
{
if ($this->getConvSpecs($from_unit, $to_unit, $value, $converted)) {
if (!empty($converted)) {
return number_format($converted, (int) $precision, $this->decimal_point, $this->thousand_separator);
} else {
return "";
}
} else {
return false;
}
}
/**
* CVH : changed this Function getConvSpecs in order to have it look up
* intermediary Conversions from the
* "base" unit being that one that has the highest hierarchical order in one
* "logical" Conversion_Array
* when taking $conv->addConversion('km',
* array('meter'=>1000, 'dmeter'=>10000, 'centimeter'=>100000,
* 'millimeter'=>1000000, 'mile'=>0.62137, 'naut.mile'=>0.53996,
* 'inch(es)/zoll'=>39370, 'ft/foot/feet'=>3280.8, 'yd/yard'=>1093.6));
* "km" would be the logical base unit for all units of dinstance, thus,
* if the function fails to find a direct or reverse conversion in the table
* it is only logical to suspect that if there is a chance
* converting the value it only is via the "base" unit, and so
* there is not even a need for a recursive search keeping the perfomance
* acceptable and the ressource small...
*
* CVH check_key checks for a key in the Conversiontable and returns a value
*
* @param $key
*
* @return bool
*/
public function checkKey($key)
{
if (array_key_exists($key, $this->conversion_table)) {
if (!empty($this->conversion_table[$key])) {
return $this->conversion_table[$key];
}
}
return false;
}
/**
* Key function. Finds the conversion ratio and offset from one unit to another.
*
* @param string name of the source unit from which to convert
* @param string name of the target unit to which we are converting
* @param double conversion ratio found. Returned by reference.
* @param double offset which needs to be added (or subtracted, if negative)
* to the result to convert correctly.
* For temperature or some scientific conversions,
* i.e. Fahrenheit -> Celcius
* @return boolean true if ratio and offset are found for the supplied
* units, false otherwise
* @access private
*/
public function getConvSpecs($from_unit, $to_unit, $value, &$converted)
{
$key = $from_unit . "_" . $to_unit;
$revkey = $to_unit . "_" . $from_unit;
$found = false;
if ($ct_arr = $this->checkKey($key)) {
// Conversion Specs found directly
$ratio = (double) $ct_arr['ratio'];
$offset = $ct_arr['offset'];
$converted = (double) (($value * $ratio) + $offset);
return true;
} elseif ($ct_arr = $this->checkKey($revkey)) {
// not found in direct order, try reverse order
$ratio = (double) (1 / $ct_arr['ratio']);
$offset = -$ct_arr['offset'];
$converted = (double) (($value + $offset) * $ratio);
return true;
} else {
// not found test for intermediary conversion
// return ratio = 1 if keyparts match
if ($key == $revkey) {
$ratio = 1;
$offset = 0;
$converted = $value;
return true;
}
// otherwise search intermediary
reset($this->conversion_table);
while (list($convk, $i1_value) = each($this->conversion_table)) {
// split the key into parts
$keyparts = preg_split("/_/", $convk);
// return ratio = 1 if keyparts match
// Now test if either part matches the from or to unit
if ($keyparts[1] == $to_unit && ($i2_value = $this->checkKey($keyparts[0] . "_" . $from_unit))) {
// an intermediary $keyparts[0] was found
// now let us put things together intermediary 1 and 2
$converted = (double) (((($value - $i2_value['offset']) / $i2_value['ratio']) * $i1_value['ratio']) + $i1_value['offset']);
$found = true;
} elseif ($keyparts[1] == $from_unit && ($i2_value = $this->checkKey($keyparts[0] . "_" . $to_unit))) {
// an intermediary $keyparts[0] was found
// now let us put things together intermediary 2 and 1
$converted = (double) (((($value - $i1_value['offset']) / $i1_value['ratio']) + $i2_value['offset']) * $i2_value['ratio']);
$found = true;
}
}
return $found;
}
}
}