46 lines
1.0 KiB
PHP
46 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\BusProNet\Model\Service;
|
|
|
|
/**
|
|
* Formats service labels for form and UI display.
|
|
*/
|
|
final class ServiceLabelFormatter
|
|
{
|
|
public function formatServiceLabel(Service $service): string
|
|
{
|
|
$label = $service->label ?? '';
|
|
|
|
if (null === $service->price) {
|
|
return $label;
|
|
}
|
|
|
|
if (0.0 === $service->price) {
|
|
return sprintf('%s (inkl.)', $label);
|
|
}
|
|
|
|
if ($service->price < 0) {
|
|
return sprintf('%s (-%s€ Rabatt)', $label, number_format(abs($service->price), 2, ',', '.'));
|
|
}
|
|
|
|
return sprintf('%s (€%s)', $label, number_format($service->price, 2, ',', '.'));
|
|
}
|
|
|
|
/**
|
|
* @param array<int, Service> $services
|
|
*/
|
|
public function formatServiceLabelForServices(string $fallbackLabel, array $services): string
|
|
{
|
|
$service = reset($services);
|
|
if (false === $service) {
|
|
return $fallbackLabel;
|
|
}
|
|
|
|
return $this->formatServiceLabel($service);
|
|
}
|
|
}
|