Files
myep/src/Twig/AppRuntime.php
T

97 lines
2.7 KiB
PHP

<?php
namespace App\Twig;
use App\BusProNet\DataProvider\CountryDataProvider;
use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;
use Twig\Extra\Intl\IntlExtension;
class AppRuntime implements RuntimeExtensionInterface
{
public function __construct(
private readonly IntlExtension $intlExtension,
private readonly CountryDataProvider $countryDataProvider,
) {
}
public function fileIconFilter(Environment $environment, string $mimeType, ?string $classes = 'w-4 h-4'): string
{
$icon = match ($mimeType) {
'application/pdf' => 'pdf',
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'excel',
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'word',
default => 'download',
};
return $this->renderIcon($environment, $icon, $classes);
}
public function formatBytes(int $bytes, ?int $precision = 2): string
{
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$precision}f", $bytes / (1024 ** $factor)).@$size[$factor];
}
public function formatMoney(int $amount): string
{
// Amounts are stored as integers so divide by 100 first
$amount = $amount / 100;
return $this->intlExtension->formatCurrency($amount, 'EUR');
}
public function renderIcon(Environment $environment, string $icon, string $classes = 'w-5 h-5'): string
{
return $environment->render('_partials/_icon.html.twig', [
'icon' => $icon,
'class' => $classes,
]);
}
public function mapStatus(string $status): string
{
$status = strtoupper($status);
return match ($status) {
'F', 'F/S' => 'Festbuchung',
'S' => 'Stornierung',
'O' => 'Option',
'U' => 'Umbuchung',
default => '',
};
}
public function mapGender(string $gender): string
{
$gender = strtoupper($gender);
return match ($gender) {
'M' => 'männlich',
'W' => 'weiblich',
'D' => 'divers',
default => '',
};
}
public function mapCountry(?string $country): ?string
{
if (null === $country) {
return null;
}
return $this->countryDataProvider->get($country)?->name;
}
public function mapNationality(?string $nationality): ?string
{
if (null === $nationality) {
return null;
}
return $this->countryDataProvider->get($nationality)?->nationality;
}
}