<?php
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);
}