firefly-iii/app/Support/Twig/Journal.php

110 lines
2.7 KiB
PHP
Raw Normal View History

2015-05-01 15:44:35 -05:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-05-01 15:44:35 -05:00
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\TransactionJournal;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
2015-05-01 15:44:35 -05:00
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
2015-05-02 15:12:26 -05:00
2015-05-01 15:44:35 -05:00
/**
2015-05-02 04:32:45 -05:00
* Class Journal
2015-05-01 15:44:35 -05:00
*
* @package FireflyIII\Support\Twig
*/
2015-05-02 04:32:45 -05:00
class Journal extends Twig_Extension
2015-05-01 15:44:35 -05:00
{
2015-05-02 15:05:18 -05:00
/**
* @return array
*/
2016-02-06 08:01:26 -06:00
public function getFilters(): array
2015-05-01 15:44:35 -05:00
{
2015-06-06 10:40:41 -05:00
$filters = [$this->typeIcon()];
2015-05-01 15:44:35 -05:00
2015-06-06 10:40:41 -05:00
return $filters;
}
/**
* @return array
*/
2016-02-06 08:01:26 -06:00
public function getFunctions(): array
2015-06-06 10:40:41 -05:00
{
$functions = [
$this->invalidJournal(),
];
return $functions;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
2016-02-06 08:01:26 -06:00
public function getName(): string
2015-06-06 10:40:41 -05:00
{
return 'FireflyIII\Support\Twig\Journals';
}
2016-02-06 08:01:26 -06:00
/**
* @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;
}
);
}
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFilter
*/
2016-02-06 08:01:26 -06:00
protected function typeIcon(): Twig_SimpleFilter
2015-06-06 10:40:41 -05:00
{
return new Twig_SimpleFilter(
2016-02-06 08:01:26 -06:00
'typeIcon', function (TransactionJournal $journal): string {
2015-06-03 11:22:47 -05:00
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($journal->id);
$cache->addProperty('typeIcon');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return $cache->get(); // @codeCoverageIgnore
2015-06-03 11:22:47 -05:00
}
switch (true) {
case $journal->isWithdrawal():
2015-06-23 14:14:21 -05:00
$txt = '<i class="fa fa-long-arrow-left fa-fw" title="' . trans('firefly.withdrawal') . '"></i>';
2015-06-03 11:22:47 -05:00
break;
case $journal->isDeposit():
2015-06-23 14:14:21 -05:00
$txt = '<i class="fa fa-long-arrow-right fa-fw" title="' . trans('firefly.deposit') . '"></i>';
2015-06-03 11:22:47 -05:00
break;
case $journal->isTransfer():
2015-06-03 11:22:47 -05:00
$txt = '<i class="fa fa-fw fa-exchange" title="' . trans('firefly.transfer') . '"></i>';
break;
case $journal->isOpeningBalance():
2015-06-23 14:14:21 -05:00
$txt = '<i class="fa-fw fa fa-ban" title="' . trans('firefly.openingBalance') . '"></i>';
2015-06-03 11:22:47 -05:00
break;
2015-05-17 02:35:49 -05:00
default:
2015-06-03 11:22:47 -05:00
$txt = '';
break;
2015-05-01 15:44:35 -05:00
}
2015-06-03 14:25:11 -05:00
$cache->store($txt);
2015-06-03 11:22:47 -05:00
return $txt;
2015-05-01 15:44:35 -05:00
}, ['is_safe' => ['html']]
);
}
}