← All changes
|
addons/automator/includes/actions/unit-converter-actions.php
+368
-368
8.7.9
→
8.8.4
View file →
| @@ -1,368 +1,368 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Unit Converter Actions | |
| 4 | - * | |
| 5 | - * @package WPbot_Automator | |
| 6 | - */ | |
| 7 | - | |
| 8 | -namespace WPbot_Automator\Actions; | |
| 9 | - | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | - exit; | |
| 12 | -} | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * Class Unit_Converter_Actions | |
| 16 | - */ | |
| 17 | -class Unit_Converter_Actions extends Action { | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * Constructor | |
| 21 | - */ | |
| 22 | - public function __construct() { | |
| 23 | - $this->id = 'unit_converter'; | |
| 24 | - $this->group = __( 'Unit Converter', 'wpbot-automator' ); | |
| 25 | - } | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * Execute the action | |
| 29 | - */ | |
| 30 | - public function execute( $action_data, $trigger_data ) { | |
| 31 | - $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : ''; | |
| 32 | - | |
| 33 | - if ( empty( $action_id ) ) { | |
| 34 | - return false; | |
| 35 | - } | |
| 36 | - | |
| 37 | - $config = isset( $action_data['config'] ) ? $action_data['config'] : array(); | |
| 38 | - $value = isset( $config['value'] ) ? floatval( $this->parse_tokens( $config['value'], $trigger_data ) ) : 0; | |
| 39 | - $from = isset( $config['from'] ) ? $config['from'] : ''; | |
| 40 | - $to = isset( $config['to'] ) ? $config['to'] : ''; | |
| 41 | - | |
| 42 | - switch ( $action_id ) { | |
| 43 | - case 'length_conversion': | |
| 44 | - return $this->convert_length( $value, $from, $to ); | |
| 45 | - case 'weight_conversion': | |
| 46 | - return $this->convert_weight( $value, $from, $to ); | |
| 47 | - case 'temperature_conversion': | |
| 48 | - return $this->convert_temperature( $value, $from, $to ); | |
| 49 | - case 'volume_conversion': | |
| 50 | - return $this->convert_volume( $value, $from, $to ); | |
| 51 | - case 'area_conversion': | |
| 52 | - return $this->convert_area( $value, $from, $to ); | |
| 53 | - case 'speed_conversion': | |
| 54 | - return $this->convert_speed( $value, $from, $to ); | |
| 55 | - case 'pressure_conversion': | |
| 56 | - return $this->convert_pressure( $value, $from, $to ); | |
| 57 | - case 'energy_conversion': | |
| 58 | - return $this->convert_energy( $value, $from, $to ); | |
| 59 | - case 'power_conversion': | |
| 60 | - return $this->convert_power( $value, $from, $to ); | |
| 61 | - case 'data_size_conversion': | |
| 62 | - return $this->convert_data_size( $value, $from, $to ); | |
| 63 | - case 'angle_conversion': | |
| 64 | - return $this->convert_angle( $value, $from, $to ); | |
| 65 | - case 'frequency_conversion': | |
| 66 | - return $this->convert_frequency( $value, $from, $to ); | |
| 67 | - case 'fuel_economy_conversion': | |
| 68 | - return $this->convert_fuel_economy( $value, $from, $to ); | |
| 69 | - default: | |
| 70 | - return false; | |
| 71 | - } | |
| 72 | - } | |
| 73 | - | |
| 74 | - /** | |
| 75 | - * Length Conversion | |
| 76 | - */ | |
| 77 | - private function convert_length( $value, $from, $to ) { | |
| 78 | - $units = array( | |
| 79 | - 'mm' => 0.001, | |
| 80 | - 'cm' => 0.01, | |
| 81 | - 'm' => 1, | |
| 82 | - 'km' => 1000, | |
| 83 | - 'in' => 0.0254, | |
| 84 | - 'ft' => 0.3048, | |
| 85 | - 'yd' => 0.9144, | |
| 86 | - 'mi' => 1609.344, | |
| 87 | - ); | |
| 88 | - | |
| 89 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 90 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 91 | - } | |
| 92 | - | |
| 93 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 94 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 95 | - } | |
| 96 | - | |
| 97 | - /** | |
| 98 | - * Weight Conversion | |
| 99 | - */ | |
| 100 | - private function convert_weight( $value, $from, $to ) { | |
| 101 | - $units = array( | |
| 102 | - 'mg' => 0.000001, | |
| 103 | - 'g' => 0.001, | |
| 104 | - 'kg' => 1, | |
| 105 | - 'ton' => 1000, | |
| 106 | - 'oz' => 0.0283495, | |
| 107 | - 'lb' => 0.453592, | |
| 108 | - ); | |
| 109 | - | |
| 110 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 111 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 112 | - } | |
| 113 | - | |
| 114 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 115 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 116 | - } | |
| 117 | - | |
| 118 | - /** | |
| 119 | - * Temperature Conversion | |
| 120 | - */ | |
| 121 | - private function convert_temperature( $value, $from, $to ) { | |
| 122 | - if ( $from === $to ) { | |
| 123 | - return array( 'success' => true, 'result' => $value ); | |
| 124 | - } | |
| 125 | - | |
| 126 | - // Convert to Celsius first | |
| 127 | - $celsius = 0; | |
| 128 | - if ( $from === 'c' ) { | |
| 129 | - $celsius = $value; | |
| 130 | - } elseif ( $from === 'f' ) { | |
| 131 | - $celsius = ( $value - 32 ) * 5 / 9; | |
| 132 | - } elseif ( $from === 'k' ) { | |
| 133 | - $celsius = $value - 273.15; | |
| 134 | - } | |
| 135 | - | |
| 136 | - // Convert from Celsius to target | |
| 137 | - $result = 0; | |
| 138 | - if ( $to === 'c' ) { | |
| 139 | - $result = $celsius; | |
| 140 | - } elseif ( $to === 'f' ) { | |
| 141 | - $result = ( $celsius * 9 / 5 ) + 32; | |
| 142 | - } elseif ( $to === 'k' ) { | |
| 143 | - $result = $celsius + 273.15; | |
| 144 | - } | |
| 145 | - | |
| 146 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 147 | - } | |
| 148 | - | |
| 149 | - /** | |
| 150 | - * Volume Conversion | |
| 151 | - */ | |
| 152 | - private function convert_volume( $value, $from, $to ) { | |
| 153 | - $units = array( | |
| 154 | - 'ml' => 0.001, | |
| 155 | - 'l' => 1, | |
| 156 | - 'gal' => 3.78541, | |
| 157 | - 'floz' => 0.0295735, | |
| 158 | - 'cup' => 0.236588, | |
| 159 | - 'pt' => 0.473176, | |
| 160 | - 'qt' => 0.946353, | |
| 161 | - ); | |
| 162 | - | |
| 163 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 164 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 165 | - } | |
| 166 | - | |
| 167 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 168 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 169 | - } | |
| 170 | - | |
| 171 | - /** | |
| 172 | - * Area Conversion | |
| 173 | - */ | |
| 174 | - private function convert_area( $value, $from, $to ) { | |
| 175 | - $units = array( | |
| 176 | - 'sqm' => 1, | |
| 177 | - 'sqft' => 0.092903, | |
| 178 | - 'ac' => 4046.86, | |
| 179 | - 'ha' => 10000, | |
| 180 | - 'sqkm' => 1000000, | |
| 181 | - 'sqmi' => 2589988.11, | |
| 182 | - ); | |
| 183 | - | |
| 184 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 185 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 186 | - } | |
| 187 | - | |
| 188 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 189 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 190 | - } | |
| 191 | - | |
| 192 | - /** | |
| 193 | - * Speed Conversion | |
| 194 | - */ | |
| 195 | - private function convert_speed( $value, $from, $to ) { | |
| 196 | - $units = array( | |
| 197 | - 'kmh' => 1 / 3.6, | |
| 198 | - 'mph' => 0.44704, | |
| 199 | - 'ms' => 1, | |
| 200 | - 'knot' => 0.514444, | |
| 201 | - 'fts' => 0.3048, | |
| 202 | - ); | |
| 203 | - | |
| 204 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 205 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 206 | - } | |
| 207 | - | |
| 208 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 209 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 210 | - } | |
| 211 | - | |
| 212 | - /** | |
| 213 | - * Pressure Conversion | |
| 214 | - */ | |
| 215 | - private function convert_pressure( $value, $from, $to ) { | |
| 216 | - $units = array( | |
| 217 | - 'pa' => 1, | |
| 218 | - 'bar' => 100000, | |
| 219 | - 'psi' => 6894.76, | |
| 220 | - 'atm' => 101325, | |
| 221 | - 'torr'=> 133.322, | |
| 222 | - ); | |
| 223 | - | |
| 224 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 225 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 226 | - } | |
| 227 | - | |
| 228 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 229 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 230 | - } | |
| 231 | - | |
| 232 | - /** | |
| 233 | - * Energy Conversion | |
| 234 | - */ | |
| 235 | - private function convert_energy( $value, $from, $to ) { | |
| 236 | - $units = array( | |
| 237 | - 'j' => 1, | |
| 238 | - 'cal' => 4.184, | |
| 239 | - 'kwh' => 3600000, | |
| 240 | - 'btu' => 1055.06, | |
| 241 | - 'ev' => 1.60218e-19, | |
| 242 | - ); | |
| 243 | - | |
| 244 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 245 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 246 | - } | |
| 247 | - | |
| 248 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 249 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 250 | - } | |
| 251 | - | |
| 252 | - /** | |
| 253 | - * Power Conversion | |
| 254 | - */ | |
| 255 | - private function convert_power( $value, $from, $to ) { | |
| 256 | - $units = array( | |
| 257 | - 'w' => 1, | |
| 258 | - 'kw' => 1000, | |
| 259 | - 'hp' => 745.7, | |
| 260 | - 'btuh'=> 0.293071, | |
| 261 | - ); | |
| 262 | - | |
| 263 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 264 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 265 | - } | |
| 266 | - | |
| 267 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 268 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 269 | - } | |
| 270 | - | |
| 271 | - /** | |
| 272 | - * Data Size Conversion | |
| 273 | - */ | |
| 274 | - private function convert_data_size( $value, $from, $to ) { | |
| 275 | - $units = array( | |
| 276 | - 'bit' => 0.125, | |
| 277 | - 'b' => 1, | |
| 278 | - 'kb' => 1024, | |
| 279 | - 'mb' => pow( 1024, 2 ), | |
| 280 | - 'gb' => pow( 1024, 3 ), | |
| 281 | - 'tb' => pow( 1024, 4 ), | |
| 282 | - 'pb' => pow( 1024, 5 ), | |
| 283 | - ); | |
| 284 | - | |
| 285 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 286 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 287 | - } | |
| 288 | - | |
| 289 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 290 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 291 | - } | |
| 292 | - | |
| 293 | - /** | |
| 294 | - * Angle Conversion | |
| 295 | - */ | |
| 296 | - private function convert_angle( $value, $from, $to ) { | |
| 297 | - $units = array( | |
| 298 | - 'deg' => 1, | |
| 299 | - 'rad' => 180 / M_PI, | |
| 300 | - 'grad'=> 0.9, | |
| 301 | - 'min' => 1 / 60, | |
| 302 | - 'sec' => 1 / 3600, | |
| 303 | - ); | |
| 304 | - | |
| 305 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 306 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 307 | - } | |
| 308 | - | |
| 309 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 310 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 311 | - } | |
| 312 | - | |
| 313 | - /** | |
| 314 | - * Frequency Conversion | |
| 315 | - */ | |
| 316 | - private function convert_frequency( $value, $from, $to ) { | |
| 317 | - $units = array( | |
| 318 | - 'hz' => 1, | |
| 319 | - 'khz' => 1000, | |
| 320 | - 'mhz' => 1000000, | |
| 321 | - 'ghz' => 1000000000, | |
| 322 | - 'rpm' => 1 / 60, | |
| 323 | - ); | |
| 324 | - | |
| 325 | - if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 326 | - return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 327 | - } | |
| 328 | - | |
| 329 | - $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 330 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 331 | - } | |
| 332 | - | |
| 333 | - /** | |
| 334 | - * Fuel Economy Conversion | |
| 335 | - */ | |
| 336 | - private function convert_fuel_economy( $value, $from, $to ) { | |
| 337 | - if ( $from === $to ) { | |
| 338 | - return array( 'success' => true, 'result' => $value ); | |
| 339 | - } | |
| 340 | - | |
| 341 | - // Specialized conversion because L/100km is inverse | |
| 342 | - // Convert everything to MPG first | |
| 343 | - $mpg = 0; | |
| 344 | - if ( $from === 'mpg' ) { | |
| 345 | - $mpg = $value; | |
| 346 | - } elseif ( $from === 'l100' ) { | |
| 347 | - $mpg = 235.215 / $value; | |
| 348 | - } elseif ( $from === 'kml' ) { | |
| 349 | - $mpg = $value * 2.35215; | |
| 350 | - } elseif ( $from === 'mpg_imp' ) { | |
| 351 | - $mpg = $value / 1.20095; | |
| 352 | - } | |
| 353 | - | |
| 354 | - // Convert from MPG to target | |
| 355 | - $result = 0; | |
| 356 | - if ( $to === 'mpg' ) { | |
| 357 | - $result = $mpg; | |
| 358 | - } elseif ( $to === 'l100' ) { | |
| 359 | - $result = 235.215 / $mpg; | |
| 360 | - } elseif ( $to === 'kml' ) { | |
| 361 | - $result = $mpg / 2.35215; | |
| 362 | - } elseif ( $to === 'mpg_imp' ) { | |
| 363 | - $result = $mpg * 1.20095; | |
| 364 | - } | |
| 365 | - | |
| 366 | - return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 367 | - } | |
| 368 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Unit Converter Actions | |
| 4 | + * | |
| 5 | + * @package WPbot_Automator | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace WPbot_Automator\Actions; | |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Class Unit_Converter_Actions | |
| 16 | + */ | |
| 17 | +class Unit_Converter_Actions extends Action { | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Constructor | |
| 21 | + */ | |
| 22 | + public function __construct() { | |
| 23 | + $this->id = 'unit_converter'; | |
| 24 | + $this->group = __( 'Unit Converter', 'wpbot-automator' ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Execute the action | |
| 29 | + */ | |
| 30 | + public function execute( $action_data, $trigger_data ) { | |
| 31 | + $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : ''; | |
| 32 | + | |
| 33 | + if ( empty( $action_id ) ) { | |
| 34 | + return false; | |
| 35 | + } | |
| 36 | + | |
| 37 | + $config = isset( $action_data['config'] ) ? $action_data['config'] : array(); | |
| 38 | + $value = isset( $config['value'] ) ? floatval( $this->parse_tokens( $config['value'], $trigger_data ) ) : 0; | |
| 39 | + $from = isset( $config['from'] ) ? $config['from'] : ''; | |
| 40 | + $to = isset( $config['to'] ) ? $config['to'] : ''; | |
| 41 | + | |
| 42 | + switch ( $action_id ) { | |
| 43 | + case 'length_conversion': | |
| 44 | + return $this->convert_length( $value, $from, $to ); | |
| 45 | + case 'weight_conversion': | |
| 46 | + return $this->convert_weight( $value, $from, $to ); | |
| 47 | + case 'temperature_conversion': | |
| 48 | + return $this->convert_temperature( $value, $from, $to ); | |
| 49 | + case 'volume_conversion': | |
| 50 | + return $this->convert_volume( $value, $from, $to ); | |
| 51 | + case 'area_conversion': | |
| 52 | + return $this->convert_area( $value, $from, $to ); | |
| 53 | + case 'speed_conversion': | |
| 54 | + return $this->convert_speed( $value, $from, $to ); | |
| 55 | + case 'pressure_conversion': | |
| 56 | + return $this->convert_pressure( $value, $from, $to ); | |
| 57 | + case 'energy_conversion': | |
| 58 | + return $this->convert_energy( $value, $from, $to ); | |
| 59 | + case 'power_conversion': | |
| 60 | + return $this->convert_power( $value, $from, $to ); | |
| 61 | + case 'data_size_conversion': | |
| 62 | + return $this->convert_data_size( $value, $from, $to ); | |
| 63 | + case 'angle_conversion': | |
| 64 | + return $this->convert_angle( $value, $from, $to ); | |
| 65 | + case 'frequency_conversion': | |
| 66 | + return $this->convert_frequency( $value, $from, $to ); | |
| 67 | + case 'fuel_economy_conversion': | |
| 68 | + return $this->convert_fuel_economy( $value, $from, $to ); | |
| 69 | + default: | |
| 70 | + return false; | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Length Conversion | |
| 76 | + */ | |
| 77 | + private function convert_length( $value, $from, $to ) { | |
| 78 | + $units = array( | |
| 79 | + 'mm' => 0.001, | |
| 80 | + 'cm' => 0.01, | |
| 81 | + 'm' => 1, | |
| 82 | + 'km' => 1000, | |
| 83 | + 'in' => 0.0254, | |
| 84 | + 'ft' => 0.3048, | |
| 85 | + 'yd' => 0.9144, | |
| 86 | + 'mi' => 1609.344, | |
| 87 | + ); | |
| 88 | + | |
| 89 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 90 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 91 | + } | |
| 92 | + | |
| 93 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 94 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Weight Conversion | |
| 99 | + */ | |
| 100 | + private function convert_weight( $value, $from, $to ) { | |
| 101 | + $units = array( | |
| 102 | + 'mg' => 0.000001, | |
| 103 | + 'g' => 0.001, | |
| 104 | + 'kg' => 1, | |
| 105 | + 'ton' => 1000, | |
| 106 | + 'oz' => 0.0283495, | |
| 107 | + 'lb' => 0.453592, | |
| 108 | + ); | |
| 109 | + | |
| 110 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 111 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 112 | + } | |
| 113 | + | |
| 114 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 115 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Temperature Conversion | |
| 120 | + */ | |
| 121 | + private function convert_temperature( $value, $from, $to ) { | |
| 122 | + if ( $from === $to ) { | |
| 123 | + return array( 'success' => true, 'result' => $value ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + // Convert to Celsius first | |
| 127 | + $celsius = 0; | |
| 128 | + if ( $from === 'c' ) { | |
| 129 | + $celsius = $value; | |
| 130 | + } elseif ( $from === 'f' ) { | |
| 131 | + $celsius = ( $value - 32 ) * 5 / 9; | |
| 132 | + } elseif ( $from === 'k' ) { | |
| 133 | + $celsius = $value - 273.15; | |
| 134 | + } | |
| 135 | + | |
| 136 | + // Convert from Celsius to target | |
| 137 | + $result = 0; | |
| 138 | + if ( $to === 'c' ) { | |
| 139 | + $result = $celsius; | |
| 140 | + } elseif ( $to === 'f' ) { | |
| 141 | + $result = ( $celsius * 9 / 5 ) + 32; | |
| 142 | + } elseif ( $to === 'k' ) { | |
| 143 | + $result = $celsius + 273.15; | |
| 144 | + } | |
| 145 | + | |
| 146 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | + * Volume Conversion | |
| 151 | + */ | |
| 152 | + private function convert_volume( $value, $from, $to ) { | |
| 153 | + $units = array( | |
| 154 | + 'ml' => 0.001, | |
| 155 | + 'l' => 1, | |
| 156 | + 'gal' => 3.78541, | |
| 157 | + 'floz' => 0.0295735, | |
| 158 | + 'cup' => 0.236588, | |
| 159 | + 'pt' => 0.473176, | |
| 160 | + 'qt' => 0.946353, | |
| 161 | + ); | |
| 162 | + | |
| 163 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 164 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 165 | + } | |
| 166 | + | |
| 167 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 168 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | + * Area Conversion | |
| 173 | + */ | |
| 174 | + private function convert_area( $value, $from, $to ) { | |
| 175 | + $units = array( | |
| 176 | + 'sqm' => 1, | |
| 177 | + 'sqft' => 0.092903, | |
| 178 | + 'ac' => 4046.86, | |
| 179 | + 'ha' => 10000, | |
| 180 | + 'sqkm' => 1000000, | |
| 181 | + 'sqmi' => 2589988.11, | |
| 182 | + ); | |
| 183 | + | |
| 184 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 185 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 186 | + } | |
| 187 | + | |
| 188 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 189 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 190 | + } | |
| 191 | + | |
| 192 | + /** | |
| 193 | + * Speed Conversion | |
| 194 | + */ | |
| 195 | + private function convert_speed( $value, $from, $to ) { | |
| 196 | + $units = array( | |
| 197 | + 'kmh' => 1 / 3.6, | |
| 198 | + 'mph' => 0.44704, | |
| 199 | + 'ms' => 1, | |
| 200 | + 'knot' => 0.514444, | |
| 201 | + 'fts' => 0.3048, | |
| 202 | + ); | |
| 203 | + | |
| 204 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 205 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 206 | + } | |
| 207 | + | |
| 208 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 209 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 210 | + } | |
| 211 | + | |
| 212 | + /** | |
| 213 | + * Pressure Conversion | |
| 214 | + */ | |
| 215 | + private function convert_pressure( $value, $from, $to ) { | |
| 216 | + $units = array( | |
| 217 | + 'pa' => 1, | |
| 218 | + 'bar' => 100000, | |
| 219 | + 'psi' => 6894.76, | |
| 220 | + 'atm' => 101325, | |
| 221 | + 'torr'=> 133.322, | |
| 222 | + ); | |
| 223 | + | |
| 224 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 225 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 226 | + } | |
| 227 | + | |
| 228 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 229 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 230 | + } | |
| 231 | + | |
| 232 | + /** | |
| 233 | + * Energy Conversion | |
| 234 | + */ | |
| 235 | + private function convert_energy( $value, $from, $to ) { | |
| 236 | + $units = array( | |
| 237 | + 'j' => 1, | |
| 238 | + 'cal' => 4.184, | |
| 239 | + 'kwh' => 3600000, | |
| 240 | + 'btu' => 1055.06, | |
| 241 | + 'ev' => 1.60218e-19, | |
| 242 | + ); | |
| 243 | + | |
| 244 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 245 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 246 | + } | |
| 247 | + | |
| 248 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 249 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 250 | + } | |
| 251 | + | |
| 252 | + /** | |
| 253 | + * Power Conversion | |
| 254 | + */ | |
| 255 | + private function convert_power( $value, $from, $to ) { | |
| 256 | + $units = array( | |
| 257 | + 'w' => 1, | |
| 258 | + 'kw' => 1000, | |
| 259 | + 'hp' => 745.7, | |
| 260 | + 'btuh'=> 0.293071, | |
| 261 | + ); | |
| 262 | + | |
| 263 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 264 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 265 | + } | |
| 266 | + | |
| 267 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 268 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 269 | + } | |
| 270 | + | |
| 271 | + /** | |
| 272 | + * Data Size Conversion | |
| 273 | + */ | |
| 274 | + private function convert_data_size( $value, $from, $to ) { | |
| 275 | + $units = array( | |
| 276 | + 'bit' => 0.125, | |
| 277 | + 'b' => 1, | |
| 278 | + 'kb' => 1024, | |
| 279 | + 'mb' => pow( 1024, 2 ), | |
| 280 | + 'gb' => pow( 1024, 3 ), | |
| 281 | + 'tb' => pow( 1024, 4 ), | |
| 282 | + 'pb' => pow( 1024, 5 ), | |
| 283 | + ); | |
| 284 | + | |
| 285 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 286 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 287 | + } | |
| 288 | + | |
| 289 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 290 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 291 | + } | |
| 292 | + | |
| 293 | + /** | |
| 294 | + * Angle Conversion | |
| 295 | + */ | |
| 296 | + private function convert_angle( $value, $from, $to ) { | |
| 297 | + $units = array( | |
| 298 | + 'deg' => 1, | |
| 299 | + 'rad' => 180 / M_PI, | |
| 300 | + 'grad'=> 0.9, | |
| 301 | + 'min' => 1 / 60, | |
| 302 | + 'sec' => 1 / 3600, | |
| 303 | + ); | |
| 304 | + | |
| 305 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 306 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 307 | + } | |
| 308 | + | |
| 309 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 310 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 311 | + } | |
| 312 | + | |
| 313 | + /** | |
| 314 | + * Frequency Conversion | |
| 315 | + */ | |
| 316 | + private function convert_frequency( $value, $from, $to ) { | |
| 317 | + $units = array( | |
| 318 | + 'hz' => 1, | |
| 319 | + 'khz' => 1000, | |
| 320 | + 'mhz' => 1000000, | |
| 321 | + 'ghz' => 1000000000, | |
| 322 | + 'rpm' => 1 / 60, | |
| 323 | + ); | |
| 324 | + | |
| 325 | + if ( ! isset( $units[ $from ] ) || ! isset( $units[ $to ] ) ) { | |
| 326 | + return array( 'success' => false, 'message' => 'Invalid units.' ); | |
| 327 | + } | |
| 328 | + | |
| 329 | + $result = $value * ( $units[ $from ] / $units[ $to ] ); | |
| 330 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 331 | + } | |
| 332 | + | |
| 333 | + /** | |
| 334 | + * Fuel Economy Conversion | |
| 335 | + */ | |
| 336 | + private function convert_fuel_economy( $value, $from, $to ) { | |
| 337 | + if ( $from === $to ) { | |
| 338 | + return array( 'success' => true, 'result' => $value ); | |
| 339 | + } | |
| 340 | + | |
| 341 | + // Specialized conversion because L/100km is inverse | |
| 342 | + // Convert everything to MPG first | |
| 343 | + $mpg = 0; | |
| 344 | + if ( $from === 'mpg' ) { | |
| 345 | + $mpg = $value; | |
| 346 | + } elseif ( $from === 'l100' ) { | |
| 347 | + $mpg = 235.215 / $value; | |
| 348 | + } elseif ( $from === 'kml' ) { | |
| 349 | + $mpg = $value * 2.35215; | |
| 350 | + } elseif ( $from === 'mpg_imp' ) { | |
| 351 | + $mpg = $value / 1.20095; | |
| 352 | + } | |
| 353 | + | |
| 354 | + // Convert from MPG to target | |
| 355 | + $result = 0; | |
| 356 | + if ( $to === 'mpg' ) { | |
| 357 | + $result = $mpg; | |
| 358 | + } elseif ( $to === 'l100' ) { | |
| 359 | + $result = 235.215 / $mpg; | |
| 360 | + } elseif ( $to === 'kml' ) { | |
| 361 | + $result = $mpg / 2.35215; | |
| 362 | + } elseif ( $to === 'mpg_imp' ) { | |
| 363 | + $result = $mpg * 1.20095; | |
| 364 | + } | |
| 365 | + | |
| 366 | + return array( 'success' => true, 'result' => $result, 'message' => 'Converted successfully.' ); | |
| 367 | + } | |
| 368 | +} | |