mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Various PSR12 code cleanup
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* FireflyValidator.php
|
||||
* Copyright (c) 2019 james@firefly-iii.org
|
||||
@@ -56,8 +57,8 @@ use function is_string;
|
||||
class FireflyValidator extends Validator
|
||||
{
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
* @throws IncompatibleWithGoogleAuthenticatorException
|
||||
@@ -81,9 +82,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -91,7 +92,7 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
$field = $parameters[1] ?? 'id';
|
||||
|
||||
if (0 === (int) $value) {
|
||||
if (0 === (int)$value) {
|
||||
return true;
|
||||
}
|
||||
$count = DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count();
|
||||
@@ -100,8 +101,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -120,8 +121,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -188,13 +189,40 @@ class FireflyValidator extends Validator
|
||||
// replace characters outside of ASCI range.
|
||||
$value = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
|
||||
$search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
|
||||
$replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31',
|
||||
'32', '33', '34', '35',];
|
||||
$replace = [
|
||||
'',
|
||||
'10',
|
||||
'11',
|
||||
'12',
|
||||
'13',
|
||||
'14',
|
||||
'15',
|
||||
'16',
|
||||
'17',
|
||||
'18',
|
||||
'19',
|
||||
'20',
|
||||
'21',
|
||||
'22',
|
||||
'23',
|
||||
'24',
|
||||
'25',
|
||||
'26',
|
||||
'27',
|
||||
'28',
|
||||
'29',
|
||||
'30',
|
||||
'31',
|
||||
'32',
|
||||
'33',
|
||||
'34',
|
||||
'35',
|
||||
];
|
||||
|
||||
// take
|
||||
$first = substr($value, 0, 4);
|
||||
$last = substr($value, 4);
|
||||
$iban = $last . $first;
|
||||
$iban = $last.$first;
|
||||
$iban = trim(str_replace($search, $replace, $iban));
|
||||
if (0 === strlen($iban)) {
|
||||
return false;
|
||||
@@ -209,13 +237,13 @@ class FireflyValidator extends Validator
|
||||
return false;
|
||||
}
|
||||
|
||||
return 1 === (int) $checksum;
|
||||
return 1 === (int)$checksum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -224,13 +252,13 @@ class FireflyValidator extends Validator
|
||||
/** @var mixed $compare */
|
||||
$compare = $parameters[0] ?? '0';
|
||||
|
||||
return bccomp((string) $value, (string) $compare) < 0;
|
||||
return bccomp((string)$value, (string)$compare) < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -239,13 +267,13 @@ class FireflyValidator extends Validator
|
||||
/** @var mixed $compare */
|
||||
$compare = $parameters[0] ?? '0';
|
||||
|
||||
return bccomp((string) $value, (string) $compare) > 0;
|
||||
return bccomp((string)$value, (string)$compare) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -253,7 +281,7 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
$field = $parameters[1] ?? 'id';
|
||||
|
||||
if (0 === (int) $value) {
|
||||
if (0 === (int)$value) {
|
||||
return true;
|
||||
}
|
||||
$count = DB::table($parameters[0])->where($field, $value)->count();
|
||||
@@ -262,9 +290,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param string $attribute
|
||||
*
|
||||
* @param string|null $value
|
||||
* @param string|null $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -273,7 +301,7 @@ class FireflyValidator extends Validator
|
||||
// first, get the index from this string:
|
||||
$value = $value ?? '';
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int) ($parts[1] ?? '0');
|
||||
$index = (int)($parts[1] ?? '0');
|
||||
|
||||
// get the name of the trigger from the data array:
|
||||
$actionType = $this->data['actions'][$index]['type'] ?? 'invalid';
|
||||
@@ -306,8 +334,14 @@ class FireflyValidator extends Validator
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$account = $repository->findByName(
|
||||
$value,
|
||||
[AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE,
|
||||
AccountType::CREDITCARD]
|
||||
[
|
||||
AccountType::DEFAULT,
|
||||
AccountType::ASSET,
|
||||
AccountType::LOAN,
|
||||
AccountType::DEBT,
|
||||
AccountType::MORTGAGE,
|
||||
AccountType::CREDITCARD,
|
||||
]
|
||||
);
|
||||
|
||||
return null !== $account;
|
||||
@@ -328,8 +362,8 @@ class FireflyValidator extends Validator
|
||||
/**
|
||||
* $attribute has the format triggers.%d.value.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string|null $value
|
||||
* @param string $attribute
|
||||
* @param string|null $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -337,7 +371,7 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
// first, get the index from this string:
|
||||
$parts = explode('.', $attribute);
|
||||
$index = (int) ($parts[1] ?? '0');
|
||||
$index = (int)($parts[1] ?? '0');
|
||||
|
||||
// get the name of the trigger from the data array:
|
||||
$triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid';
|
||||
@@ -354,18 +388,35 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
// these trigger types need a simple strlen check:
|
||||
$length = ['source_account_starts', 'source_account_ends', 'source_account_is', 'source_account_contains', 'destination_account_starts',
|
||||
'destination_account_ends',
|
||||
'destination_account_is', 'destination_account_contains', 'description_starts', 'description_ends', 'description_contains', 'description_is',
|
||||
'category_is',
|
||||
'budget_is', 'tag_is', 'currency_is', 'notes_contain', 'notes_start', 'notes_end', 'notes_are',];
|
||||
$length = [
|
||||
'source_account_starts',
|
||||
'source_account_ends',
|
||||
'source_account_is',
|
||||
'source_account_contains',
|
||||
'destination_account_starts',
|
||||
'destination_account_ends',
|
||||
'destination_account_is',
|
||||
'destination_account_contains',
|
||||
'description_starts',
|
||||
'description_ends',
|
||||
'description_contains',
|
||||
'description_is',
|
||||
'category_is',
|
||||
'budget_is',
|
||||
'tag_is',
|
||||
'currency_is',
|
||||
'notes_contain',
|
||||
'notes_start',
|
||||
'notes_end',
|
||||
'notes_are',
|
||||
];
|
||||
if (in_array($triggerType, $length, true)) {
|
||||
return '' !== $value;
|
||||
}
|
||||
|
||||
// check if it's an existing account.
|
||||
if (in_array($triggerType, ['destination_account_id', 'source_account_id'], true)) {
|
||||
return is_numeric($value) && (int) $value > 0;
|
||||
return is_numeric($value) && (int)$value > 0;
|
||||
}
|
||||
|
||||
// check transaction type.
|
||||
@@ -392,8 +443,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -401,7 +452,7 @@ class FireflyValidator extends Validator
|
||||
{
|
||||
$verify = false;
|
||||
if (array_key_exists('verify_password', $this->data)) {
|
||||
$verify = 1 === (int) $this->data['verify_password'];
|
||||
$verify = 1 === (int)$this->data['verify_password'];
|
||||
}
|
||||
if ($verify) {
|
||||
/** @var Verifier $service */
|
||||
@@ -414,9 +465,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -430,14 +481,14 @@ class FireflyValidator extends Validator
|
||||
return $this->validateByAccountTypeString($value, $parameters, $this->data['objectType']);
|
||||
}
|
||||
if (array_key_exists('type', $this->data)) {
|
||||
return $this->validateByAccountTypeString($value, $parameters, (string) $this->data['type']);
|
||||
return $this->validateByAccountTypeString($value, $parameters, (string)$this->data['type']);
|
||||
}
|
||||
if (array_key_exists('account_type_id', $this->data)) {
|
||||
return $this->validateByAccountTypeId($value, $parameters);
|
||||
}
|
||||
$parameterId = $parameters[0] ?? null;
|
||||
if (null !== $parameterId) {
|
||||
return $this->validateByParameterId((int) $parameterId, $value);
|
||||
return $this->validateByParameterId((int)$parameterId, $value);
|
||||
}
|
||||
if (array_key_exists('id', $this->data)) {
|
||||
return $this->validateByAccountId($value);
|
||||
@@ -471,23 +522,23 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @param string $type
|
||||
* @param string $value
|
||||
* @param array $parameters
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
|
||||
{
|
||||
/** @var array|null $search */
|
||||
$search = Config::get('firefly.accountTypeByIdentifier.' . $type);
|
||||
$search = Config::get('firefly.accountTypeByIdentifier.'.$type);
|
||||
|
||||
if (null === $search) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$accountTypes = AccountType::whereIn('type', $search)->get();
|
||||
$ignore = (int) ($parameters[0] ?? 0.0);
|
||||
$ignore = (int)($parameters[0] ?? 0.0);
|
||||
$accountTypeIds = $accountTypes->pluck('id')->toArray();
|
||||
/** @var Collection $set */
|
||||
$set = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)->get();
|
||||
@@ -501,15 +552,15 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function validateByAccountTypeId($value, $parameters): bool
|
||||
{
|
||||
$type = AccountType::find($this->data['account_type_id'])->first();
|
||||
$ignore = (int) ($parameters[0] ?? 0.0);
|
||||
$ignore = (int)($parameters[0] ?? 0.0);
|
||||
|
||||
/** @var Collection $set */
|
||||
$set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();
|
||||
@@ -524,8 +575,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $accountId
|
||||
* @param mixed $value
|
||||
* @param int $accountId
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -545,7 +596,7 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -565,7 +616,7 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -575,17 +626,17 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool
|
||||
{
|
||||
$accountId = (int) ($this->data['id'] ?? 0.0);
|
||||
$accountId = (int)($this->data['id'] ?? 0.0);
|
||||
if (0 === $accountId) {
|
||||
$accountId = (int) ($parameters[0] ?? 0.0);
|
||||
$accountId = (int)($parameters[0] ?? 0.0);
|
||||
}
|
||||
|
||||
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
|
||||
@@ -617,7 +668,7 @@ class FireflyValidator extends Validator
|
||||
/** @var AccountMeta $entry */
|
||||
foreach ($set as $entry) {
|
||||
$otherAccount = $entry->account;
|
||||
$otherType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $otherAccount->accountType->type));
|
||||
$otherType = (string)config(sprintf('firefly.shortNamesByFullName.%s', $otherAccount->accountType->type));
|
||||
if (('expense' === $otherType || 'revenue' === $otherType) && $otherType !== $type) {
|
||||
Log::debug(sprintf('The other account with this account number is a "%s" so return true.', $otherType));
|
||||
return true;
|
||||
@@ -634,23 +685,13 @@ class FireflyValidator extends Validator
|
||||
*/
|
||||
public function validateUniqueCurrencyCode($attribute, $value): bool
|
||||
{
|
||||
return $this->validateUniqueCurrency('code', (string) $attribute, (string) $value);
|
||||
return $this->validateUniqueCurrency('code', (string)$attribute, (string)$value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validateUniqueCurrencyName($attribute, $value): bool
|
||||
{
|
||||
return $this->validateUniqueCurrency('name', (string) $attribute, (string) $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @param string $field
|
||||
* @param string $attribute
|
||||
* @param string $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validateUniqueCurrency(string $field, string $attribute, string $value): bool
|
||||
@@ -663,21 +704,31 @@ class FireflyValidator extends Validator
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validateUniqueCurrencySymbol($attribute, $value): bool
|
||||
public function validateUniqueCurrencyName($attribute, $value): bool
|
||||
{
|
||||
return $this->validateUniqueCurrency('symbol', (string) $attribute, (string) $value);
|
||||
return $this->validateUniqueCurrency('name', (string)$attribute, (string)$value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $something
|
||||
* @param $attribute
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function validateUniqueCurrencySymbol($attribute, $value): bool
|
||||
{
|
||||
return $this->validateUniqueCurrency('symbol', (string)$attribute, (string)$value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $something
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateUniqueExistingWebhook($value, $parameters, $something): bool
|
||||
{
|
||||
$existingId = (int) ($something[0] ?? 0);
|
||||
$existingId = (int)($something[0] ?? 0);
|
||||
$trigger = 0;
|
||||
$response = 0;
|
||||
$delivery = 0;
|
||||
@@ -724,24 +775,24 @@ class FireflyValidator extends Validator
|
||||
* parameter 1: the field
|
||||
* parameter 2: an id to ignore (when editing)
|
||||
*
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateUniqueObjectForUser($attribute, $value, $parameters): bool
|
||||
{
|
||||
[$table, $field] = $parameters;
|
||||
$exclude = (int) ($parameters[2] ?? 0.0);
|
||||
$exclude = (int)($parameters[2] ?? 0.0);
|
||||
|
||||
/*
|
||||
* If other data (in $this->getData()) contains
|
||||
* ID field, set that field to be the $exclude.
|
||||
*/
|
||||
$data = $this->getData();
|
||||
if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int) $data['id'] > 0) {
|
||||
$exclude = (int) $data['id'];
|
||||
if (!array_key_exists(2, $parameters) && array_key_exists('id', $data) && (int)$data['id'] > 0) {
|
||||
$exclude = (int)$data['id'];
|
||||
}
|
||||
// get entries from table
|
||||
$set = DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
|
||||
@@ -759,9 +810,9 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -773,16 +824,16 @@ class FireflyValidator extends Validator
|
||||
->where('object_groups.user_id', auth()->user()->id)
|
||||
->where('object_groups.title', $value);
|
||||
if (null !== $exclude) {
|
||||
$query->where('object_groups.id', '!=', (int) $exclude);
|
||||
$query->where('object_groups.id', '!=', (int)$exclude);
|
||||
}
|
||||
|
||||
return 0 === $query->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -792,7 +843,7 @@ class FireflyValidator extends Validator
|
||||
$query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id);
|
||||
if (null !== $exclude) {
|
||||
$query->where('piggy_banks.id', '!=', (int) $exclude);
|
||||
$query->where('piggy_banks.id', '!=', (int)$exclude);
|
||||
}
|
||||
$query->where('piggy_banks.name', $value);
|
||||
|
||||
@@ -800,8 +851,8 @@ class FireflyValidator extends Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
* @param mixed $value
|
||||
* @param mixed $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
Reference in New Issue
Block a user