Files
firefly-iii/app/Support/Twig/AmountFormat.php

156 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* AmountFormat.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Account as AccountModel;
use FireflyIII\Models\TransactionCurrency;
2018-03-30 22:44:37 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2019-12-28 09:44:56 +01:00
use Twig\Extension\AbstractExtension;
2021-03-21 09:15:40 +01:00
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* Contains all amount formatting routines.
*/
2019-12-28 09:44:56 +01:00
class AmountFormat extends AbstractExtension
{
/**
2017-11-15 12:25:49 +01:00
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
$this->formatAmount(),
$this->formatAmountPlain(),
];
}
/**
2017-11-15 12:25:49 +01:00
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
2017-08-23 21:21:42 +02:00
$this->formatAmountBySymbol(),
2017-06-06 07:18:09 +02:00
$this->formatAmountByCurrency(),
];
}
/**
2019-12-28 09:44:56 +01:00
* @return TwigFilter
*/
2019-12-28 09:44:56 +01:00
protected function formatAmount(): TwigFilter
{
2019-12-28 09:44:56 +01:00
return new TwigFilter(
2017-11-15 10:52:29 +01:00
'formatAmount',
2019-12-28 09:44:56 +01:00
static function (string $string): string {
2017-11-15 10:52:29 +01:00
$currency = app('amount')->getDefaultCurrency();
2017-11-15 10:52:29 +01:00
return app('amount')->formatAnything($currency, $string, true);
},
['is_safe' => ['html']]
);
}
/**
2021-03-21 09:15:40 +01:00
* @return TwigFilter
*/
2021-03-21 09:15:40 +01:00
protected function formatAmountPlain(): TwigFilter
{
2021-03-21 09:15:40 +01:00
return new TwigFilter(
'formatAmountPlain',
static function (string $string): string {
$currency = app('amount')->getDefaultCurrency();
2017-06-05 07:03:32 +02:00
2021-03-21 09:15:40 +01:00
return app('amount')->formatAnything($currency, $string, false);
2017-11-15 10:52:29 +01:00
},
['is_safe' => ['html']]
2017-06-06 07:18:09 +02:00
);
}
2017-08-23 21:21:42 +02:00
/**
* Will format the amount by the currency related to the given account.
*
2019-12-28 09:44:56 +01:00
* @return TwigFunction
2021-03-21 09:15:40 +01:00
* @deprecated
* TODO remove me because it executes a query in a view.
2017-08-23 21:21:42 +02:00
*/
2021-03-21 09:15:40 +01:00
protected function formatAmountByAccount(): TwigFunction
2017-08-23 21:21:42 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2021-03-21 09:15:40 +01:00
'formatAmountByAccount',
static function (AccountModel $account, string $amount, bool $coloured = null): string {
2018-07-27 05:03:37 +02:00
$coloured = $coloured ?? true;
2021-03-21 09:15:40 +01:00
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
$currency = $accountRepos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency();
2018-07-27 05:03:37 +02:00
2017-11-15 10:52:29 +01:00
return app('amount')->formatAnything($currency, $amount, $coloured);
},
['is_safe' => ['html']]
2017-08-23 21:21:42 +02:00
);
}
2017-06-06 07:18:09 +02:00
/**
* Will format the amount by the currency related to the given account.
*
2019-12-28 09:44:56 +01:00
* @return TwigFunction
2017-06-06 07:18:09 +02:00
*/
2019-12-28 09:44:56 +01:00
protected function formatAmountBySymbol(): TwigFunction
2017-06-06 07:18:09 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2017-11-15 10:52:29 +01:00
'formatAmountBySymbol',
2019-08-12 18:16:12 +02:00
static function (string $amount, string $symbol, int $decimalPlaces = null, bool $coloured = null): string {
2018-07-27 05:03:37 +02:00
$decimalPlaces = $decimalPlaces ?? 2;
$coloured = $coloured ?? true;
2017-11-15 10:52:29 +01:00
$currency = new TransactionCurrency;
$currency->symbol = $symbol;
$currency->decimal_places = $decimalPlaces;
2017-06-06 07:18:09 +02:00
2017-11-15 10:52:29 +01:00
return app('amount')->formatAnything($currency, $amount, $coloured);
},
['is_safe' => ['html']]
);
}
/**
2021-03-21 09:15:40 +01:00
* Will format the amount by the currency related to the given account.
*
* @return TwigFunction
*/
2021-03-21 09:15:40 +01:00
protected function formatAmountByCurrency(): TwigFunction
{
2021-03-21 09:15:40 +01:00
return new TwigFunction(
'formatAmountByCurrency',
static function (TransactionCurrency $currency, string $amount, bool $coloured = null): string {
$coloured = $coloured ?? true;
2021-03-21 09:15:40 +01:00
return app('amount')->formatAnything($currency, $amount, $coloured);
2017-11-15 10:52:29 +01:00
},
['is_safe' => ['html']]
);
}
2017-07-07 08:09:42 +02:00
}