@@ 84-105 (lines=22) @@ | ||
81 | * @return double |
|
82 | * |
|
83 | */ |
|
84 | public function convertBaseToStandard($baseUnit, $value) |
|
85 | { |
|
86 | if (!isset($this->config[$this->family]['units'][$baseUnit])) { |
|
87 | throw new UnknownMeasureException( |
|
88 | sprintf( |
|
89 | 'Could not find metric unit "%s" in family "%s"', |
|
90 | $baseUnit, |
|
91 | $this->family |
|
92 | ) |
|
93 | ); |
|
94 | } |
|
95 | $conversionConfig = $this->config[$this->family]['units'][$baseUnit]['convert']; |
|
96 | $convertedValue = $value; |
|
97 | ||
98 | foreach ($conversionConfig as $operation) { |
|
99 | foreach ($operation as $operator => $operand) { |
|
100 | $convertedValue = $this->applyOperation($convertedValue, $operator, $operand); |
|
101 | } |
|
102 | } |
|
103 | ||
104 | return $convertedValue; |
|
105 | } |
|
106 | ||
107 | /** |
|
108 | * Apply operation between value and operand by using operator |
|
@@ 152-174 (lines=23) @@ | ||
149 | * @return double |
|
150 | * |
|
151 | */ |
|
152 | public function convertStandardToResult($finalUnit, $value) |
|
153 | { |
|
154 | if (!isset($this->config[$this->family]['units'][$finalUnit])) { |
|
155 | throw new UnknownMeasureException( |
|
156 | sprintf( |
|
157 | 'Could not find metric unit "%s" in family "%s"', |
|
158 | $finalUnit, |
|
159 | $this->family |
|
160 | ) |
|
161 | ); |
|
162 | } |
|
163 | $conversionConfig = $this->config[$this->family]['units'][$finalUnit]['convert']; |
|
164 | $convertedValue = $value; |
|
165 | ||
166 | // calculate result with conversion config (calculs must be reversed and operation inversed) |
|
167 | foreach (array_reverse($conversionConfig) as $operation) { |
|
168 | foreach ($operation as $operator => $operand) { |
|
169 | $convertedValue = $this->applyReversedOperation($convertedValue, $operator, $operand); |
|
170 | } |
|
171 | } |
|
172 | ||
173 | return $convertedValue; |
|
174 | } |
|
175 | ||
176 | /** |
|
177 | * Apply reversed operation between value and operand by using operator |