Files
mautic-marketing-program-bu…/Twig/PlanExtension.php
T
2026-11-07 19:31:49 +01:00

39 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\PlanBundle\Twig;
use MauticPlugin\PlanBundle\Service\CurrencyProvider;
use MauticPlugin\PlanBundle\Service\ItemTypeProvider;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Twig functions for the plan templates (kind: "description" or "allocation"):
* plan_item_type_name(kind, key) localized type name, or null for an unknown key
* plan_item_type_description(kind, key) localized type description ('' when none)
* plan_item_type_icon(kind, key) the type's pictogram (Remix Icon class), or null
* plan_currency() the general currency code, e.g. EUR
* plan_money(amount) an amount with 2 decimals, e.g. 12,500.00
*/
class PlanExtension extends AbstractExtension
{
public function __construct(
private ItemTypeProvider $typeProvider,
private CurrencyProvider $currencyProvider,
) {
}
public function getFunctions(): array
{
return [
new TwigFunction('plan_item_type_name', fn (string $kind, ?string $key): ?string => $this->typeProvider->getName($kind, $key)),
new TwigFunction('plan_item_type_description', fn (string $kind, ?string $key): ?string => $this->typeProvider->getDescription($kind, $key)),
new TwigFunction('plan_item_type_icon', fn (string $kind, ?string $key): ?string => $this->typeProvider->getIcon($kind, $key)),
new TwigFunction('plan_currency', fn (): string => $this->currencyProvider->getCurrency()),
new TwigFunction('plan_money', fn ($amount): string => number_format((float) $amount, 2)),
];
}
}