firefly-iii/app/Support/Twig/Journal.php
2016-03-03 08:31:31 +01:00

99 lines
2.4 KiB
PHP

<?php
declare(strict_types = 1);
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\TransactionJournal;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* Class Journal
*
* @package FireflyIII\Support\Twig
*/
class Journal extends Twig_Extension
{
/**
* @return array
*/
public function getFilters(): array
{
$filters = [$this->typeIcon()];
return $filters;
}
/**
* @return array
*/
public function getFunctions(): array
{
$functions = [
$this->invalidJournal(),
];
return $functions;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName(): string
{
return 'FireflyIII\Support\Twig\Journals';
}
/**
* @return Twig_SimpleFunction
*/
protected function invalidJournal(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'invalidJournal', function (TransactionJournal $journal): bool {
if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) {
return true;
}
return false;
}
);
}
/**
* @return Twig_SimpleFilter
*/
protected function typeIcon(): Twig_SimpleFilter
{
return new Twig_SimpleFilter(
'typeIcon', function (TransactionJournal $journal): string {
switch (true) {
case $journal->isWithdrawal():
$txt = '<i class="fa fa-long-arrow-left fa-fw" title="' . trans('firefly.withdrawal') . '"></i>';
break;
case $journal->isDeposit():
$txt = '<i class="fa fa-long-arrow-right fa-fw" title="' . trans('firefly.deposit') . '"></i>';
break;
case $journal->isTransfer():
$txt = '<i class="fa fa-fw fa-exchange" title="' . trans('firefly.transfer') . '"></i>';
break;
case $journal->isOpeningBalance():
$txt = '<i class="fa-fw fa fa-ban" title="' . trans('firefly.openingBalance') . '"></i>';
break;
default:
$txt = '';
break;
}
return $txt;
}, ['is_safe' => ['html']]
);
}
}