2017-06-04 13:39:16 +02:00
< ? php
2022-12-29 19:42:26 +01:00
2017-06-04 13:39:16 +02:00
/**
* 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 ;
2025-09-04 06:22:04 +02:00
use FireflyIII\Exceptions\FireflyException ;
2017-06-04 13:39:16 +02:00
use FireflyIII\Models\Account as AccountModel ;
use FireflyIII\Models\TransactionCurrency ;
2018-03-30 22:44:37 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface ;
2025-02-07 05:26:03 +01:00
use FireflyIII\Support\Facades\Amount ;
use Illuminate\Support\Facades\Log ;
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 ;
2025-05-27 16:57:36 +02:00
use Override ;
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
{
2025-05-27 16:57:36 +02:00
#[Override]
2017-06-04 13:39:16 +02:00
public function getFilters () : array
{
return [
$this -> formatAmount (),
$this -> formatAmountPlain (),
];
}
2023-05-29 13:56:55 +02:00
protected function formatAmount () : TwigFilter
2017-06-04 13:39:16 +02:00
{
2021-03-21 09:15:40 +01:00
return new TwigFilter (
2023-05-29 13:56:55 +02:00
'formatAmount' ,
2021-03-21 09:15:40 +01:00
static function ( string $string ) : string {
2025-09-04 06:22:04 +02:00
$currency = Amount :: getPrimaryCurrency ();
2017-06-05 07:03:32 +02:00
2025-09-04 06:22:04 +02:00
return Amount :: formatAnything ( $currency , $string , true );
2017-11-15 10:52:29 +01:00
},
[ 'is_safe' => [ 'html' ]]
2017-06-06 07:18:09 +02:00
);
}
2023-06-21 12:34:58 +02:00
protected function formatAmountPlain () : TwigFilter
2017-08-23 21:21:42 +02:00
{
2023-06-21 12:34:58 +02:00
return new TwigFilter (
'formatAmountPlain' ,
static function ( string $string ) : string {
2025-09-04 06:22:04 +02:00
$currency = Amount :: getPrimaryCurrency ();
2018-07-27 05:03:37 +02:00
2025-09-04 06:22:04 +02:00
return Amount :: formatAnything ( $currency , $string , false );
2017-11-15 10:52:29 +01:00
},
[ 'is_safe' => [ 'html' ]]
2017-08-23 21:21:42 +02:00
);
}
2025-05-27 16:57:36 +02:00
#[Override]
2024-02-22 20:11:09 +01:00
public function getFunctions () : array
{
return [
$this -> formatAmountByAccount (),
$this -> formatAmountBySymbol (),
$this -> formatAmountByCurrency (),
2024-12-25 07:13:41 +01:00
$this -> formatAmountByCode (),
2024-02-22 20:11:09 +01:00
];
}
2023-05-29 13:56:55 +02:00
/**
* Will format the amount by the currency related to the given account .
*
2024-01-04 07:44:52 +01:00
* TODO Remove me when v2 hits .
2023-05-29 13:56:55 +02:00
*/
2023-06-21 12:34:58 +02:00
protected function formatAmountByAccount () : TwigFunction
2023-05-29 13:56:55 +02:00
{
return new TwigFunction (
2023-06-21 12:34:58 +02:00
'formatAmountByAccount' ,
2024-03-18 20:25:30 +01:00
static function ( AccountModel $account , string $amount , ? bool $coloured = null ) : string {
2023-12-10 06:45:59 +01:00
$coloured ? ? = true ;
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app ( AccountRepositoryInterface :: class );
2025-09-04 06:22:04 +02:00
$currency = $accountRepos -> getAccountCurrency ( $account ) ? ? Amount :: getPrimaryCurrency ();
2023-05-29 13:56:55 +02:00
2025-09-04 06:22:04 +02:00
return Amount :: formatAnything ( $currency , $amount , $coloured );
2023-05-29 13:56:55 +02:00
},
[ 'is_safe' => [ 'html' ]]
);
}
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
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' ,
2025-09-15 19:20:51 +02:00
static function ( string $amount , ? string $symbol = null , ? int $decimalPlaces = null , ? bool $coloured = null ) : string {
2025-09-04 06:22:04 +02:00
2025-09-07 07:56:10 +02:00
if ( null === $symbol ) {
2025-09-15 19:25:59 +02:00
$message = sprintf ( 'formatAmountBySymbol("%s", %s, %d, %s) was called without a symbol. Please browse to /flush to clear your cache.' , $amount , var_export ( $symbol , true ), $decimalPlaces , var_export ( $coloured , true ));
2025-09-04 06:22:04 +02:00
Log :: error ( $message );
2025-09-15 19:20:51 +02:00
$currency = Amount :: getPrimaryCurrency ();
}
if ( null !== $symbol ) {
2025-09-15 19:25:59 +02:00
$decimalPlaces ? ? = 2 ;
$coloured ? ? = true ;
2025-09-15 19:20:51 +02:00
$currency = new TransactionCurrency ();
$currency -> symbol = $symbol ;
$currency -> decimal_places = $decimalPlaces ;
2025-09-04 06:22:04 +02:00
}
2017-06-06 07:18:09 +02:00
2025-09-04 06:22:04 +02:00
return Amount :: formatAnything ( $currency , $amount , $coloured );
2017-11-15 10:52:29 +01:00
},
[ 'is_safe' => [ 'html' ]]
2017-06-04 13:39:16 +02:00
);
}
/**
2023-06-21 12:34:58 +02:00
* Will format the amount by the currency related to the given account .
2017-06-04 13:39:16 +02:00
*/
2023-06-21 12:34:58 +02:00
protected function formatAmountByCurrency () : TwigFunction
2017-06-04 13:39:16 +02:00
{
2023-06-21 12:34:58 +02:00
return new TwigFunction (
'formatAmountByCurrency' ,
2024-03-18 20:25:30 +01:00
static function ( TransactionCurrency $currency , string $amount , ? bool $coloured = null ) : string {
2023-12-10 06:45:59 +01:00
$coloured ? ? = true ;
2017-06-04 23:39:26 +02:00
2025-09-04 06:22:04 +02:00
return Amount :: formatAnything ( $currency , $amount , $coloured );
2017-11-15 10:52:29 +01:00
},
[ 'is_safe' => [ 'html' ]]
2017-06-04 23:39:26 +02:00
);
}
2025-05-04 17:41:26 +02:00
/**
* Use the code to format a currency .
*/
protected function formatAmountByCode () : TwigFunction
{
// formatAmountByCode
return new TwigFunction (
'formatAmountByCode' ,
static function ( string $amount , string $code , ? bool $coloured = null ) : string {
$coloured ? ? = true ;
2025-09-07 14:49:49 +02:00
try {
$currency = Amount :: getTransactionCurrencyByCode ( $code );
2025-09-07 14:58:46 +02:00
} catch ( FireflyException ) {
2025-08-01 12:31:01 +02:00
Log :: error ( sprintf ( 'Could not find currency with code "%s". Fallback to primary currency.' , $code ));
2025-07-31 20:24:19 +02:00
$currency = Amount :: getPrimaryCurrency ();
2025-05-04 17:41:26 +02:00
Log :: error ( sprintf ( 'Fallback currency is "%s".' , $currency -> code ));
}
2025-09-04 06:22:04 +02:00
return Amount :: formatAnything ( $currency , $amount , $coloured );
2025-05-04 17:41:26 +02:00
},
[ 'is_safe' => [ 'html' ]]
);
}
2017-07-07 08:09:42 +02:00
}