serving_weight; } elseif ($fromUnit === 'serving') { return $amount; } if ( empty($fromUnit) || empty($food->serving_unit) || $food->serving_unit === $fromUnit ) { $multiplier = 1; } elseif ($fromUnit === 'tsp') { $multiplier = match ($food->serving_unit) { 'tbsp' => 1/3, 'cup' => 1/48, default => throw new \DomainException(), }; } elseif ($fromUnit === 'tbsp') { $multiplier = match ($food->serving_unit) { 'tsp' => 3, 'cup' => 1/16, default => throw new \DomainException(), }; } elseif ($fromUnit === 'cup') { $multiplier = match ($food->serving_unit) { 'tsp' => 48, 'tbsp' => 16, default => throw new \DomainException(), }; } else { throw new \DomainException("Unhandled unit combination: {$fromUnit}, {$food->serving_unit} ({$food->name})"); } return $multiplier / $food->serving_size * $amount; } }