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

159 lines
5.0 KiB
PHP
Raw Normal View History

<?php
/**
* AmountFormat.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://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;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* Contains all amount formatting routines.
*/
class AmountFormat extends Twig_Extension
{
/**
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(),
];
}
/**
* @return Twig_SimpleFilter
*/
protected function formatAmount(): Twig_SimpleFilter
{
return new Twig_SimpleFilter(
2017-11-15 10:52:29 +01:00
'formatAmount',
function (string $string): string {
$currency = app('amount')->getDefaultCurrency();
2017-11-15 10:52:29 +01:00
return app('amount')->formatAnything($currency, $string, true);
},
['is_safe' => ['html']]
);
}
/**
* Will format the amount by the currency related to the given account.
*
* @return Twig_SimpleFunction
*/
protected function formatAmountByAccount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
2017-11-15 10:52:29 +01:00
'formatAmountByAccount',
function (AccountModel $account, string $amount, bool $coloured = true): string {
2018-03-30 22:44:37 +02:00
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
/** @var CurrencyRepositoryInterface $currencyRepos */
$currencyRepos = app(CurrencyRepositoryInterface::class);
$currency = app('amount')->getDefaultCurrency();
$currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id');
2018-03-30 22:49:46 +02:00
$accountCurrency = null;
2017-11-15 12:25:49 +01:00
if (0 !== $currencyId) {
2018-03-30 22:44:37 +02:00
$accountCurrency = $currencyRepos->findNull($currencyId);
}
if (null !== $accountCurrency) {
$currency = $accountCurrency;
2017-11-15 10:52:29 +01:00
}
2017-06-05 07:03:32 +02:00
2017-11-15 10:52:29 +01:00
return app('amount')->formatAnything($currency, $amount, $coloured);
},
['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.
*
* @return Twig_SimpleFunction
*/
2017-09-09 06:41:45 +02:00
protected function formatAmountByCurrency(): Twig_SimpleFunction
2017-08-23 21:21:42 +02:00
{
return new Twig_SimpleFunction(
2017-11-15 10:52:29 +01:00
'formatAmountByCurrency',
function (TransactionCurrency $currency, string $amount, bool $coloured = true): string {
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.
*
* @return Twig_SimpleFunction
*/
2017-09-09 06:41:45 +02:00
protected function formatAmountBySymbol(): Twig_SimpleFunction
2017-06-06 07:18:09 +02:00
{
return new Twig_SimpleFunction(
2017-11-15 10:52:29 +01:00
'formatAmountBySymbol',
function (string $amount, string $symbol, int $decimalPlaces = 2, bool $coloured = true): string {
$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']]
);
}
/**
* @return Twig_SimpleFilter
*/
protected function formatAmountPlain(): Twig_SimpleFilter
{
return new Twig_SimpleFilter(
2017-11-15 10:52:29 +01:00
'formatAmountPlain',
function (string $string): string {
$currency = app('amount')->getDefaultCurrency();
2017-11-15 10:52:29 +01:00
return app('amount')->formatAnything($currency, $string, false);
},
['is_safe' => ['html']]
);
}
2017-07-07 08:09:42 +02:00
}