1 | <?php |
||
7 | abstract class Quantity { |
||
8 | |||
9 | // The sub class must set these, since every Quantity has different units |
||
10 | // const BASE_UNIT = 'l'; |
||
11 | |||
12 | // static public $default_unit = self::BASE_UNIT; |
||
13 | // static public $units = array(); |
||
14 | |||
15 | /** |
||
16 | * Convert without the bother of an object |
||
17 | */ |
||
18 | static public function convert( $amount, $fromUnit, $toUnit ) { |
||
22 | |||
23 | /** |
||
24 | * Throw a ConversionException if the given unit isn't valid for the called class |
||
25 | */ |
||
26 | static public function validateUnit( &$unit ) { |
||
32 | |||
33 | /** |
||
34 | * Check if the given unit is valid for the called class |
||
35 | */ |
||
36 | static public function validUnit( &$unit ) { |
||
48 | |||
49 | |||
50 | |||
51 | public $original_unit = ''; |
||
52 | public $unit = ''; |
||
53 | public $amount = 0; |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | */ |
||
58 | public function __construct( $amount, $unit = null, $convertToDefault = true ) { |
||
75 | |||
76 | /** |
||
77 | * Convert object amount to another unit, and return, don't save |
||
78 | */ |
||
79 | public function to( $toUnit ) { |
||
84 | |||
85 | /** |
||
86 | * The convertor heart, that every sub class needs |
||
87 | */ |
||
88 | abstract protected function convertor( $toUnit ); |
||
89 | |||
90 | /** |
||
91 | * The default convertor, using the Quantity's conversion table |
||
92 | */ |
||
93 | protected function convertUsingTable( $toUnit ) { |
||
108 | |||
109 | /** |
||
110 | * Advanded convertor, using unit-base-unit methods |
||
111 | */ |
||
112 | protected function convertUsingMethods( $toUnit ) { |
||
125 | |||
126 | /** |
||
127 | * Convert the object to another standard unit |
||
128 | */ |
||
129 | public function convertTo( $unit ) { |
||
133 | |||
134 | /** |
||
135 | * Return this quantity in all known units |
||
136 | */ |
||
137 | public function all() { |
||
149 | |||
150 | } |
||
151 |