. */ declare(strict_types=1); namespace FireflyIII\Support\Twig; use FireflyIII\Models\Account as AccountModel; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; /** * Contains all amount formatting routines. */ class AmountFormat extends AbstractExtension { /** * {@inheritdoc} */ public function getFilters(): array { return [ $this->formatAmount(), $this->formatAmountPlain(), ]; } /** * {@inheritdoc} */ public function getFunctions(): array { return [ $this->formatAmountByAccount(), $this->formatAmountBySymbol(), $this->formatAmountByCurrency(), ]; } /** * @return TwigFilter */ protected function formatAmount(): TwigFilter { return new TwigFilter( 'formatAmount', static function (string $string): string { $currency = app('amount')->getDefaultCurrency(); return app('amount')->formatAnything($currency, $string, true); }, ['is_safe' => ['html']] ); } /** * @return TwigFilter */ protected function formatAmountPlain(): TwigFilter { return new TwigFilter( 'formatAmountPlain', static function (string $string): string { $currency = app('amount')->getDefaultCurrency(); return app('amount')->formatAnything($currency, $string, false); }, ['is_safe' => ['html']] ); } /** * Will format the amount by the currency related to the given account. * * @return TwigFunction * @deprecated * TODO remove me because it executes a query in a view. */ protected function formatAmountByAccount(): TwigFunction { return new TwigFunction( 'formatAmountByAccount', static function (AccountModel $account, string $amount, bool $coloured = null): string { $coloured = $coloured ?? true; /** @var AccountRepositoryInterface $accountRepos */ $accountRepos = app(AccountRepositoryInterface::class); $currency = $accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency(); return app('amount')->formatAnything($currency, $amount, $coloured); }, ['is_safe' => ['html']] ); } /** * Will format the amount by the currency related to the given account. * * @return TwigFunction */ protected function formatAmountBySymbol(): TwigFunction { return new TwigFunction( 'formatAmountBySymbol', static function (string $amount, string $symbol, int $decimalPlaces = null, bool $coloured = null): string { $decimalPlaces = $decimalPlaces ?? 2; $coloured = $coloured ?? true; $currency = new TransactionCurrency; $currency->symbol = $symbol; $currency->decimal_places = $decimalPlaces; return app('amount')->formatAnything($currency, $amount, $coloured); }, ['is_safe' => ['html']] ); } /** * Will format the amount by the currency related to the given account. * * @return TwigFunction */ protected function formatAmountByCurrency(): TwigFunction { return new TwigFunction( 'formatAmountByCurrency', static function (TransactionCurrency $currency, string $amount, bool $coloured = null): string { $coloured = $coloured ?? true; return app('amount')->formatAnything($currency, $amount, $coloured); }, ['is_safe' => ['html']] ); } }