forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeedConversion.php
48 lines (45 loc) · 1.37 KB
/
SpeedConversion.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
<?php
/**
* This function converts the submitted
* speed from one unit to another
*
* Conversion explanation taken from
* https://www.asknumbers.com/SpeedConversion.aspx
* Available units for conversion:
* mph -> miles per hour
* km/h -> kilometers per hour
* m/s -> meters per second
* ft/s -> feet per second
* kn -> 1 knot which is equal to 1 nautical mile (1852 km/h)
* The conversion is made using kilometers as base
*
* @param float $speed
* @param string $unitFrom
* @param string $unitTo
* @return int
*/
function convertSpeed(float $speed, string $unitFrom, string $unitTo)
{
$speedUnitsFrom = [
'mph' => 1.609344,
'km/h' => 1,
'm/s'=> 3.6,
'ft/s'=> 1.097,
'kn' => 1.852,
];
$speedUnitsTo = [
'mph' => 0.6213712,
'km/h' => 1,
'm/s'=> 0.277778,
'ft/s'=> 0.911344,
'kn' => 0.539957,
];
$availableUnits = array_keys($speedUnitsFrom);
if (!is_numeric($speed)) {
throw new \Exception("Please pass a valid speed number for converting it from one unit to another.");
}
if (!in_array($unitFrom, $availableUnits) || !in_array($unitTo, $availableUnits)) {
throw new \Exception("Please pass a valid speed unit.\n\nAvailable units: " . implode(', ', $availableUnits));
}
return round($speed * $speedUnitsFrom[$unitFrom] * $speedUnitsTo[$unitTo], 2);
}