chore: reformat code.

This commit is contained in:
James Cole
2023-06-21 12:34:58 +02:00
parent 8d87abde64
commit 3dcb35710b
799 changed files with 23319 additions and 22173 deletions

View File

@@ -46,18 +46,6 @@ class AmountFormat extends AbstractExtension
];
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
$this->formatAmountBySymbol(),
$this->formatAmountByCurrency(),
];
}
/**
* @return TwigFilter
*/
@@ -74,6 +62,34 @@ class AmountFormat extends AbstractExtension
);
}
/**
* @return TwigFilter
*/
protected function formatAmountPlain(): TwigFilter
{
return new TwigFilter(
'formatAmountPlain',
static function (string $string): string {
$currency = app('amount')->getDefaultCurrency();
return app('amount')->formatAnything($currency, $string, false);
},
['is_safe' => ['html']]
);
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
$this->formatAmountBySymbol(),
$this->formatAmountByCurrency(),
];
}
/**
* Will format the amount by the currency related to the given account.
*
@@ -96,24 +112,6 @@ class AmountFormat extends AbstractExtension
);
}
/**
* Will format the amount by the currency related to the given account.
*
* @return TwigFunction
*/
protected function formatAmountByCurrency(): TwigFunction
{
return new TwigFunction(
'formatAmountByCurrency',
static function (TransactionCurrency $currency, string $amount, bool $coloured = null): string {
$coloured = $coloured ?? true;
return app('amount')->formatAnything($currency, $amount, $coloured);
},
['is_safe' => ['html']]
);
}
/**
* Will format the amount by the currency related to the given account.
*
@@ -137,16 +135,18 @@ class AmountFormat extends AbstractExtension
}
/**
* @return TwigFilter
* Will format the amount by the currency related to the given account.
*
* @return TwigFunction
*/
protected function formatAmountPlain(): TwigFilter
protected function formatAmountByCurrency(): TwigFunction
{
return new TwigFilter(
'formatAmountPlain',
static function (string $string): string {
$currency = app('amount')->getDefaultCurrency();
return new TwigFunction(
'formatAmountByCurrency',
static function (TransactionCurrency $currency, string $amount, bool $coloured = null): string {
$coloured = $coloured ?? true;
return app('amount')->formatAnything($currency, $string, false);
return app('amount')->formatAnything($currency, $amount, $coloured);
},
['is_safe' => ['html']]
);

View File

@@ -53,99 +53,6 @@ class General extends AbstractExtension
];
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->phpdate(),
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialObjectType(),
$this->menuOpenRoutePartial(),
$this->formatDate(),
$this->getMetaField(),
$this->hasRole(),
$this->getRootSearchOperator(),
$this->carbonize(),
];
}
/**
* Will return "active" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return TwigFunction
*/
protected function activeRoutePartial(): TwigFunction
{
return new TwigFunction(
'activeRoutePartial',
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
if (str_contains($name, $route)) {
return 'active';
}
return '';
}
);
}
/**
* This function will return "active" when the current route matches the first argument (even partly)
* but, the variable $objectType has been set and matches the second argument.
*
* @return TwigFunction
*/
protected function activeRoutePartialObjectType(): TwigFunction
{
return new TwigFunction(
'activeRoutePartialObjectType',
static function ($context): string {
[, $route, $objectType] = func_get_args();
$activeObjectType = $context['objectType'] ?? false;
if ($objectType === $activeObjectType
&& false !== stripos(
Route::getCurrentRoute()->getName(),
$route
)) {
return 'active';
}
return '';
},
['needs_context' => true]
);
}
/**
* Will return "active" when the current route matches the given argument
* exactly.
*
* @return TwigFunction
*/
protected function activeRouteStrict(): TwigFunction
{
return new TwigFunction(
'activeRouteStrict',
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
if (Route::getCurrentRoute()->getName() === $route) {
return 'active';
}
return '';
}
);
}
/**
* Show account balance. Only used on the front page of Firefly III.
*
@@ -167,36 +74,6 @@ class General extends AbstractExtension
);
}
/**
* @return TwigFunction
*/
protected function carbonize(): TwigFunction
{
return new TwigFunction(
'carbonize',
static function (string $date): Carbon {
return new Carbon($date, config('app.timezone'));
}
);
}
/**
* Formats a string as a thing by converting it to a Carbon first.
*
* @return TwigFunction
*/
protected function formatDate(): TwigFunction
{
return new TwigFunction(
'formatDate',
function (string $date, string $format): string {
$carbon = new Carbon($date);
return $carbon->isoFormat($format);
}
);
}
/**
* Used to convert 1024 to 1kb etc.
*
@@ -222,104 +99,6 @@ class General extends AbstractExtension
);
}
/**
* @return TwigFunction
* TODO remove me when layout v1 is deprecated.
*/
protected function getMetaField(): TwigFunction
{
return new TwigFunction(
'accountGetMetaField',
static function (Account $account, string $field): string {
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$result = $repository->getMetaValue($account, $field);
if (null === $result) {
return '';
}
return $result;
}
);
}
protected function getRootSearchOperator(): TwigFunction
{
return new TwigFunction(
'getRootSearchOperator',
static function (string $operator): string {
$result = OperatorQuerySearch::getRootOperator($operator);
return str_replace('-', 'not_', $result);
}
);
}
/**
* Will return true if the user is of role X.
*
* @return TwigFunction
*/
protected function hasRole(): TwigFunction
{
return new TwigFunction(
'hasRole',
static function (string $role): bool {
$repository = app(UserRepositoryInterface::class);
if ($repository->hasRole(auth()->user(), $role)) {
return true;
}
return false;
}
);
}
/**
* @return TwigFilter
*/
protected function markdown(): TwigFilter
{
return new TwigFilter(
'markdown',
static function (string $text): string {
$converter = new GithubFlavoredMarkdownConverter(
[
'allow_unsafe_links' => false,
'max_nesting_level' => 3,
'html_input' => 'escape',
]
);
return (string)$converter->convert($text);
},
['is_safe' => ['html']]
);
}
/**
* Will return "menu-open" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return TwigFunction
*/
protected function menuOpenRoutePartial(): TwigFunction
{
return new TwigFunction(
'menuOpenRoutePartial',
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
if (str_contains($name, $route)) {
return 'menu-open';
}
return '';
}
);
}
/**
* Show icon with attachment.
*
@@ -398,6 +177,28 @@ class General extends AbstractExtension
);
}
/**
* @return TwigFilter
*/
protected function markdown(): TwigFilter
{
return new TwigFilter(
'markdown',
static function (string $text): string {
$converter = new GithubFlavoredMarkdownConverter(
[
'allow_unsafe_links' => false,
'max_nesting_level' => 3,
'html_input' => 'escape',
]
);
return (string)$converter->convert($text);
},
['is_safe' => ['html']]
);
}
/**
* Show URL host name
*
@@ -416,6 +217,25 @@ class General extends AbstractExtension
);
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->phpdate(),
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialObjectType(),
$this->menuOpenRoutePartial(),
$this->formatDate(),
$this->getMetaField(),
$this->hasRole(),
$this->getRootSearchOperator(),
$this->carbonize(),
];
}
/**
* Basic example thing for some views.
*
@@ -430,4 +250,184 @@ class General extends AbstractExtension
}
);
}
/**
* Will return "active" when the current route matches the given argument
* exactly.
*
* @return TwigFunction
*/
protected function activeRouteStrict(): TwigFunction
{
return new TwigFunction(
'activeRouteStrict',
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
if (Route::getCurrentRoute()->getName() === $route) {
return 'active';
}
return '';
}
);
}
/**
* Will return "active" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return TwigFunction
*/
protected function activeRoutePartial(): TwigFunction
{
return new TwigFunction(
'activeRoutePartial',
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
if (str_contains($name, $route)) {
return 'active';
}
return '';
}
);
}
/**
* This function will return "active" when the current route matches the first argument (even partly)
* but, the variable $objectType has been set and matches the second argument.
*
* @return TwigFunction
*/
protected function activeRoutePartialObjectType(): TwigFunction
{
return new TwigFunction(
'activeRoutePartialObjectType',
static function ($context): string {
[, $route, $objectType] = func_get_args();
$activeObjectType = $context['objectType'] ?? false;
if ($objectType === $activeObjectType
&& false !== stripos(
Route::getCurrentRoute()->getName(),
$route
)) {
return 'active';
}
return '';
},
['needs_context' => true]
);
}
/**
* Will return "menu-open" when a part of the route matches the argument.
* ie. "accounts" will match "accounts.index".
*
* @return TwigFunction
*/
protected function menuOpenRoutePartial(): TwigFunction
{
return new TwigFunction(
'menuOpenRoutePartial',
static function (): string {
$args = func_get_args();
$route = $args[0]; // name of the route.
$name = Route::getCurrentRoute()->getName() ?? '';
if (str_contains($name, $route)) {
return 'menu-open';
}
return '';
}
);
}
/**
* Formats a string as a thing by converting it to a Carbon first.
*
* @return TwigFunction
*/
protected function formatDate(): TwigFunction
{
return new TwigFunction(
'formatDate',
function (string $date, string $format): string {
$carbon = new Carbon($date);
return $carbon->isoFormat($format);
}
);
}
/**
* @return TwigFunction
* TODO remove me when layout v1 is deprecated.
*/
protected function getMetaField(): TwigFunction
{
return new TwigFunction(
'accountGetMetaField',
static function (Account $account, string $field): string {
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$result = $repository->getMetaValue($account, $field);
if (null === $result) {
return '';
}
return $result;
}
);
}
/**
* Will return true if the user is of role X.
*
* @return TwigFunction
*/
protected function hasRole(): TwigFunction
{
return new TwigFunction(
'hasRole',
static function (string $role): bool {
$repository = app(UserRepositoryInterface::class);
if ($repository->hasRole(auth()->user(), $role)) {
return true;
}
return false;
}
);
}
protected function getRootSearchOperator(): TwigFunction
{
return new TwigFunction(
'getRootSearchOperator',
static function (string $operator): string {
$result = OperatorQuerySearch::getRootOperator($operator);
return str_replace('-', 'not_', $result);
}
);
}
/**
* @return TwigFunction
*/
protected function carbonize(): TwigFunction
{
return new TwigFunction(
'carbonize',
static function (string $date): Carbon {
return new Carbon($date, config('app.timezone'));
}
);
}
}

View File

@@ -33,25 +33,15 @@ use Twig\TwigFunction;
class Rule extends AbstractExtension
{
/**
* @return TwigFunction
* @return array
*/
public function allActionTriggers(): TwigFunction
public function getFunctions(): array
{
return new TwigFunction(
'allRuleActions',
static function () {
// array of valid values for actions
$ruleActions = array_keys(Config::get('firefly.rule-actions'));
$possibleActions = [];
foreach ($ruleActions as $key) {
$possibleActions[$key] = (string)trans('firefly.rule_action_'.$key.'_choice');
}
unset($ruleActions);
asort($possibleActions);
return $possibleActions;
}
);
return [
$this->allJournalTriggers(),
$this->allRuleTriggers(),
$this->allActionTriggers(),
];
}
/**
@@ -82,7 +72,7 @@ class Rule extends AbstractExtension
$possibleTriggers = [];
foreach ($ruleTriggers as $key) {
if ('user_action' !== $key) {
$possibleTriggers[$key] = (string)trans('firefly.rule_trigger_'.$key.'_choice');
$possibleTriggers[$key] = (string)trans('firefly.rule_trigger_' . $key . '_choice');
}
}
unset($ruleTriggers);
@@ -94,14 +84,24 @@ class Rule extends AbstractExtension
}
/**
* @return array
* @return TwigFunction
*/
public function getFunctions(): array
public function allActionTriggers(): TwigFunction
{
return [
$this->allJournalTriggers(),
$this->allRuleTriggers(),
$this->allActionTriggers(),
];
return new TwigFunction(
'allRuleActions',
static function () {
// array of valid values for actions
$ruleActions = array_keys(Config::get('firefly.rule-actions'));
$possibleActions = [];
foreach ($ruleActions as $key) {
$possibleActions[$key] = (string)trans('firefly.rule_action_' . $key . '_choice');
}
unset($ruleActions);
asort($possibleActions);
return $possibleActions;
}
);
}
}

View File

@@ -76,6 +76,201 @@ class TransactionGroupTwig extends AbstractExtension
);
}
/**
* Generate normal amount for transaction from a transaction group.
*
* @param array $array
*
* @return string
*/
private function normalJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['currency_symbol'], (int)$array['currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* @param string $amount
* @param string $transactionType
* @param string $sourceType
*
* @return string
*/
private function signAmount(string $amount, string $transactionType, string $sourceType): string
{
// withdrawals stay negative
if ($transactionType !== TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
// opening balance and it comes from initial balance? its expense.
if ($transactionType === TransactionType::OPENING_BALANCE && AccountType::INITIAL_BALANCE !== $sourceType) {
$amount = bcmul($amount, '-1');
}
// reconciliation and it comes from reconciliation?
if ($transactionType === TransactionType::RECONCILIATION && AccountType::RECONCILIATION !== $sourceType) {
$amount = bcmul($amount, '-1');
}
return $amount;
}
/**
* Generate foreign amount for transaction from a transaction group.
*
* @param array $array
*
* @return string
*/
private function foreignJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['foreign_amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* Shows the amount for a single journal object.
*
* @return TwigFunction
*/
public function journalObjectAmount(): TwigFunction
{
return new TwigFunction(
'journalObjectAmount',
function (TransactionJournal $journal): string {
$result = $this->normalJournalObjectAmount($journal);
// now append foreign amount, if any.
if ($this->journalObjectHasForeign($journal)) {
$foreign = $this->foreignJournalObjectAmount($journal);
$result = sprintf('%s (%s)', $result, $foreign);
}
return $result;
},
['is_safe' => ['html']]
);
}
/**
* Generate normal amount for transaction from a transaction group.
*
* @param TransactionJournal $journal
*
* @return string
*/
private function normalJournalObjectAmount(TransactionJournal $journal): string
{
$type = $journal->transactionType->type;
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $journal->transactionCurrency;
$amount = $first->amount ?? '0';
$colored = true;
$sourceType = $first->account()->first()->accountType()->first()->type;
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
private function journalObjectHasForeign(TransactionJournal $journal): bool
{
/** @var Transaction $first */
$first = $journal->transactions()->where('amount', '<', 0)->first();
return '' !== $first->foreign_amount;
}
/**
* Generate foreign amount for journal from a transaction group.
*
* @param TransactionJournal $journal
*
* @return string
*/
private function foreignJournalObjectAmount(TransactionJournal $journal): string
{
$type = $journal->transactionType->type;
/** @var Transaction $first */
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $first->foreignCurrency;
$amount = '' === $first->foreign_amount ? '0' : $first->foreign_amount;
$colored = true;
$sourceType = $first->account()->first()->accountType()->first()->type;
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* @return TwigFunction
*/
public function journalHasMeta(): TwigFunction
{
return new TwigFunction(
'journalHasMeta',
static function (int $journalId, string $metaField) {
$count = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->count();
return 1 === $count;
}
);
}
/**
* @return TwigFunction
*/
@@ -119,199 +314,4 @@ class TransactionGroupTwig extends AbstractExtension
}
);
}
/**
* @return TwigFunction
*/
public function journalHasMeta(): TwigFunction
{
return new TwigFunction(
'journalHasMeta',
static function (int $journalId, string $metaField) {
$count = DB::table('journal_meta')
->where('name', $metaField)
->where('transaction_journal_id', $journalId)
->whereNull('deleted_at')
->count();
return 1 === $count;
}
);
}
/**
* Shows the amount for a single journal object.
*
* @return TwigFunction
*/
public function journalObjectAmount(): TwigFunction
{
return new TwigFunction(
'journalObjectAmount',
function (TransactionJournal $journal): string {
$result = $this->normalJournalObjectAmount($journal);
// now append foreign amount, if any.
if ($this->journalObjectHasForeign($journal)) {
$foreign = $this->foreignJournalObjectAmount($journal);
$result = sprintf('%s (%s)', $result, $foreign);
}
return $result;
},
['is_safe' => ['html']]
);
}
/**
* Generate foreign amount for transaction from a transaction group.
*
* @param array $array
*
* @return string
*/
private function foreignJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['foreign_amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* Generate foreign amount for journal from a transaction group.
*
* @param TransactionJournal $journal
*
* @return string
*/
private function foreignJournalObjectAmount(TransactionJournal $journal): string
{
$type = $journal->transactionType->type;
/** @var Transaction $first */
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $first->foreignCurrency;
$amount = '' === $first->foreign_amount ? '0' : $first->foreign_amount;
$colored = true;
$sourceType = $first->account()->first()->accountType()->first()->type;
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
private function journalObjectHasForeign(TransactionJournal $journal): bool
{
/** @var Transaction $first */
$first = $journal->transactions()->where('amount', '<', 0)->first();
return '' !== $first->foreign_amount;
}
/**
* Generate normal amount for transaction from a transaction group.
*
* @param array $array
*
* @return string
*/
private function normalJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['currency_symbol'], (int)$array['currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* Generate normal amount for transaction from a transaction group.
*
* @param TransactionJournal $journal
*
* @return string
*/
private function normalJournalObjectAmount(TransactionJournal $journal): string
{
$type = $journal->transactionType->type;
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $journal->transactionCurrency;
$amount = $first->amount ?? '0';
$colored = true;
$sourceType = $first->account()->first()->accountType()->first()->type;
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info money-transfer">%s</span>', $result);
}
return $result;
}
/**
* @param string $amount
* @param string $transactionType
* @param string $sourceType
*
* @return string
*/
private function signAmount(string $amount, string $transactionType, string $sourceType): string
{
// withdrawals stay negative
if ($transactionType !== TransactionType::WITHDRAWAL) {
$amount = bcmul($amount, '-1');
}
// opening balance and it comes from initial balance? its expense.
if ($transactionType === TransactionType::OPENING_BALANCE && AccountType::INITIAL_BALANCE !== $sourceType) {
$amount = bcmul($amount, '-1');
}
// reconciliation and it comes from reconciliation?
if ($transactionType === TransactionType::RECONCILIATION && AccountType::RECONCILIATION !== $sourceType) {
$amount = bcmul($amount, '-1');
}
return $amount;
}
}