Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
1009 / 1009 |
|
100.00% |
22 / 22 |
CRAP | |
100.00% |
1 / 1 |
| Measurement | |
100.00% |
1009 / 1009 |
|
100.00% |
22 / 22 |
405 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| convertTemperature | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
10 | |||
| normalizeTemperature | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
10 | |||
| convertWeight | |
100.00% |
44 / 44 |
|
100.00% |
1 / 1 |
16 | |||
| normalizeWeight | |
100.00% |
43 / 43 |
|
100.00% |
1 / 1 |
16 | |||
| convertLength | |
100.00% |
68 / 68 |
|
100.00% |
1 / 1 |
24 | |||
| normalizeLength | |
100.00% |
67 / 67 |
|
100.00% |
1 / 1 |
24 | |||
| convertArea | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
15 | |||
| normalizeArea | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
15 | |||
| convertVolume | |
100.00% |
116 / 116 |
|
100.00% |
1 / 1 |
40 | |||
| normalizeVolume | |
100.00% |
115 / 115 |
|
100.00% |
1 / 1 |
40 | |||
| convertSpeed | |
100.00% |
104 / 104 |
|
100.00% |
1 / 1 |
36 | |||
| normalizeSpeed | |
100.00% |
103 / 103 |
|
100.00% |
1 / 1 |
36 | |||
| convertTime | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
11 | |||
| normalizeTime | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
11 | |||
| convertAngle | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
12 | |||
| normalizeAngle | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
12 | |||
| convertPressure | |
100.00% |
40 / 40 |
|
100.00% |
1 / 1 |
15 | |||
| normalizePressure | |
100.00% |
39 / 39 |
|
100.00% |
1 / 1 |
15 | |||
| convertEnergy | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
11 | |||
| normalizeEnergy | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
11 | |||
| convertFileSize | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
12 | |||
| normalizeFileSize | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\Converter |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\Converter; |
| 16 | |
| 17 | /** |
| 18 | * Measurement converter. |
| 19 | * |
| 20 | * @package phpOMS\Utils\Converter |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | final class Measurement |
| 26 | { |
| 27 | /** |
| 28 | * Constructor. |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | * @codeCoverageIgnore |
| 32 | */ |
| 33 | private function __construct() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Convert temperature. |
| 39 | * |
| 40 | * @param float $value Value to convert |
| 41 | * @param string $from Input temperature |
| 42 | * @param string $to Output temperature |
| 43 | * |
| 44 | * @return float |
| 45 | * |
| 46 | * @throws \InvalidArgumentException |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public static function convertTemperature(float $value, string $from = TemperatureType::FAHRENHEIT, string $to = TemperatureType::CELSIUS) : float |
| 51 | { |
| 52 | // to kelving |
| 53 | $value = self::normalizeTemperature($value, $from); |
| 54 | |
| 55 | switch ($to) { |
| 56 | case TemperatureType::KELVIN: |
| 57 | break; |
| 58 | case TemperatureType::CELSIUS: |
| 59 | $value -= 273.15; |
| 60 | break; |
| 61 | case TemperatureType::FAHRENHEIT: |
| 62 | $value = (($value - 273.15) * 1.8) + 32; |
| 63 | break; |
| 64 | case TemperatureType::REAUMUR: |
| 65 | $value = ($value - 273.15) * 0.8; |
| 66 | break; |
| 67 | case TemperatureType::RANKINE: |
| 68 | $value = ($value - 273.15) * 1.8 + 491.67; |
| 69 | break; |
| 70 | case TemperatureType::DELISLE: |
| 71 | $value = ($value - 273.15) * 1.5 - 100; |
| 72 | break; |
| 73 | case TemperatureType::NEWTON: |
| 74 | $value = ($value - 273.15) * 0.33; |
| 75 | break; |
| 76 | case TemperatureType::ROMER: |
| 77 | $value = ($value - 273.15) * 0.525 + 7.5; |
| 78 | break; |
| 79 | default: |
| 80 | throw new \InvalidArgumentException('Temperature not supported'); |
| 81 | } |
| 82 | |
| 83 | return $value; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Convert temperature. |
| 88 | * |
| 89 | * @param float $value Value to convert |
| 90 | * @param string $from Input temperature |
| 91 | * |
| 92 | * @return float |
| 93 | * |
| 94 | * @throws \InvalidArgumentException |
| 95 | * |
| 96 | * @since 1.0.0 |
| 97 | */ |
| 98 | private static function normalizeTemperature(float $value, string $from) : float |
| 99 | { |
| 100 | switch ($from) { |
| 101 | case TemperatureType::KELVIN: |
| 102 | break; |
| 103 | case TemperatureType::CELSIUS: |
| 104 | $value += 273.15; |
| 105 | break; |
| 106 | case TemperatureType::FAHRENHEIT: |
| 107 | $value = ($value - 32) / 1.8 + 273.5; |
| 108 | break; |
| 109 | case TemperatureType::REAUMUR: |
| 110 | $value = $value / 0.8 + 273.15; |
| 111 | break; |
| 112 | case TemperatureType::RANKINE: |
| 113 | $value = ($value - 491.67) / 1.8 + 273.15; |
| 114 | break; |
| 115 | case TemperatureType::DELISLE: |
| 116 | $value = ($value + 100) / 1.5 + 273.15; |
| 117 | break; |
| 118 | case TemperatureType::NEWTON: |
| 119 | $value = $value / 0.33 + 273.15; |
| 120 | break; |
| 121 | case TemperatureType::ROMER: |
| 122 | $value = ($value - 7.5) / 0.525 + 273.15; |
| 123 | break; |
| 124 | default: |
| 125 | throw new \InvalidArgumentException('Temperature not supported'); |
| 126 | } |
| 127 | |
| 128 | return $value; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Convert weight. |
| 133 | * |
| 134 | * @param float $value Value to convert |
| 135 | * @param string $from Input weight |
| 136 | * @param string $to Output weight |
| 137 | * |
| 138 | * @return float |
| 139 | * |
| 140 | * @throws \InvalidArgumentException |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | */ |
| 144 | public static function convertWeight(float $value, string $from = WeightType::GRAM, string $to = WeightType::KILOGRAM) : float |
| 145 | { |
| 146 | // to gram |
| 147 | $value = self::normalizeWeight($value, $from); |
| 148 | |
| 149 | switch ($to) { |
| 150 | case WeightType::GRAM: |
| 151 | break; |
| 152 | case WeightType::MICROGRAM: |
| 153 | $value *= 1000000; |
| 154 | break; |
| 155 | case WeightType::MILLIGRAM: |
| 156 | $value *= 1000; |
| 157 | break; |
| 158 | case WeightType::KILOGRAM: |
| 159 | $value /= 1000; |
| 160 | break; |
| 161 | case WeightType::METRIC_TONS: |
| 162 | $value /= 1000000; |
| 163 | break; |
| 164 | case WeightType::STONES: |
| 165 | $value *= 0.15747 * 1000; |
| 166 | break; |
| 167 | case WeightType::OUNCES: |
| 168 | $value *= 0.035274; |
| 169 | break; |
| 170 | case WeightType::POUNDS: |
| 171 | $value *= 0.0022046; |
| 172 | break; |
| 173 | case WeightType::GRAIN: |
| 174 | $value *= 15.432; |
| 175 | break; |
| 176 | case WeightType::CARAT: |
| 177 | $value *= 5.0; |
| 178 | break; |
| 179 | case WeightType::LONG_TONS: |
| 180 | $value *= 9.8420653e-07; |
| 181 | break; |
| 182 | case WeightType::SHORT_TONS: |
| 183 | $value *= 1.1023113e-06; |
| 184 | break; |
| 185 | case WeightType::TROY_POUNDS: |
| 186 | $value *= 0.0026792; |
| 187 | break; |
| 188 | case WeightType::TROY_OUNCES: |
| 189 | $value *= 0.032151; |
| 190 | break; |
| 191 | default: |
| 192 | throw new \InvalidArgumentException('Weight not supported'); |
| 193 | } |
| 194 | |
| 195 | return $value; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Convert weight. |
| 200 | * |
| 201 | * @param float $value Value to convert |
| 202 | * @param string $from Input weight |
| 203 | * |
| 204 | * @return float |
| 205 | * |
| 206 | * @throws \InvalidArgumentException |
| 207 | * |
| 208 | * @since 1.0.0 |
| 209 | */ |
| 210 | private static function normalizeWeight(float $value, string $from) : float |
| 211 | { |
| 212 | switch ($from) { |
| 213 | case WeightType::GRAM: |
| 214 | break; |
| 215 | case WeightType::MICROGRAM: |
| 216 | $value /= 1000000; |
| 217 | break; |
| 218 | case WeightType::MILLIGRAM: |
| 219 | $value /= 1000; |
| 220 | break; |
| 221 | case WeightType::KILOGRAM: |
| 222 | $value *= 1000; |
| 223 | break; |
| 224 | case WeightType::METRIC_TONS: |
| 225 | $value *= 1000000; |
| 226 | break; |
| 227 | case WeightType::STONES: |
| 228 | $value /= 0.15747 * 1000; |
| 229 | break; |
| 230 | case WeightType::OUNCES: |
| 231 | $value /= 0.035274; |
| 232 | break; |
| 233 | case WeightType::POUNDS: |
| 234 | $value /= 0.0022046; |
| 235 | break; |
| 236 | case WeightType::GRAIN: |
| 237 | $value /= 15.432; |
| 238 | break; |
| 239 | case WeightType::CARAT: |
| 240 | $value /= 5.0; |
| 241 | break; |
| 242 | case WeightType::LONG_TONS: |
| 243 | $value /= 9.8420653e-07; |
| 244 | break; |
| 245 | case WeightType::SHORT_TONS: |
| 246 | $value /= 1.1023113e-06; |
| 247 | break; |
| 248 | case WeightType::TROY_POUNDS: |
| 249 | $value /= 0.0026792; |
| 250 | break; |
| 251 | case WeightType::TROY_OUNCES: |
| 252 | $value /= 0.032151; |
| 253 | break; |
| 254 | default: |
| 255 | throw new \InvalidArgumentException('Weight not supported'); |
| 256 | } |
| 257 | |
| 258 | return $value; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Convert length. |
| 263 | * |
| 264 | * @param float $value Value to convert |
| 265 | * @param string $from Input length |
| 266 | * @param string $to Output length |
| 267 | * |
| 268 | * @return float |
| 269 | * |
| 270 | * @throws \InvalidArgumentException |
| 271 | * |
| 272 | * @since 1.0.0 |
| 273 | */ |
| 274 | public static function convertLength(float $value, string $from = LengthType::METERS, string $to = LengthType::KILOMETERS) : float |
| 275 | { |
| 276 | // to meter |
| 277 | $value = self::normalizeLength($value, $from); |
| 278 | |
| 279 | switch ($to) { |
| 280 | case LengthType::METERS: |
| 281 | break; |
| 282 | case LengthType::MILES: |
| 283 | $value *= 0.00062137; |
| 284 | break; |
| 285 | case LengthType::MICROMETER: |
| 286 | $value *= 1000000; |
| 287 | break; |
| 288 | case LengthType::CENTIMETERS: |
| 289 | $value *= 100; |
| 290 | break; |
| 291 | case LengthType::MILLIMETERS: |
| 292 | $value *= 1000; |
| 293 | break; |
| 294 | case LengthType::KILOMETERS: |
| 295 | $value /= 1000; |
| 296 | break; |
| 297 | case LengthType::CHAINS: |
| 298 | $value *= 0.049710; |
| 299 | break; |
| 300 | case LengthType::FEET: |
| 301 | $value *= 3.2808; |
| 302 | break; |
| 303 | case LengthType::FURLONGS: |
| 304 | $value *= 0.0049710; |
| 305 | break; |
| 306 | case LengthType::MICROINCH: |
| 307 | $value *= 39370000; |
| 308 | break; |
| 309 | case LengthType::NANOMETER: |
| 310 | $value *= 1000000000; |
| 311 | break; |
| 312 | case LengthType::INCHES: |
| 313 | $value *= 39.370; |
| 314 | break; |
| 315 | case LengthType::YARDS: |
| 316 | $value *= 1.0936; |
| 317 | break; |
| 318 | case LengthType::PARSECS: |
| 319 | $value *= 3.2407793e-17; |
| 320 | break; |
| 321 | case LengthType::UK_NAUTICAL_MILES: |
| 322 | $value *= 0.00053961; |
| 323 | break; |
| 324 | case LengthType::US_NAUTICAL_MILES: |
| 325 | $value *= 0.00053996; |
| 326 | break; |
| 327 | case LengthType::UK_NAUTICAL_LEAGUES: |
| 328 | $value *= 0.00017987; |
| 329 | break; |
| 330 | case LengthType::UK_LEAGUES: |
| 331 | $value *= 0.00020700; |
| 332 | break; |
| 333 | case LengthType::US_LEAGUES: |
| 334 | $value *= 0.00020712; |
| 335 | break; |
| 336 | case LengthType::NAUTICAL_LEAGUES: |
| 337 | $value *= 0.00017999; |
| 338 | break; |
| 339 | case LengthType::LIGHTYEARS: |
| 340 | $value *= 1.0570008e-16; |
| 341 | break; |
| 342 | case LengthType::DECIMETERS: |
| 343 | $value *= 10; |
| 344 | break; |
| 345 | default: |
| 346 | throw new \InvalidArgumentException('Length not supported'); |
| 347 | } |
| 348 | |
| 349 | return $value; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Convert length. |
| 354 | * |
| 355 | * @param float $value Value to convert |
| 356 | * @param string $from Input length |
| 357 | * |
| 358 | * @return float |
| 359 | * |
| 360 | * @throws \InvalidArgumentException |
| 361 | * |
| 362 | * @since 1.0.0 |
| 363 | */ |
| 364 | private static function normalizeLength(float $value, string $from) : float |
| 365 | { |
| 366 | switch ($from) { |
| 367 | case LengthType::METERS: |
| 368 | break; |
| 369 | case LengthType::MILES: |
| 370 | $value /= 0.00062137; |
| 371 | break; |
| 372 | case LengthType::MICROMETER: |
| 373 | $value /= 1000000; |
| 374 | break; |
| 375 | case LengthType::NANOMETER: |
| 376 | $value /= 1000000000; |
| 377 | break; |
| 378 | case LengthType::CENTIMETERS: |
| 379 | $value /= 100; |
| 380 | break; |
| 381 | case LengthType::MILLIMETERS: |
| 382 | $value /= 1000; |
| 383 | break; |
| 384 | case LengthType::KILOMETERS: |
| 385 | $value *= 1000; |
| 386 | break; |
| 387 | case LengthType::CHAINS: |
| 388 | $value /= 0.049710; |
| 389 | break; |
| 390 | case LengthType::FEET: |
| 391 | $value /= 3.2808; |
| 392 | break; |
| 393 | case LengthType::FURLONGS: |
| 394 | $value /= 0.0049710; |
| 395 | break; |
| 396 | case LengthType::MICROINCH: |
| 397 | $value /= 39370000; |
| 398 | break; |
| 399 | case LengthType::INCHES: |
| 400 | $value /= 39.370; |
| 401 | break; |
| 402 | case LengthType::YARDS: |
| 403 | $value /= 1.0936; |
| 404 | break; |
| 405 | case LengthType::PARSECS: |
| 406 | $value /= 3.2407793e-17; |
| 407 | break; |
| 408 | case LengthType::UK_NAUTICAL_MILES: |
| 409 | $value /= 0.00053961; |
| 410 | break; |
| 411 | case LengthType::US_NAUTICAL_MILES: |
| 412 | $value /= 0.00053996; |
| 413 | break; |
| 414 | case LengthType::UK_NAUTICAL_LEAGUES: |
| 415 | $value /= 0.00017987; |
| 416 | break; |
| 417 | case LengthType::UK_LEAGUES: |
| 418 | $value /= 0.00020700; |
| 419 | break; |
| 420 | case LengthType::US_LEAGUES: |
| 421 | $value /= 0.00020712; |
| 422 | break; |
| 423 | case LengthType::NAUTICAL_LEAGUES: |
| 424 | $value /= 0.00017999; |
| 425 | break; |
| 426 | case LengthType::LIGHTYEARS: |
| 427 | $value /= 1.0570008e-16; |
| 428 | break; |
| 429 | case LengthType::DECIMETERS: |
| 430 | $value /= 10; |
| 431 | break; |
| 432 | default: |
| 433 | throw new \InvalidArgumentException('Length not supported'); |
| 434 | } |
| 435 | |
| 436 | return $value; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Convert area. |
| 441 | * |
| 442 | * @param float $value Value to convert |
| 443 | * @param string $from Input length |
| 444 | * @param string $to Output length |
| 445 | * |
| 446 | * @return float |
| 447 | * |
| 448 | * @throws \InvalidArgumentException |
| 449 | * |
| 450 | * @since 1.0.0 |
| 451 | */ |
| 452 | public static function convertArea(float $value, string $from = AreaType::SQUARE_METERS, string $to = AreaType::SQUARE_KILOMETERS) : float |
| 453 | { |
| 454 | // to square meter |
| 455 | $value = self::normalizeArea($value, $from); |
| 456 | |
| 457 | switch ($to) { |
| 458 | case AreaType::SQUARE_METERS: |
| 459 | break; |
| 460 | case AreaType::SQUARE_FEET: |
| 461 | $value *= 10.764; |
| 462 | break; |
| 463 | case AreaType::SQUARE_KILOMETERS: |
| 464 | $value /= 1000000; |
| 465 | break; |
| 466 | case AreaType::SQUARE_MILES: |
| 467 | $value *= 3.8610216e-07; |
| 468 | break; |
| 469 | case AreaType::SQUARE_YARDS: |
| 470 | $value *= 1.1960; |
| 471 | break; |
| 472 | case AreaType::SQUARE_INCHES: |
| 473 | $value *= 1550; |
| 474 | break; |
| 475 | case AreaType::SQUARE_MICROINCHES: |
| 476 | $value *= 1.550031e+15; |
| 477 | break; |
| 478 | case AreaType::SQUARE_CENTIMETERS: |
| 479 | $value *= 10000; |
| 480 | break; |
| 481 | case AreaType::SQUARE_MILIMETERS: |
| 482 | $value *= 1000000; |
| 483 | break; |
| 484 | case AreaType::SQUARE_MICROMETERS: |
| 485 | $value *= 1000000000000; |
| 486 | break; |
| 487 | case AreaType::SQUARE_DECIMETERS: |
| 488 | $value *= 100; |
| 489 | break; |
| 490 | case AreaType::HECTARES: |
| 491 | $value /= 10000; |
| 492 | break; |
| 493 | case AreaType::ACRES: |
| 494 | $value *= 0.00024711; |
| 495 | break; |
| 496 | default: |
| 497 | throw new \InvalidArgumentException('Area not supported'); |
| 498 | } |
| 499 | |
| 500 | return $value; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Convert area. |
| 505 | * |
| 506 | * @param float $value Value to convert |
| 507 | * @param string $from Input length |
| 508 | * |
| 509 | * @return float |
| 510 | * |
| 511 | * @throws \InvalidArgumentException |
| 512 | * |
| 513 | * @since 1.0.0 |
| 514 | */ |
| 515 | private static function normalizeArea(float $value, string $from) : float |
| 516 | { |
| 517 | switch ($from) { |
| 518 | case AreaType::SQUARE_METERS: |
| 519 | break; |
| 520 | case AreaType::SQUARE_FEET: |
| 521 | $value /= 10.764; |
| 522 | break; |
| 523 | case AreaType::SQUARE_KILOMETERS: |
| 524 | $value *= 1000000; |
| 525 | break; |
| 526 | case AreaType::SQUARE_MILES: |
| 527 | $value /= 3.8610216e-07; |
| 528 | break; |
| 529 | case AreaType::SQUARE_YARDS: |
| 530 | $value /= 1.1960; |
| 531 | break; |
| 532 | case AreaType::SQUARE_INCHES: |
| 533 | $value /= 1550; |
| 534 | break; |
| 535 | case AreaType::SQUARE_MICROINCHES: |
| 536 | $value /= 1.550031e+15; |
| 537 | break; |
| 538 | case AreaType::SQUARE_CENTIMETERS: |
| 539 | $value /= 10000; |
| 540 | break; |
| 541 | case AreaType::SQUARE_MILIMETERS: |
| 542 | $value /= 1000000; |
| 543 | break; |
| 544 | case AreaType::SQUARE_MICROMETERS: |
| 545 | $value /= 1000000000000; |
| 546 | break; |
| 547 | case AreaType::SQUARE_DECIMETERS: |
| 548 | $value /= 100; |
| 549 | break; |
| 550 | case AreaType::HECTARES: |
| 551 | $value *= 10000; |
| 552 | break; |
| 553 | case AreaType::ACRES: |
| 554 | $value /= 0.00024711; |
| 555 | break; |
| 556 | default: |
| 557 | throw new \InvalidArgumentException('Area not supported'); |
| 558 | } |
| 559 | |
| 560 | return $value; |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Convert volume. |
| 565 | * |
| 566 | * @param float $value Value to convert |
| 567 | * @param string $from Input volume |
| 568 | * @param string $to Output volume |
| 569 | * |
| 570 | * @return float |
| 571 | * |
| 572 | * @throws \InvalidArgumentException |
| 573 | * |
| 574 | * @since 1.0.0 |
| 575 | */ |
| 576 | public static function convertVolume(float $value, string $from = VolumeType::LITER, string $to = VolumeType::LITER) : float |
| 577 | { |
| 578 | // to square meter |
| 579 | $value = self::normalizeVolume($value, $from); |
| 580 | |
| 581 | switch ($to) { |
| 582 | case VolumeType::LITER: |
| 583 | break; |
| 584 | case VolumeType::US_GALLON_LIQUID: |
| 585 | $value *= 0.26417; |
| 586 | break; |
| 587 | case VolumeType::UK_GALLON: |
| 588 | $value *= 0.21997; |
| 589 | break; |
| 590 | case VolumeType::US_PINT_LIQUID: |
| 591 | $value *= 2.1134; |
| 592 | break; |
| 593 | case VolumeType::UK_PINT: |
| 594 | $value *= 1.7598; |
| 595 | break; |
| 596 | case VolumeType::CENTILITER: |
| 597 | $value *= 100; |
| 598 | break; |
| 599 | case VolumeType::MILLILITER: |
| 600 | $value *= 1000; |
| 601 | break; |
| 602 | case VolumeType::CUBIC_METER: |
| 603 | $value /= 1000; |
| 604 | break; |
| 605 | case VolumeType::UK_BARREL: |
| 606 | $value *= 0.0061103; |
| 607 | break; |
| 608 | case VolumeType::US_GALLON_DRY: |
| 609 | $value *= 0.22702; |
| 610 | break; |
| 611 | case VolumeType::CUBIC_FEET: |
| 612 | $value *= 0.035315; |
| 613 | break; |
| 614 | case VolumeType::US_QUARTS_LIQUID: |
| 615 | $value *= 1.0567; |
| 616 | break; |
| 617 | case VolumeType::US_QUARTS_DRY: |
| 618 | $value *= 0.90808; |
| 619 | break; |
| 620 | case VolumeType::UK_QUARTS: |
| 621 | $value *= 0.87988; |
| 622 | break; |
| 623 | case VolumeType::US_PINT_DRY: |
| 624 | $value *= 1.8162; |
| 625 | break; |
| 626 | case VolumeType::US_CUP: |
| 627 | $value *= 4.2268; |
| 628 | break; |
| 629 | case VolumeType::CAN_CUP: |
| 630 | $value *= 4.3994; |
| 631 | break; |
| 632 | case VolumeType::METRIC_CUP: |
| 633 | $value *= 4; |
| 634 | break; |
| 635 | case VolumeType::US_GILL: |
| 636 | $value *= 8.4535; |
| 637 | break; |
| 638 | case VolumeType::US_TABLESPOON: |
| 639 | $value *= 67.628; |
| 640 | break; |
| 641 | case VolumeType::UK_TABLESPOON: |
| 642 | $value *= 70.390; |
| 643 | break; |
| 644 | case VolumeType::METRIC_TABLESPOON: |
| 645 | $value *= 66.667; |
| 646 | break; |
| 647 | case VolumeType::US_TEASPOON: |
| 648 | $value *= 202.88; |
| 649 | break; |
| 650 | case VolumeType::UK_TEASPOON: |
| 651 | $value *= 281.56; |
| 652 | break; |
| 653 | case VolumeType::METRIC_TEASPOON: |
| 654 | $value *= 200; |
| 655 | break; |
| 656 | case VolumeType::US_OUNCES: |
| 657 | $value *= 33.814; |
| 658 | break; |
| 659 | case VolumeType::UK_OUNCES: |
| 660 | $value *= 35.195; |
| 661 | break; |
| 662 | case VolumeType::CUBIC_INCH: |
| 663 | $value *= 61.024; |
| 664 | break; |
| 665 | case VolumeType::CUBIC_CENTIMETER: |
| 666 | $value *= 1000; |
| 667 | break; |
| 668 | case VolumeType::CUBIC_MILLIMETER: |
| 669 | $value *= 1000000; |
| 670 | break; |
| 671 | case VolumeType::MICROLITER: |
| 672 | $value *= 1000000; |
| 673 | break; |
| 674 | case VolumeType::KILOLITER: |
| 675 | $value *= 1000; |
| 676 | break; |
| 677 | case VolumeType::UK_GILL: |
| 678 | $value *= 7.0390; |
| 679 | break; |
| 680 | case VolumeType::CUBIC_YARD: |
| 681 | $value *= 0.0013080; |
| 682 | break; |
| 683 | case VolumeType::US_BARREL_DRY: |
| 684 | $value *= 0.0086485; |
| 685 | break; |
| 686 | case VolumeType::US_BARREL_LIQUID: |
| 687 | $value *= 0.0083864; |
| 688 | break; |
| 689 | case VolumeType::US_BARREL_OIL: |
| 690 | $value *= 0.0062898; |
| 691 | break; |
| 692 | case VolumeType::US_BARREL_FEDERAL: |
| 693 | $value *= 0.0085217; |
| 694 | break; |
| 695 | default: |
| 696 | throw new \InvalidArgumentException('Volume not supported'); |
| 697 | } |
| 698 | |
| 699 | return $value; |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Convert volume. |
| 704 | * |
| 705 | * @param float $value Value to convert |
| 706 | * @param string $from Input volume |
| 707 | * |
| 708 | * @return float |
| 709 | * |
| 710 | * @throws \InvalidArgumentException |
| 711 | * |
| 712 | * @since 1.0.0 |
| 713 | */ |
| 714 | private static function normalizeVolume(float $value, string $from) : float |
| 715 | { |
| 716 | switch ($from) { |
| 717 | case VolumeType::LITER: |
| 718 | break; |
| 719 | case VolumeType::US_GALLON_LIQUID: |
| 720 | $value /= 0.26417; |
| 721 | break; |
| 722 | case VolumeType::UK_GALLON: |
| 723 | $value /= 0.21997; |
| 724 | break; |
| 725 | case VolumeType::US_PINT_LIQUID: |
| 726 | $value /= 2.1134; |
| 727 | break; |
| 728 | case VolumeType::UK_PINT: |
| 729 | $value /= 1.7598; |
| 730 | break; |
| 731 | case VolumeType::CENTILITER: |
| 732 | $value /= 100; |
| 733 | break; |
| 734 | case VolumeType::MILLILITER: |
| 735 | $value /= 1000; |
| 736 | break; |
| 737 | case VolumeType::CUBIC_METER: |
| 738 | $value *= 1000; |
| 739 | break; |
| 740 | case VolumeType::UK_BARREL: |
| 741 | $value /= 0.0061103; |
| 742 | break; |
| 743 | case VolumeType::US_GALLON_DRY: |
| 744 | $value /= 0.22702; |
| 745 | break; |
| 746 | case VolumeType::CUBIC_FEET: |
| 747 | $value /= 0.035315; |
| 748 | break; |
| 749 | case VolumeType::US_QUARTS_LIQUID: |
| 750 | $value /= 1.0567; |
| 751 | break; |
| 752 | case VolumeType::US_QUARTS_DRY: |
| 753 | $value /= 0.90808; |
| 754 | break; |
| 755 | case VolumeType::UK_QUARTS: |
| 756 | $value /= 0.87988; |
| 757 | break; |
| 758 | case VolumeType::US_PINT_DRY: |
| 759 | $value /= 1.8162; |
| 760 | break; |
| 761 | case VolumeType::US_CUP: |
| 762 | $value /= 4.2268; |
| 763 | break; |
| 764 | case VolumeType::CAN_CUP: |
| 765 | $value /= 4.3994; |
| 766 | break; |
| 767 | case VolumeType::METRIC_CUP: |
| 768 | $value /= 4; |
| 769 | break; |
| 770 | case VolumeType::US_GILL: |
| 771 | $value /= 8.4535; |
| 772 | break; |
| 773 | case VolumeType::US_TABLESPOON: |
| 774 | $value /= 67.628; |
| 775 | break; |
| 776 | case VolumeType::UK_TABLESPOON: |
| 777 | $value /= 70.390; |
| 778 | break; |
| 779 | case VolumeType::METRIC_TABLESPOON: |
| 780 | $value /= 66.667; |
| 781 | break; |
| 782 | case VolumeType::US_TEASPOON: |
| 783 | $value /= 202.88; |
| 784 | break; |
| 785 | case VolumeType::UK_TEASPOON: |
| 786 | $value /= 281.56; |
| 787 | break; |
| 788 | case VolumeType::METRIC_TEASPOON: |
| 789 | $value /= 200; |
| 790 | break; |
| 791 | case VolumeType::US_OUNCES: |
| 792 | $value /= 33.814; |
| 793 | break; |
| 794 | case VolumeType::UK_OUNCES: |
| 795 | $value /= 35.195; |
| 796 | break; |
| 797 | case VolumeType::CUBIC_INCH: |
| 798 | $value /= 61.024; |
| 799 | break; |
| 800 | case VolumeType::CUBIC_CENTIMETER: |
| 801 | $value /= 1000; |
| 802 | break; |
| 803 | case VolumeType::CUBIC_MILLIMETER: |
| 804 | $value /= 1000000; |
| 805 | break; |
| 806 | case VolumeType::MICROLITER: |
| 807 | $value /= 1000000; |
| 808 | break; |
| 809 | case VolumeType::KILOLITER: |
| 810 | $value /= 1000; |
| 811 | break; |
| 812 | case VolumeType::UK_GILL: |
| 813 | $value /= 7.0390; |
| 814 | break; |
| 815 | case VolumeType::CUBIC_YARD: |
| 816 | $value /= 0.0013080; |
| 817 | break; |
| 818 | case VolumeType::US_BARREL_DRY: |
| 819 | $value /= 0.0086485; |
| 820 | break; |
| 821 | case VolumeType::US_BARREL_LIQUID: |
| 822 | $value /= 0.0083864; |
| 823 | break; |
| 824 | case VolumeType::US_BARREL_OIL: |
| 825 | $value /= 0.0062898; |
| 826 | break; |
| 827 | case VolumeType::US_BARREL_FEDERAL: |
| 828 | $value /= 0.0085217; |
| 829 | break; |
| 830 | default: |
| 831 | throw new \InvalidArgumentException('Volume not supported'); |
| 832 | } |
| 833 | |
| 834 | return $value; |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Convert speed. |
| 839 | * |
| 840 | * @param float $value Value to convert |
| 841 | * @param string $from Input weight |
| 842 | * @param string $to Output weight |
| 843 | * |
| 844 | * @return float |
| 845 | * |
| 846 | * @throws \InvalidArgumentException |
| 847 | * |
| 848 | * @since 1.0.0 |
| 849 | */ |
| 850 | public static function convertSpeed(float $value, string $from = SpeedType::KILOMETERS_PER_HOUR, string $to = SpeedType::KILOMETERS_PER_HOUR) : float |
| 851 | { |
| 852 | // to kph |
| 853 | $value = self::normalizeSpeed($value, $from); |
| 854 | |
| 855 | switch ($to) { |
| 856 | case SpeedType::KILOMETERS_PER_HOUR: |
| 857 | break; |
| 858 | case SpeedType::MILES_PER_DAY: |
| 859 | $value = $value * 24 / 1.60934; |
| 860 | break; |
| 861 | case SpeedType::MILES_PER_HOUR: |
| 862 | $value /= 1.60934; |
| 863 | break; |
| 864 | case SpeedType::MILES_PER_MINUTE: |
| 865 | $value = $value / 60 / 1.60934; |
| 866 | break; |
| 867 | case SpeedType::MILES_PER_SECOND: |
| 868 | $value = $value / 3600 / 1.60934; |
| 869 | break; |
| 870 | case SpeedType::KILOMETERS_PER_DAY: |
| 871 | $value *= 24; |
| 872 | break; |
| 873 | case SpeedType::KILOMETERS_PER_MINUTE: |
| 874 | $value /= 60; |
| 875 | break; |
| 876 | case SpeedType::KILOMETERS_PER_SECOND: |
| 877 | $value /= 3600; |
| 878 | break; |
| 879 | case SpeedType::METERS_PER_DAY: |
| 880 | $value = $value * 24 * 1000; |
| 881 | break; |
| 882 | case SpeedType::METERS_PER_HOUR: |
| 883 | $value *= 1000; |
| 884 | break; |
| 885 | case SpeedType::METERS_PER_MINUTE: |
| 886 | $value = $value / 60 * 1000; |
| 887 | break; |
| 888 | case SpeedType::METERS_PER_SECOND: |
| 889 | $value = $value / 3600 * 1000; |
| 890 | break; |
| 891 | case SpeedType::CENTIMETERS_PER_DAY: |
| 892 | $value = $value * 24 * 100000; |
| 893 | break; |
| 894 | case SpeedType::CENTIMETERS_PER_HOUR: |
| 895 | $value *= 100000; |
| 896 | break; |
| 897 | case SpeedType::CENTIMETERS_PER_MINUTES: |
| 898 | $value = $value / 60 * 100000; |
| 899 | break; |
| 900 | case SpeedType::CENTIMETERS_PER_SECOND: |
| 901 | $value = $value / 3600 * 100000; |
| 902 | break; |
| 903 | case SpeedType::MILLIMETERS_PER_DAY: |
| 904 | $value = $value * 24 * 1000000; |
| 905 | break; |
| 906 | case SpeedType::MILLIMETERS_PER_HOUR: |
| 907 | $value *= 1000000; |
| 908 | break; |
| 909 | case SpeedType::MILLIMETERS_PER_MINUTE: |
| 910 | $value = $value / 60 * 1000000; |
| 911 | break; |
| 912 | case SpeedType::MILLIMETERS_PER_SECOND: |
| 913 | $value = $value / 3600 * 1000000; |
| 914 | break; |
| 915 | case SpeedType::YARDS_PER_DAY: |
| 916 | $value = $value * 24 / 0.0009144; |
| 917 | break; |
| 918 | case SpeedType::YARDS_PER_HOUR: |
| 919 | $value /= 0.0009144; |
| 920 | break; |
| 921 | case SpeedType::YARDS_PER_MINUTE: |
| 922 | $value = $value / 60 / 0.0009144; |
| 923 | break; |
| 924 | case SpeedType::YARDS_PER_SECOND: |
| 925 | $value = $value / 3600 / 0.0009144; |
| 926 | break; |
| 927 | case SpeedType::INCHES_PER_DAY: |
| 928 | $value = $value * 24 / 0.0000254; |
| 929 | break; |
| 930 | case SpeedType::INCHES_PER_HOUR: |
| 931 | $value /= 0.0000254; |
| 932 | break; |
| 933 | case SpeedType::INCHES_PER_MINUTE: |
| 934 | $value = $value / 60 / 0.0000254; |
| 935 | break; |
| 936 | case SpeedType::INCHES_PER_SECOND: |
| 937 | $value = $value / 3600 / 0.0000254; |
| 938 | break; |
| 939 | case SpeedType::FEET_PER_DAY: |
| 940 | $value = $value * 24 / 0.0003048; |
| 941 | break; |
| 942 | case SpeedType::FEET_PER_HOUR: |
| 943 | $value /= 0.0003048; |
| 944 | break; |
| 945 | case SpeedType::FEET_PER_MINUTE: |
| 946 | $value = $value / 60 / 0.0003048; |
| 947 | break; |
| 948 | case SpeedType::FEET_PER_SECOND: |
| 949 | $value = $value / 3600 / 0.0003048; |
| 950 | break; |
| 951 | case SpeedType::MACH: |
| 952 | $value /= 1225.044; |
| 953 | break; |
| 954 | case SpeedType::KNOTS: |
| 955 | $value /= 1.852; |
| 956 | break; |
| 957 | default: |
| 958 | throw new \InvalidArgumentException('Speed not supported'); |
| 959 | } |
| 960 | |
| 961 | return $value; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Convert speed. |
| 966 | * |
| 967 | * @param float $value Value to convert |
| 968 | * @param string $from Input weight |
| 969 | * |
| 970 | * @return float |
| 971 | * |
| 972 | * @throws \InvalidArgumentException |
| 973 | * |
| 974 | * @since 1.0.0 |
| 975 | */ |
| 976 | private static function normalizeSpeed(float $value, string $from) : float |
| 977 | { |
| 978 | switch ($from) { |
| 979 | case SpeedType::KILOMETERS_PER_HOUR: |
| 980 | break; |
| 981 | case SpeedType::MILES_PER_DAY: |
| 982 | $value = $value / 24 * 1.60934; |
| 983 | break; |
| 984 | case SpeedType::MILES_PER_HOUR: |
| 985 | $value *= 1.60934; |
| 986 | break; |
| 987 | case SpeedType::MILES_PER_MINUTE: |
| 988 | $value *= 60 * 1.60934; |
| 989 | break; |
| 990 | case SpeedType::MILES_PER_SECOND: |
| 991 | $value *= 60 * 60 * 1.60934; |
| 992 | break; |
| 993 | case SpeedType::KILOMETERS_PER_DAY: |
| 994 | $value /= 24; |
| 995 | break; |
| 996 | case SpeedType::KILOMETERS_PER_MINUTE: |
| 997 | $value *= 60; |
| 998 | break; |
| 999 | case SpeedType::KILOMETERS_PER_SECOND: |
| 1000 | $value *= 60 * 60; |
| 1001 | break; |
| 1002 | case SpeedType::METERS_PER_DAY: |
| 1003 | $value = $value / 24 / 1000; |
| 1004 | break; |
| 1005 | case SpeedType::METERS_PER_HOUR: |
| 1006 | $value /= 1000; |
| 1007 | break; |
| 1008 | case SpeedType::METERS_PER_MINUTE: |
| 1009 | $value *= 60 / 1000; |
| 1010 | break; |
| 1011 | case SpeedType::METERS_PER_SECOND: |
| 1012 | $value *= 60 * 60 / 1000; |
| 1013 | break; |
| 1014 | case SpeedType::CENTIMETERS_PER_DAY: |
| 1015 | $value = $value / 24 / 100000; |
| 1016 | break; |
| 1017 | case SpeedType::CENTIMETERS_PER_HOUR: |
| 1018 | $value /= 100000; |
| 1019 | break; |
| 1020 | case SpeedType::CENTIMETERS_PER_MINUTES: |
| 1021 | $value *= 60 / 100000; |
| 1022 | break; |
| 1023 | case SpeedType::CENTIMETERS_PER_SECOND: |
| 1024 | $value *= 60 * 60 / 100000; |
| 1025 | break; |
| 1026 | case SpeedType::MILLIMETERS_PER_DAY: |
| 1027 | $value = $value / 24 / 1000000; |
| 1028 | break; |
| 1029 | case SpeedType::MILLIMETERS_PER_HOUR: |
| 1030 | $value /= 1000000; |
| 1031 | break; |
| 1032 | case SpeedType::MILLIMETERS_PER_MINUTE: |
| 1033 | $value *= 60 / 1000000; |
| 1034 | break; |
| 1035 | case SpeedType::MILLIMETERS_PER_SECOND: |
| 1036 | $value *= 60 * 60 / 1000000; |
| 1037 | break; |
| 1038 | case SpeedType::YARDS_PER_DAY: |
| 1039 | $value = $value / 24 * 0.0009144; |
| 1040 | break; |
| 1041 | case SpeedType::YARDS_PER_HOUR: |
| 1042 | $value *= 0.0009144; |
| 1043 | break; |
| 1044 | case SpeedType::YARDS_PER_MINUTE: |
| 1045 | $value *= 60 * 0.0009144; |
| 1046 | break; |
| 1047 | case SpeedType::YARDS_PER_SECOND: |
| 1048 | $value *= 60 * 60 * 0.0009144; |
| 1049 | break; |
| 1050 | case SpeedType::INCHES_PER_DAY: |
| 1051 | $value = $value / 24 * 0.0000254; |
| 1052 | break; |
| 1053 | case SpeedType::INCHES_PER_HOUR: |
| 1054 | $value *= 0.0000254; |
| 1055 | break; |
| 1056 | case SpeedType::INCHES_PER_MINUTE: |
| 1057 | $value *= 60 * 0.0000254; |
| 1058 | break; |
| 1059 | case SpeedType::INCHES_PER_SECOND: |
| 1060 | $value *= 60 * 60 * 0.0000254; |
| 1061 | break; |
| 1062 | case SpeedType::FEET_PER_DAY: |
| 1063 | $value = $value / 24 * 0.0003048; |
| 1064 | break; |
| 1065 | case SpeedType::FEET_PER_HOUR: |
| 1066 | $value *= 0.0003048; |
| 1067 | break; |
| 1068 | case SpeedType::FEET_PER_MINUTE: |
| 1069 | $value *= 60 * 0.0003048; |
| 1070 | break; |
| 1071 | case SpeedType::FEET_PER_SECOND: |
| 1072 | $value *= 60 * 60 * 0.0003048; |
| 1073 | break; |
| 1074 | case SpeedType::MACH: |
| 1075 | $value *= 1225.044; |
| 1076 | break; |
| 1077 | case SpeedType::KNOTS: |
| 1078 | $value *= 1.852; |
| 1079 | break; |
| 1080 | default: |
| 1081 | throw new \InvalidArgumentException('Speed not supported'); |
| 1082 | } |
| 1083 | |
| 1084 | return $value; |
| 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * Convert speed. |
| 1089 | * |
| 1090 | * @param float $value Value to convert |
| 1091 | * @param string $from Input weight |
| 1092 | * @param string $to Output weight |
| 1093 | * |
| 1094 | * @return float |
| 1095 | * |
| 1096 | * @throws \InvalidArgumentException |
| 1097 | * |
| 1098 | * @since 1.0.0 |
| 1099 | */ |
| 1100 | public static function convertTime(float $value, string $from = TimeType::SECONDS, string $to = TimeType::HOURS) : float |
| 1101 | { |
| 1102 | // to seconds |
| 1103 | $value = self::normalizeTime($value, $from); |
| 1104 | |
| 1105 | switch ($to) { |
| 1106 | case TimeType::SECONDS: |
| 1107 | break; |
| 1108 | case TimeType::MINUTES: |
| 1109 | $value /= 60; |
| 1110 | break; |
| 1111 | case TimeType::MILLISECONDS: |
| 1112 | $value *= 1000; |
| 1113 | break; |
| 1114 | case TimeType::HOURS: |
| 1115 | $value *= 3600; |
| 1116 | break; |
| 1117 | case TimeType::DAYS: |
| 1118 | $value *= 3600 * 24; |
| 1119 | break; |
| 1120 | case TimeType::WEEKS: |
| 1121 | $value *= 3600 * 24 * 7; |
| 1122 | break; |
| 1123 | case TimeType::MONTH: |
| 1124 | $value *= 3600 * 24 * 30; |
| 1125 | break; |
| 1126 | case TimeType::QUARTER: |
| 1127 | $value *= 3600 * 24 * 90; |
| 1128 | break; |
| 1129 | case TimeType::YEAR: |
| 1130 | $value *= 3600 * 24 * 365; |
| 1131 | break; |
| 1132 | default: |
| 1133 | throw new \InvalidArgumentException('Size not supported'); |
| 1134 | } |
| 1135 | |
| 1136 | return $value; |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * Convert speed. |
| 1141 | * |
| 1142 | * @param float $value Value to convert |
| 1143 | * @param string $from Input weight |
| 1144 | * |
| 1145 | * @return float |
| 1146 | * |
| 1147 | * @throws \InvalidArgumentException |
| 1148 | * |
| 1149 | * @since 1.0.0 |
| 1150 | */ |
| 1151 | private static function normalizeTime(float $value, string $from) : float |
| 1152 | { |
| 1153 | switch ($from) { |
| 1154 | case TimeType::SECONDS: |
| 1155 | break; |
| 1156 | case TimeType::MINUTES: |
| 1157 | $value *= 60; |
| 1158 | break; |
| 1159 | case TimeType::MILLISECONDS: |
| 1160 | $value /= 1000; |
| 1161 | break; |
| 1162 | case TimeType::HOURS: |
| 1163 | $value /= 3600; |
| 1164 | break; |
| 1165 | case TimeType::DAYS: |
| 1166 | $value /= 3600 * 24; |
| 1167 | break; |
| 1168 | case TimeType::WEEKS: |
| 1169 | $value /= 3600 * 24 * 7; |
| 1170 | break; |
| 1171 | case TimeType::MONTH: |
| 1172 | $value /= 3600 * 24 * 30; |
| 1173 | break; |
| 1174 | case TimeType::QUARTER: |
| 1175 | $value /= 3600 * 24 * 90; |
| 1176 | break; |
| 1177 | case TimeType::YEAR: |
| 1178 | $value /= 3600 * 24 * 365; |
| 1179 | break; |
| 1180 | default: |
| 1181 | throw new \InvalidArgumentException('Size not supported'); |
| 1182 | } |
| 1183 | |
| 1184 | return $value; |
| 1185 | } |
| 1186 | |
| 1187 | /** |
| 1188 | * Convert speed. |
| 1189 | * |
| 1190 | * @param float $value Value to convert |
| 1191 | * @param string $from Input weight |
| 1192 | * @param string $to Output weight |
| 1193 | * |
| 1194 | * @return float |
| 1195 | * |
| 1196 | * @throws \InvalidArgumentException |
| 1197 | * |
| 1198 | * @since 1.0.0 |
| 1199 | */ |
| 1200 | public static function convertAngle(float $value, string $from = AngleType::DEGREE, string $to = AngleType::DEGREE) : float |
| 1201 | { |
| 1202 | // to degree |
| 1203 | $value = self::normalizeAngle($value, $from); |
| 1204 | |
| 1205 | switch ($to) { |
| 1206 | case AngleType::DEGREE: |
| 1207 | break; |
| 1208 | case AngleType::RADIAN: |
| 1209 | $value /= 57.296; |
| 1210 | break; |
| 1211 | case AngleType::SECOND: |
| 1212 | $value /= 0.00027778; |
| 1213 | break; |
| 1214 | case AngleType::MINUTE: |
| 1215 | $value /= 0.016667; |
| 1216 | break; |
| 1217 | case AngleType::MILLIRADIAN_US: |
| 1218 | $value /= 0.09; |
| 1219 | break; |
| 1220 | case AngleType::MILLIRADIAN_UK: |
| 1221 | $value /= 0.057296; |
| 1222 | break; |
| 1223 | case AngleType::MILLIRADIAN_USSR: |
| 1224 | $value /= 0.057143; |
| 1225 | break; |
| 1226 | case AngleType::MILLIRADIAN_NATO: |
| 1227 | $value /= 0.056250; |
| 1228 | break; |
| 1229 | case AngleType::GRADIAN: |
| 1230 | $value /= 0.09; |
| 1231 | break; |
| 1232 | case AngleType::CENTRAD: |
| 1233 | $value /= 0.57296; |
| 1234 | break; |
| 1235 | default: |
| 1236 | throw new \InvalidArgumentException('Angle not supported'); |
| 1237 | } |
| 1238 | |
| 1239 | return $value; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Convert speed. |
| 1244 | * |
| 1245 | * @param float $value Value to convert |
| 1246 | * @param string $from Input weight |
| 1247 | * |
| 1248 | * @return float |
| 1249 | * |
| 1250 | * @throws \InvalidArgumentException |
| 1251 | * |
| 1252 | * @since 1.0.0 |
| 1253 | */ |
| 1254 | private static function normalizeAngle(float $value, string $from) : float |
| 1255 | { |
| 1256 | switch ($from) { |
| 1257 | case AngleType::DEGREE: |
| 1258 | break; |
| 1259 | case AngleType::RADIAN: |
| 1260 | $value *= 57.296; |
| 1261 | break; |
| 1262 | case AngleType::SECOND: |
| 1263 | $value *= 0.00027778; |
| 1264 | break; |
| 1265 | case AngleType::MINUTE: |
| 1266 | $value *= 0.016667; |
| 1267 | break; |
| 1268 | case AngleType::MILLIRADIAN_US: |
| 1269 | $value *= 0.09; |
| 1270 | break; |
| 1271 | case AngleType::MILLIRADIAN_UK: |
| 1272 | $value *= 0.057296; |
| 1273 | break; |
| 1274 | case AngleType::MILLIRADIAN_USSR: |
| 1275 | $value *= 0.057143; |
| 1276 | break; |
| 1277 | case AngleType::MILLIRADIAN_NATO: |
| 1278 | $value *= 0.056250; |
| 1279 | break; |
| 1280 | case AngleType::GRADIAN: |
| 1281 | $value *= 0.09; |
| 1282 | break; |
| 1283 | case AngleType::CENTRAD: |
| 1284 | $value *= 0.57296; |
| 1285 | break; |
| 1286 | default: |
| 1287 | throw new \InvalidArgumentException('Angle not supported'); |
| 1288 | } |
| 1289 | |
| 1290 | return $value; |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * Convert speed. |
| 1295 | * |
| 1296 | * @param float $value Value to convert |
| 1297 | * @param string $from Input weight |
| 1298 | * @param string $to Output weight |
| 1299 | * |
| 1300 | * @return float |
| 1301 | * |
| 1302 | * @throws \InvalidArgumentException |
| 1303 | * |
| 1304 | * @since 1.0.0 |
| 1305 | */ |
| 1306 | public static function convertPressure(float $value, string $from = PressureType::PASCALS, string $to = PressureType::BAR) : float |
| 1307 | { |
| 1308 | // to pascals |
| 1309 | $value = self::normalizePressure($value, $from); |
| 1310 | |
| 1311 | switch ($to) { |
| 1312 | case PressureType::PASCALS: |
| 1313 | break; |
| 1314 | case PressureType::BAR: |
| 1315 | $value /= 100000; |
| 1316 | break; |
| 1317 | case PressureType::POUND_PER_SQUARE_INCH: |
| 1318 | $value *= 0.00014504; |
| 1319 | break; |
| 1320 | case PressureType::ATMOSPHERES: |
| 1321 | $value /= 101325; |
| 1322 | break; |
| 1323 | case PressureType::INCHES_OF_MERCURY: |
| 1324 | $value *= 0.00029530; |
| 1325 | break; |
| 1326 | case PressureType::INCHES_OF_WATER: |
| 1327 | $value *= 0.0040146; |
| 1328 | break; |
| 1329 | case PressureType::MILLIMETERS_OF_WATER: |
| 1330 | $value *= 0.10197; |
| 1331 | break; |
| 1332 | case PressureType::MILLIMETERS_OF_MERCURY: |
| 1333 | $value *= 0.0075006; |
| 1334 | break; |
| 1335 | case PressureType::MILLIBAR: |
| 1336 | $value /= 100; |
| 1337 | break; |
| 1338 | case PressureType::KILOGRAM_PER_SQUARE_METER: |
| 1339 | $value *= 0.10197; |
| 1340 | break; |
| 1341 | case PressureType::NEWTONS_PER_METER_SQUARED: |
| 1342 | break; |
| 1343 | case PressureType::POUNDS_PER_SQUARE_FOOT: |
| 1344 | $value *= 0.020885; |
| 1345 | break; |
| 1346 | case PressureType::TORRS: |
| 1347 | $value *= 0.0075006; |
| 1348 | break; |
| 1349 | default: |
| 1350 | throw new \InvalidArgumentException('Size not supported'); |
| 1351 | } |
| 1352 | |
| 1353 | return $value; |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * Convert speed. |
| 1358 | * |
| 1359 | * @param float $value Value to convert |
| 1360 | * @param string $from Input weight |
| 1361 | * |
| 1362 | * @return float |
| 1363 | * |
| 1364 | * @throws \InvalidArgumentException |
| 1365 | * |
| 1366 | * @since 1.0.0 |
| 1367 | */ |
| 1368 | private static function normalizePressure(float $value, string $from) : float |
| 1369 | { |
| 1370 | switch ($from) { |
| 1371 | case PressureType::PASCALS: |
| 1372 | break; |
| 1373 | case PressureType::BAR: |
| 1374 | $value *= 100000; |
| 1375 | break; |
| 1376 | case PressureType::POUND_PER_SQUARE_INCH: |
| 1377 | $value /= 0.00014504; |
| 1378 | break; |
| 1379 | case PressureType::ATMOSPHERES: |
| 1380 | $value *= 101325; |
| 1381 | break; |
| 1382 | case PressureType::INCHES_OF_MERCURY: |
| 1383 | $value /= 0.00029530; |
| 1384 | break; |
| 1385 | case PressureType::INCHES_OF_WATER: |
| 1386 | $value /= 0.0040146; |
| 1387 | break; |
| 1388 | case PressureType::MILLIMETERS_OF_WATER: |
| 1389 | $value /= 0.10197; |
| 1390 | break; |
| 1391 | case PressureType::MILLIMETERS_OF_MERCURY: |
| 1392 | $value /= 0.0075006; |
| 1393 | break; |
| 1394 | case PressureType::MILLIBAR: |
| 1395 | $value *= 100; |
| 1396 | break; |
| 1397 | case PressureType::KILOGRAM_PER_SQUARE_METER: |
| 1398 | $value /= 0.10197; |
| 1399 | break; |
| 1400 | case PressureType::NEWTONS_PER_METER_SQUARED: |
| 1401 | break; |
| 1402 | case PressureType::POUNDS_PER_SQUARE_FOOT: |
| 1403 | $value /= 0.020885; |
| 1404 | break; |
| 1405 | case PressureType::TORRS: |
| 1406 | $value /= 0.0075006; |
| 1407 | break; |
| 1408 | default: |
| 1409 | throw new \InvalidArgumentException('Size not supported'); |
| 1410 | } |
| 1411 | |
| 1412 | return $value; |
| 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * Convert speed. |
| 1417 | * |
| 1418 | * @param float $value Value to convert |
| 1419 | * @param string $from Input weight |
| 1420 | * @param string $to Output weight |
| 1421 | * |
| 1422 | * @return float |
| 1423 | * |
| 1424 | * @throws \InvalidArgumentException |
| 1425 | * |
| 1426 | * @since 1.0.0 |
| 1427 | */ |
| 1428 | public static function convertEnergy(float $value, string $from = EnergyPowerType::JOULS, string $to = EnergyPowerType::KILOWATT_HOUERS) : float |
| 1429 | { |
| 1430 | $value = self::normalizeEnergy($value, $from); |
| 1431 | |
| 1432 | switch ($to) { |
| 1433 | case EnergyPowerType::JOULS: |
| 1434 | break; |
| 1435 | case EnergyPowerType::KILOWATT_HOUERS: |
| 1436 | $value *= 0.00000027778; |
| 1437 | break; |
| 1438 | case EnergyPowerType::MEGAWATT_HOUERS: |
| 1439 | $value *= 0.00000000027778; |
| 1440 | break; |
| 1441 | case EnergyPowerType::KILOTONS: |
| 1442 | $value *= 0.00000000000023901; |
| 1443 | break; |
| 1444 | case EnergyPowerType::CALORIES: |
| 1445 | $value *= 0.00023885; |
| 1446 | break; |
| 1447 | case EnergyPowerType::BTU: |
| 1448 | $value *= 0.00094782; |
| 1449 | break; |
| 1450 | case EnergyPowerType::KILOJOULS: |
| 1451 | $value *= 0.0010000; |
| 1452 | break; |
| 1453 | case EnergyPowerType::THERMEC: |
| 1454 | $value *= 0.0000000094782; |
| 1455 | break; |
| 1456 | case EnergyPowerType::NEWTON_METERS: |
| 1457 | break; |
| 1458 | default: |
| 1459 | throw new \InvalidArgumentException('Energy not supported'); |
| 1460 | } |
| 1461 | |
| 1462 | return $value; |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * Convert speed. |
| 1467 | * |
| 1468 | * @param float $value Value to convert |
| 1469 | * @param string $from Input weight |
| 1470 | * |
| 1471 | * @return float |
| 1472 | * |
| 1473 | * @throws \InvalidArgumentException |
| 1474 | * |
| 1475 | * @since 1.0.0 |
| 1476 | */ |
| 1477 | private static function normalizeEnergy(float $value, string $from) : float |
| 1478 | { |
| 1479 | switch ($from) { |
| 1480 | case EnergyPowerType::JOULS: |
| 1481 | break; |
| 1482 | case EnergyPowerType::KILOWATT_HOUERS: |
| 1483 | $value /= 0.00000027778; |
| 1484 | break; |
| 1485 | case EnergyPowerType::MEGAWATT_HOUERS: |
| 1486 | $value /= 0.00000000027778; |
| 1487 | break; |
| 1488 | case EnergyPowerType::KILOTONS: |
| 1489 | $value /= 0.00000000000023901; |
| 1490 | break; |
| 1491 | case EnergyPowerType::CALORIES: |
| 1492 | $value /= 0.00023885; |
| 1493 | break; |
| 1494 | case EnergyPowerType::BTU: |
| 1495 | $value /= 0.00094782; |
| 1496 | break; |
| 1497 | case EnergyPowerType::KILOJOULS: |
| 1498 | $value /= 0.0010000; |
| 1499 | break; |
| 1500 | case EnergyPowerType::THERMEC: |
| 1501 | $value /= 0.0000000094782; |
| 1502 | break; |
| 1503 | case EnergyPowerType::NEWTON_METERS: |
| 1504 | break; |
| 1505 | default: |
| 1506 | throw new \InvalidArgumentException('Energy not supported'); |
| 1507 | } |
| 1508 | |
| 1509 | return $value; |
| 1510 | } |
| 1511 | |
| 1512 | /** |
| 1513 | * File size. |
| 1514 | * |
| 1515 | * @param float $value Value to convert |
| 1516 | * @param string $from Input weight |
| 1517 | * @param string $to Output weight |
| 1518 | * |
| 1519 | * @return float |
| 1520 | * |
| 1521 | * @throws \InvalidArgumentException |
| 1522 | * |
| 1523 | * @since 1.0.0 |
| 1524 | */ |
| 1525 | public static function convertFileSize(float $value, string $from = FileSizeType::BYTE, string $to = FileSizeType::MEGABYTE) : float |
| 1526 | { |
| 1527 | // to byte |
| 1528 | $value = self::normalizeFileSize($value, $from); |
| 1529 | |
| 1530 | switch ($to) { |
| 1531 | case FileSizeType::BYTE: |
| 1532 | break; |
| 1533 | case FileSizeType::KILOBYTE: |
| 1534 | $value /= 1000; |
| 1535 | break; |
| 1536 | case FileSizeType::MEGABYTE: |
| 1537 | $value /= 1000000; |
| 1538 | break; |
| 1539 | case FileSizeType::GIGABYTE: |
| 1540 | $value /= 1000000000; |
| 1541 | break; |
| 1542 | case FileSizeType::TERRABYTE: |
| 1543 | $value /= 1000000000000; |
| 1544 | break; |
| 1545 | case FileSizeType::BIT: |
| 1546 | $value *= 8; |
| 1547 | break; |
| 1548 | case FileSizeType::KILOBIT: |
| 1549 | $value *= 0.008; |
| 1550 | break; |
| 1551 | case FileSizeType::MEGABIT: |
| 1552 | $value *= 0.000008; |
| 1553 | break; |
| 1554 | case FileSizeType::GIGABIT: |
| 1555 | $value *= 8e-9; |
| 1556 | break; |
| 1557 | case FileSizeType::TERRABIT: |
| 1558 | $value *= 8e-12; |
| 1559 | break; |
| 1560 | default: |
| 1561 | throw new \InvalidArgumentException('Size not supported'); |
| 1562 | } |
| 1563 | |
| 1564 | return $value; |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * File size. |
| 1569 | * |
| 1570 | * @param float $value Value to convert |
| 1571 | * @param string $from Input weight |
| 1572 | * |
| 1573 | * @return float |
| 1574 | * |
| 1575 | * @throws \InvalidArgumentException |
| 1576 | * |
| 1577 | * @since 1.0.0 |
| 1578 | */ |
| 1579 | private static function normalizeFileSize(float $value, string $from) : float |
| 1580 | { |
| 1581 | switch ($from) { |
| 1582 | case FileSizeType::BYTE: |
| 1583 | break; |
| 1584 | case FileSizeType::KILOBYTE: |
| 1585 | $value *= 1000; |
| 1586 | break; |
| 1587 | case FileSizeType::MEGABYTE: |
| 1588 | $value *= 1000000; |
| 1589 | break; |
| 1590 | case FileSizeType::GIGABYTE: |
| 1591 | $value *= 1000000000; |
| 1592 | break; |
| 1593 | case FileSizeType::TERRABYTE: |
| 1594 | $value *= 1000000000000; |
| 1595 | break; |
| 1596 | case FileSizeType::BIT: |
| 1597 | $value /= 8; |
| 1598 | break; |
| 1599 | case FileSizeType::KILOBIT: |
| 1600 | $value /= 0.008; |
| 1601 | break; |
| 1602 | case FileSizeType::MEGABIT: |
| 1603 | $value /= 0.000008; |
| 1604 | break; |
| 1605 | case FileSizeType::GIGABIT: |
| 1606 | $value /= 8e-9; |
| 1607 | break; |
| 1608 | case FileSizeType::TERRABIT: |
| 1609 | $value /= 8e-12; |
| 1610 | break; |
| 1611 | default: |
| 1612 | throw new \InvalidArgumentException('Size not supported'); |
| 1613 | } |
| 1614 | |
| 1615 | return $value; |
| 1616 | } |
| 1617 | } |