2017-06-04 13:39:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AmountFormat.php
|
2020-02-16 13:56:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2017-06-04 13:39:16 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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/>.
|
2017-06-04 13:39:16 +02:00
|
|
|
*/
|
|
|
|
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;
|
2017-06-04 13:39:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains all amount formatting routines.
|
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
class AmountFormat extends AbstractExtension
|
2017-06-04 13:39:16 +02:00
|
|
|
{
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* {@inheritdoc}
|
2017-06-04 13:39:16 +02:00
|
|
|
*/
|
|
|
|
public function getFilters(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
$this->formatAmount(),
|
|
|
|
$this->formatAmountPlain(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-28 09:44:56 +01:00
|
|
|
* @return TwigFilter
|
2017-06-04 13:39:16 +02:00
|
|
|
*/
|
2019-12-28 09:44:56 +01:00
|
|
|
protected function formatAmount(): TwigFilter
|
2017-06-04 13:39:16 +02:00
|
|
|
{
|
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-06-04 23:39:26 +02:00
|
|
|
|
2017-11-15 10:52:29 +01:00
|
|
|
return app('amount')->formatAnything($currency, $string, true);
|
|
|
|
},
|
|
|
|
['is_safe' => ['html']]
|
2017-06-04 13:39:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-21 09:15:40 +01:00
|
|
|
* @return TwigFilter
|
2017-06-04 13:39:16 +02:00
|
|
|
*/
|
2021-03-21 09:15:40 +01:00
|
|
|
protected function formatAmountPlain(): TwigFilter
|
2017-06-04 13:39:16 +02:00
|
|
|
{
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:00:29 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getFunctions(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
$this->formatAmountByAccount(),
|
|
|
|
$this->formatAmountBySymbol(),
|
|
|
|
$this->formatAmountByCurrency(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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
|
2022-10-30 11:43:17 +01:00
|
|
|
* TODO remove me when layout v1 is deprecated.
|
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-16 21:38:35 +02:00
|
|
|
|
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']]
|
2017-06-04 13:39:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-21 09:15:40 +01:00
|
|
|
* Will format the amount by the currency related to the given account.
|
|
|
|
*
|
|
|
|
* @return TwigFunction
|
2017-06-04 13:39:16 +02:00
|
|
|
*/
|
2021-03-21 09:15:40 +01:00
|
|
|
protected function formatAmountByCurrency(): TwigFunction
|
2017-06-04 13:39:16 +02:00
|
|
|
{
|
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;
|
2017-06-04 23:39:26 +02:00
|
|
|
|
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-06-04 23:39:26 +02:00
|
|
|
);
|
|
|
|
}
|
2017-07-07 08:09:42 +02:00
|
|
|
}
|