class Vehicle
{ private static function fuelTypeMapping(?string $fuelType): ?string { if (is_null($fuelType)) { return null; } return match (Str::lower($fuelType)) { 'p' => 'Petrol', 'd' => 'Diesel', 'e' => 'Electric', 'h' => 'Hybrid', default => $fuelType, }; } }use Illuminate\Support\Str; // Import the Str class if not already imported
// Assuming you have a $fuelType variable or value to pass to the method
$fuelType = 'P'; // Example fuel type
// Call the fuelTypeMapping method and capture the result
$result = Vehicle::fuelTypeMapping($fuelType);
// Handle the output
if (is_null($result)) {
echo "Fuel type is not specified or is null.";
} else {
echo "Mapped fuel type: " . $result;
}
private static function fuelTypeMapping(?string $fuelType): ?string
{
if (is_null($fuelType)) {
return null;
}
$lowercaseFuelType = Str::lower($fuelType);
$mappedFuelType = match ($lowercaseFuelType) {
'p' => 'Petrol',
'd' => 'Diesel',
'e' => 'Electric',
'h' => 'Hybrid',
default => null, // Invalid fuel type, return null or throw an exception
};
if (is_null($mappedFuelType)) {
throw new InvalidArgumentException("Invalid fuel type: {$fuelType}");
}
return $mappedFuelType;
}
No comments:
Post a Comment