/
Volume.php
363 lines (335 loc) · 14.7 KB
/
Volume.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Measure
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id$
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* Implement needed classes
*/
require_once 'Zend/Measure/Exception.php';
require_once 'Zend/Measure/Abstract.php';
require_once 'Zend/Locale.php';
/**
* @category Zend
* @package Zend_Measure
* @subpackage Zend_Measure_Volume
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Measure_Volume extends Zend_Measure_Abstract
{
// Volume definitions
const STANDARD = 'CUBIC_METER';
const ACRE_FOOT = 'ACRE_FOOT';
const ACRE_FOOT_SURVEY = 'ACRE_FOOT_SURVEY';
const ACRE_INCH = 'ACRE_INCH';
const BARREL_WINE = 'BARREL_WINE';
const BARREL = 'BARREL';
const BARREL_US_DRY = 'BARREL_US_DRY';
const BARREL_US_FEDERAL = 'BARREL_US_FEDERAL';
const BARREL_US = 'BARREL_US';
const BARREL_US_PETROLEUM = 'BARREL_US_PETROLEUM';
const BOARD_FOOT = 'BOARD_FOOT';
const BUCKET = 'BUCKET';
const BUCKET_US = 'BUCKET_US';
const BUSHEL = 'BUSHEL';
const BUSHEL_US = 'BUSHEL_US';
const CENTILTER = 'CENTILITER';
const CORD = 'CORD';
const CORD_FOOT = 'CORD_FOOT';
const CUBIC_CENTIMETER = 'CUBIC_CENTIMETER';
const CUBIC_CUBIT = 'CUBIC_CUBIT';
const CUBIC_DECIMETER = 'CUBIC_DECIMETER';
const CUBIC_DEKAMETER = 'CUBIC_DEKAMETER';
const CUBIC_FOOT = 'CUBIC_FOOT';
const CUBIC_INCH = 'CUBIC_INCH';
const CUBIC_KILOMETER = 'CUBIC_KILOMETER';
const CUBIC_METER = 'CUBIC_METER';
const CUBIC_MILE = 'CUBIC_MILE';
const CUBIC_MICROMETER = 'CUBIC_MICROMETER';
const CUBIC_MILLIMETER = 'CUBIC_MILLIMETER';
const CUBIC_YARD = 'CUBIC_YARD';
const CUP_CANADA = 'CUP_CANADA';
const CUP = 'CUP';
const CUP_US = 'CUP_US';
const DECILITER = 'DECILITER';
const DEKALITER = 'DEKALITER';
const DRAM = 'DRAM';
const DRUM_US = 'DRUM_US';
const DRUM = 'DRUM';
const FIFTH = 'FIFTH';
const GALLON = 'GALLON';
const GALLON_US_DRY = 'GALLON_US_DRY';
const GALLON_US = 'GALLON_US';
const GILL = 'GILL';
const GILL_US = 'GILL_US';
const HECTARE_METER = 'HECTARE_METER';
const HECTOLITER = 'HECTOLITER';
const HOGSHEAD = 'HOGSHEAD';
const HOGSHEAD_US = 'HOGSHEAD_US';
const JIGGER = 'JIGGER';
const KILOLITER = 'KILOLITER';
const LITER = 'LITER';
const MEASURE = 'MEASURE';
const MEGALITER = 'MEGALITER';
const MICROLITER = 'MICROLITER';
const MILLILITER = 'MILLILITER';
const MINIM = 'MINIM';
const MINIM_US = 'MINIM_US';
const OUNCE = 'OUNCE';
const OUNCE_US = 'OUNCE_US';
const PECK = 'PECK';
const PECK_US = 'PECK_US';
const PINT = 'PINT';
const PINT_US_DRY = 'PINT_US_DRY';
const PINT_US = 'PINT_US';
const PIPE = 'PIPE';
const PIPE_US = 'PIPE_US';
const PONY = 'PONY';
const QUART_GERMANY = 'QUART_GERMANY';
const QUART_ANCIENT = 'QUART_ANCIENT';
const QUART = 'QUART';
const QUART_US_DRY = 'QUART_US_DRY';
const QUART_US = 'QUART_US';
const QUART_UK = 'QUART_UK';
const SHOT = 'SHOT';
const STERE = 'STERE';
const TABLESPOON = 'TABLESPOON';
const TABLESPOON_UK = 'TABLESPOON_UK';
const TABLESPOON_US = 'TABLESPOON_US';
const TEASPOON = 'TEASPOON';
const TEASPOON_UK = 'TEASPOON_UK';
const TEASPOON_US = 'TEASPOON_US';
const YARD = 'YARD';
private static $_UNITS = array(
'ACRE_FOOT' => array(1233.48185532, 'ac ft'),
'ACRE_FOOT_SURVEY' => array(1233.489, 'ac ft'),
'ACRE_INCH' => array(102.79015461, 'ac in'),
'BARREL_WINE' => array(0.143201835, 'bbl'),
'BARREL' => array(0.16365924, 'bbl'),
'BARREL_US_DRY' => array(array('' => 26.7098656608, '/' => 231), 'bbl'),
'BARREL_US_FEDERAL' => array(0.1173477658, 'bbl'),
'BARREL_US' => array(0.1192404717, 'bbl'),
'BARREL_US_PETROLEUM' => array(0.1589872956, 'bbl'),
'BOARD_FOOT' => array(array('' => 6.5411915904, '/' => 2772), 'board foot'),
'BUCKET' => array(0.01818436, 'bucket'),
'BUCKET_US' => array(0.018927059, 'bucket'),
'BUSHEL' => array(0.03636872, 'bu'),
'BUSHEL_US' => array(0.03523907, 'bu'),
'CENTILITER' => array(0.00001, 'cl'),
'CORD' => array(3.624556416, 'cd'),
'CORD_FOOT' => array(0.453069552, 'cd ft'),
'CUBIC_CENTIMETER' => array(0.000001, 'cm³'),
'CUBIC_CUBIT' => array(0.144, 'cubit³'),
'CUBIC_DECIMETER' => array(0.001, 'dm³'),
'CUBIC_DEKAMETER' => array(1000, 'dam³'),
'CUBIC_FOOT' => array(array('' => 6.54119159, '/' => 231), 'ft³'),
'CUBIC_INCH' => array(array('' => 0.0037854118, '/' => 231), 'in³'),
'CUBIC_KILOMETER' => array(1.0e+9, 'km³'),
'CUBIC_METER' => array(1, 'm³'),
'CUBIC_MILE' => array(array('' => 0.0037854118, '/' => 231, '*' => 75271680, '*' => 3379200), 'mi³'),
'CUBIC_MICROMETER' => array(1.0e-18, 'µm³'),
'CUBIC_MILLIMETER' => array(1.0e-9, 'mm³'),
'CUBIC_YARD' => array(array('' => 0.0037854118, '/' => 231, '*' => 46656), 'yd³'),
'CUP_CANADA' => array(0.0002273045, 'c'),
'CUP' => array(0.00025, 'c'),
'CUP_US' => array(array('' => 0.0037854118, '/' => 16), 'c'),
'DECILITER' => array(0.0001, 'dl'),
'DEKALITER' => array(0.001, 'dal'),
'DRAM' => array(array('' => 0.0037854118, '/' => 1024), 'dr'),
'DRUM_US' => array(0.208197649, 'drum'),
'DRUM' => array(0.2, 'drum'),
'FIFTH' => array(0.00075708236, 'fifth'),
'GALLON' => array(0.00454609, 'gal'),
'GALLON_US_DRY' => array(0.0044048838, 'gal'),
'GALLON_US' => array(0.0037854118, 'gal'),
'GILL' => array(array('' => 0.00454609, '/' => 32), 'gi'),
'GILL_US' => array(array('' => 0.0037854118, '/' => 32), 'gi'),
'HECTARE_METER' => array(10000, 'ha m'),
'HECTOLITER' => array(0.1, 'hl'),
'HOGSHEAD' => array(0.28640367, 'hhd'),
'HOGSHEAD_US' => array(0.2384809434, 'hhd'),
'JIGGER' => array(array('' => 0.0037854118, '/' => 128, '*' => 1.5), 'jigger'),
'KILOLITER' => array(1, 'kl'),
'LITER' => array(0.001, 'l'),
'MEASURE' => array(0.0077, 'measure'),
'MEGALITER' => array(1000, 'Ml'),
'MICROLITER' => array(1.0e-9, 'µl'),
'MILLILITER' => array(0.000001, 'ml'),
'MINIM' => array(array('' => 0.00454609, '/' => 76800), 'min'),
'MINIM_US' => array(array('' => 0.0037854118,'/' => 61440), 'min'),
'OUNCE' => array(array('' => 0.00454609, '/' => 160), 'oz'),
'OUNCE_US' => array(array('' => 0.0037854118, '/' => 128), 'oz'),
'PECK' => array(0.00909218, 'pk'),
'PECK_US' => array(0.0088097676, 'pk'),
'PINT' => array(array('' => 0.00454609, '/' => 8), 'pt'),
'PINT_US_DRY' => array(array('' => 0.0044048838, '/' => 8), 'pt'),
'PINT_US' => array(array('' => 0.0037854118, '/' => 8), 'pt'),
'PIPE' => array(0.49097772, 'pipe'),
'PIPE_US' => array(0.4769618868, 'pipe'),
'PONY' => array(array('' => 0.0037854118, '/' => 128), 'pony'),
'QUART_GERMANY' => array(0.00114504, 'qt'),
'QUART_ANCIENT' => array(0.00108, 'qt'),
'QUART' => array(array('' => 0.00454609, '/' => 4), 'qt'),
'QUART_US_DRY' => array(array('' => 0.0044048838, '/' => 4), 'qt'),
'QUART_US' => array(array('' => 0.0037854118, '/' => 4), 'qt'),
'QUART_UK' => array(0.29094976, 'qt'),
'SHOT' => array(array('' => 0.0037854118, '/' => 128), 'shot'),
'STERE' => array(1, 'st'),
'TABLESPOON' => array(0.000015, 'tbsp'),
'TABLESPOON_UK' => array(array('' => 0.00454609, '/' => 320), 'tbsp'),
'TABLESPOON_US' => array(array('' => 0.0037854118, '/' => 256), 'tbsp'),
'TEASPOON' => array(0.000005, 'tsp'),
'TEASPOON_UK' => array(array('' => 0.00454609, '/' => 1280), 'tsp'),
'TEASPOON_US' => array(array('' => 0.0037854118, '/' => 768), 'tsp'),
'YARD' => array(array('' => 176.6121729408, '/' => 231), 'yd')
);
private $_Locale = null;
/**
* Zend_Measure_Volume provides an locale aware class for
* conversion and formatting of volume values
*
* Zend_Measure $input can be a locale based input string
* or a value. $locale can be used to define that the
* input is made in a different language than the actual one.
*
* @param integer|string $value Value as string, integer, real or float
* @param string $type OPTIONAL A Zend_Measure_Volume Type
* @param string|Zend_Locale $locale OPTIONAL Locale for parsing numbers
* @throws Zend_Measure_Exception
*/
public function __construct($value, $type = null, $locale = null)
{
$this->setValue($value, $type, $locale);
}
/**
* Compare if the value and type is equal
*
* @param Zend_Measure_Volume $object Volume object to compare
* @return boolean
*/
public function equals($object)
{
if ($object->toString() == $this->toString()) {
return true;
}
return false;
}
/**
* Set a new value
*
* @param integer|string $value Value as string, integer, real or float
* @param string $type OPTIONAL A Zend_Measure_Volume Type
* @param string|Zend_Locale $locale OPTIONAL Locale for parsing numbers
* @throws Zend_Measure_Exception
*/
public function setValue($value, $type = null, $locale = null)
{
if ($locale === null) {
$locale = $this->_Locale;
}
if (!$locale = Zend_Locale::isLocale($locale, true)) {
throw new Zend_Measure_Exception("language ($locale) is a unknown language");
}
if ($type === null) {
$type = self::STANDARD;
}
try {
$value = Zend_Locale_Format::getNumber($value, $locale);
} catch(Exception $e) {
throw new Zend_Measure_Exception($e->getMessage());
}
if (empty( self::$_UNITS[$type] )) {
throw new Zend_Measure_Exception("type ($type) is a unknown volume");
}
parent::setValue($value, $type, $locale);
parent::setType($type);
}
/**
* Set a new type, and convert the value
*
* @param string $type New type to set
* @throws Zend_Measure_Exception
*/
public function setType($type)
{
if (empty( self::$_UNITS[$type] )) {
throw new Zend_Measure_Exception("type ($type) is a unknown volume");
}
// Convert to standard value
$value = parent::getValue();
if (is_array(self::$_UNITS[parent::getType()][0])) {
foreach (self::$_UNITS[parent::getType()][0] as $key => $found) {
switch ( $key ) {
case "/":
$value /= $found;
break;
default:
$value *= $found;
break;
}
}
} else {
$value = $value * (self::$_UNITS[parent::getType()][0]);
}
// Convert to expected value
if (is_array(self::$_UNITS[$type][0])) {
foreach (self::$_UNITS[$type][0] as $key => $found) {
switch ( $key ) {
case "/":
$value *= $found;
break;
default:
$value /= $found;
break;
}
}
} else {
$value = $value / (self::$_UNITS[$type][0]);
}
parent::setValue($value, $type, $this->_Locale);
parent::setType( $type );
}
/**
* Returns a string representation
*
* @return string
*/
public function toString()
{
return parent::getValue() . ' ' . self::$_UNITS[parent::getType()][1];
}
/**
* Returns a string representation
*
* @return string
*/
public function __toString()
{
return $this->toString();
}
/**
* Returns the conversion list
*
* @return array
*/
public function getConversionList()
{
return self::$_UNITS;
}
}