mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
Code cleanup
This commit is contained in:
@@ -35,8 +35,14 @@ use Illuminate\Contracts\Validation\ValidationRule;
|
||||
*/
|
||||
class IsUniqueAccount implements ValidationRule, DataAwareRule
|
||||
{
|
||||
protected array $data = [];
|
||||
protected \Closure $fail;
|
||||
protected array $data = [];
|
||||
|
||||
#[\Override]
|
||||
public function setData(array $data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
@@ -103,28 +109,6 @@ class IsUniqueAccount implements ValidationRule, DataAwareRule
|
||||
return null === $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO duplicate from old validation class.
|
||||
*/
|
||||
private function validateAccountAnonymously(): bool
|
||||
{
|
||||
if (!array_key_exists('user_id', $this->data)) {
|
||||
$this->fail('No user ID provided.');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($this->data['user_id']);
|
||||
$type = AccountType::find($this->data['account_type_id'])->first();
|
||||
$value = $this->data['name'];
|
||||
|
||||
/** @var null|Account $result */
|
||||
$result = $user->accounts()->where('account_type_id', $type->id)->where('name', $value)->first();
|
||||
|
||||
return null === $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Duplicate from old validation class.
|
||||
*
|
||||
@@ -196,9 +180,25 @@ class IsUniqueAccount implements ValidationRule, DataAwareRule
|
||||
return 0 === auth()->user()->accounts()->where('name', $value)->count();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function setData(array $data): void
|
||||
/**
|
||||
* TODO duplicate from old validation class.
|
||||
*/
|
||||
private function validateAccountAnonymously(): bool
|
||||
{
|
||||
$this->data = $data;
|
||||
if (!array_key_exists('user_id', $this->data)) {
|
||||
$this->fail('No user ID provided.');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = User::find($this->data['user_id']);
|
||||
$type = AccountType::find($this->data['account_type_id'])->first();
|
||||
$value = $this->data['name'];
|
||||
|
||||
/** @var null|Account $result */
|
||||
$result = $user->accounts()->where('account_type_id', $type->id)->where('name', $value)->first();
|
||||
|
||||
return null === $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class IsValidDiscordUrl implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
if ('' === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class IsValidSlackOrDiscordUrl implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
if ('' === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class IsValidSlackUrl implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
if ('' === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -49,15 +49,15 @@ class BelongsUser implements ValidationRule
|
||||
app('log')->debug(sprintf('Going to validate %s', $attribute));
|
||||
|
||||
$result = match ($attribute) {
|
||||
'piggy_bank_id' => $this->validatePiggyBankId((int)$value),
|
||||
'piggy_bank_id' => $this->validatePiggyBankId((int) $value),
|
||||
'piggy_bank_name' => $this->validatePiggyBankName($value),
|
||||
'bill_id' => $this->validateBillId((int)$value),
|
||||
'transaction_journal_id' => $this->validateJournalId((int)$value),
|
||||
'bill_id' => $this->validateBillId((int) $value),
|
||||
'transaction_journal_id' => $this->validateJournalId((int) $value),
|
||||
'bill_name' => $this->validateBillName($value),
|
||||
'budget_id' => $this->validateBudgetId((int)$value),
|
||||
'category_id' => $this->validateCategoryId((int)$value),
|
||||
'budget_id' => $this->validateBudgetId((int) $value),
|
||||
'category_id' => $this->validateCategoryId((int) $value),
|
||||
'budget_name' => $this->validateBudgetName($value),
|
||||
'source_id', 'destination_id' => $this->validateAccountId((int)$value),
|
||||
'source_id', 'destination_id' => $this->validateAccountId((int) $value),
|
||||
default => throw new FireflyException(sprintf('Rule BelongsUser cannot handle "%s"', $attribute)),
|
||||
};
|
||||
if (false === $result) {
|
||||
@@ -110,7 +110,7 @@ class BelongsUser implements ValidationRule
|
||||
}
|
||||
$count = 0;
|
||||
foreach ($objects as $object) {
|
||||
$objectValue = trim((string)$object->{$field}); // @phpstan-ignore-line
|
||||
$objectValue = trim((string) $object->{$field}); // @phpstan-ignore-line
|
||||
app('log')->debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
|
||||
if ($objectValue === $value) {
|
||||
++$count;
|
||||
|
||||
@@ -63,15 +63,15 @@ class BelongsUserGroup implements ValidationRule
|
||||
app('log')->debug(sprintf('Group: Going to validate "%s"', $attribute));
|
||||
|
||||
$result = match ($attribute) {
|
||||
'piggy_bank_id' => $this->validatePiggyBankId((int)$value),
|
||||
'piggy_bank_id' => $this->validatePiggyBankId((int) $value),
|
||||
'piggy_bank_name' => $this->validatePiggyBankName($value),
|
||||
'bill_id' => $this->validateBillId((int)$value),
|
||||
'transaction_journal_id' => $this->validateJournalId((int)$value),
|
||||
'bill_id' => $this->validateBillId((int) $value),
|
||||
'transaction_journal_id' => $this->validateJournalId((int) $value),
|
||||
'bill_name' => $this->validateBillName($value),
|
||||
'budget_id' => $this->validateBudgetId((int)$value),
|
||||
'category_id' => $this->validateCategoryId((int)$value),
|
||||
'budget_id' => $this->validateBudgetId((int) $value),
|
||||
'category_id' => $this->validateCategoryId((int) $value),
|
||||
'budget_name' => $this->validateBudgetName($value),
|
||||
'source_id', 'destination_id' => $this->validateAccountId((int)$value),
|
||||
'source_id', 'destination_id' => $this->validateAccountId((int) $value),
|
||||
default => throw new FireflyException(sprintf('Rule BelongsUser cannot handle "%s"', $attribute)),
|
||||
};
|
||||
if (false === $result) {
|
||||
@@ -124,7 +124,7 @@ class BelongsUserGroup implements ValidationRule
|
||||
}
|
||||
$count = 0;
|
||||
foreach ($objects as $object) {
|
||||
$objectValue = trim((string)$object->{$field}); // @phpstan-ignore-line
|
||||
$objectValue = trim((string) $object->{$field}); // @phpstan-ignore-line
|
||||
app('log')->debug(sprintf('Comparing object "%s" with value "%s"', $objectValue, $value));
|
||||
if ($objectValue === $value) {
|
||||
++$count;
|
||||
|
||||
@@ -28,16 +28,15 @@ use FireflyIII\Enums\UserRoleEnum;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Repositories\UserGroup\UserGroupRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class IsAllowedGroupAction implements ValidationRule
|
||||
{
|
||||
private array $acceptedRoles;
|
||||
private string $className;
|
||||
private string $methodName;
|
||||
|
||||
private array $acceptedRoles;
|
||||
private UserGroupRepositoryInterface $repository;
|
||||
|
||||
public function __construct(string $className, string $methodName)
|
||||
@@ -71,7 +70,7 @@ class IsAllowedGroupAction implements ValidationRule
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->validateUserGroup((int)$value, $fail);
|
||||
$this->validateUserGroup((int) $value, $fail);
|
||||
}
|
||||
|
||||
private function validateUserGroup(int $userGroupId, \Closure $fail): void
|
||||
|
||||
@@ -37,7 +37,7 @@ class IsAssetAccountId implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$accountId = (int)$value;
|
||||
$accountId = (int) $value;
|
||||
|
||||
/** @var null|Account $account */
|
||||
$account = Account::with('accountType')->find($accountId);
|
||||
|
||||
@@ -39,7 +39,7 @@ class IsDateOrTime implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
if ('' === $value) {
|
||||
$fail('validation.date_or_time')->translate();
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ use Illuminate\Contracts\Validation\ValidationRule;
|
||||
class IsFilterValueIn implements ValidationRule
|
||||
{
|
||||
private string $key;
|
||||
private array $values;
|
||||
private array $values;
|
||||
|
||||
public function __construct(string $key, array $values)
|
||||
{
|
||||
|
||||
@@ -45,15 +45,15 @@ class IsTransferAccount implements ValidationRule
|
||||
$validator->setTransactionType(TransactionType::TRANSFER);
|
||||
$validator->setUser(auth()->user());
|
||||
|
||||
$validAccount = $validator->validateSource(['name' => (string)$value]);
|
||||
$validAccount = $validator->validateSource(['name' => (string) $value]);
|
||||
if (true === $validAccount) {
|
||||
app('log')->debug('Found account based on name. Return true.');
|
||||
|
||||
// found by name, use repos to return.
|
||||
return;
|
||||
}
|
||||
$validAccount = $validator->validateSource(['id' => (int)$value]);
|
||||
app('log')->debug(sprintf('Search by id (%d), result is %s.', (int)$value, var_export($validAccount, true)));
|
||||
$validAccount = $validator->validateSource(['id' => (int) $value]);
|
||||
app('log')->debug(sprintf('Search by id (%d), result is %s.', (int) $value, var_export($validAccount, true)));
|
||||
|
||||
if (false === $validAccount) {
|
||||
$fail('validation.not_transfer_account')->translate();
|
||||
|
||||
@@ -17,7 +17,7 @@ class IsValidAmount implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
|
||||
// must not be empty:
|
||||
if ($this->emptyString($value)) {
|
||||
|
||||
@@ -78,14 +78,14 @@ class IsValidAttachmentModel implements ValidationRule
|
||||
return;
|
||||
}
|
||||
$result = match ($this->model) {
|
||||
Account::class => $this->validateAccount((int)$value),
|
||||
Bill::class => $this->validateBill((int)$value),
|
||||
Budget::class => $this->validateBudget((int)$value),
|
||||
Category::class => $this->validateCategory((int)$value),
|
||||
PiggyBank::class => $this->validatePiggyBank((int)$value),
|
||||
Tag::class => $this->validateTag((int)$value),
|
||||
Transaction::class => $this->validateTransaction((int)$value),
|
||||
TransactionJournal::class => $this->validateJournal((int)$value),
|
||||
Account::class => $this->validateAccount((int) $value),
|
||||
Bill::class => $this->validateBill((int) $value),
|
||||
Budget::class => $this->validateBudget((int) $value),
|
||||
Category::class => $this->validateCategory((int) $value),
|
||||
PiggyBank::class => $this->validatePiggyBank((int) $value),
|
||||
Tag::class => $this->validateTag((int) $value),
|
||||
Transaction::class => $this->validateTransaction((int) $value),
|
||||
TransactionJournal::class => $this->validateJournal((int) $value),
|
||||
default => false,
|
||||
};
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class IsValidBulkClause implements ValidationRule
|
||||
public function __construct(string $type)
|
||||
{
|
||||
$this->rules = config(sprintf('bulk.%s', $type));
|
||||
$this->error = (string)trans('firefly.belongs_user');
|
||||
$this->error = (string) trans('firefly.belongs_user');
|
||||
}
|
||||
|
||||
public function message(): string
|
||||
@@ -51,7 +51,7 @@ class IsValidBulkClause implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$result = $this->basicValidation((string)$value);
|
||||
$result = $this->basicValidation((string) $value);
|
||||
if (false === $result) {
|
||||
$fail($this->error);
|
||||
}
|
||||
@@ -65,14 +65,14 @@ class IsValidBulkClause implements ValidationRule
|
||||
try {
|
||||
$array = json_decode($value, true, 8, JSON_THROW_ON_ERROR);
|
||||
} catch (\JsonException $e) {
|
||||
$this->error = (string)trans('validation.json');
|
||||
$this->error = (string) trans('validation.json');
|
||||
|
||||
return false;
|
||||
}
|
||||
$clauses = ['where', 'update'];
|
||||
foreach ($clauses as $clause) {
|
||||
if (!array_key_exists($clause, $array)) {
|
||||
$this->error = (string)trans(sprintf('validation.missing_%s', $clause));
|
||||
$this->error = (string) trans(sprintf('validation.missing_%s', $clause));
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ class IsValidBulkClause implements ValidationRule
|
||||
*/
|
||||
foreach ($array[$clause] as $arrayKey => $arrayValue) {
|
||||
if (!array_key_exists($arrayKey, $this->rules[$clause])) {
|
||||
$this->error = (string)trans(sprintf('validation.invalid_%s_key', $clause));
|
||||
$this->error = (string) trans(sprintf('validation.invalid_%s_key', $clause));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class IsValidPositiveAmount implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
// must not be empty:
|
||||
if ($this->emptyString($value)) {
|
||||
$fail('validation.filled')->translate();
|
||||
|
||||
@@ -17,7 +17,7 @@ class IsValidZeroOrMoreAmount implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
// must not be empty:
|
||||
if ($this->emptyString($value)) {
|
||||
$fail('validation.filled')->translate();
|
||||
|
||||
@@ -36,7 +36,7 @@ class LessThanPiggyTarget implements ValidationRule
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return (string)trans('validation.current_target_amount');
|
||||
return (string) trans('validation.current_target_amount');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -65,7 +65,7 @@ class UniqueAccountNumber implements ValidationRule
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return (string)trans('validation.unique_account_number_for_user');
|
||||
return (string) trans('validation.unique_account_number_for_user');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -68,13 +68,13 @@ class UniqueIban implements ValidationRule
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return (string)trans('validation.unique_iban_for_user');
|
||||
return (string) trans('validation.unique_iban_for_user');
|
||||
}
|
||||
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
if (!$this->passes($attribute, $value)) {
|
||||
$fail((string)trans('validation.unique_iban_for_user'));
|
||||
$fail((string) trans('validation.unique_iban_for_user'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class ValidRecurrenceRepetitionType implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
if ('daily' === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
$value = (string)$value;
|
||||
$value = (string) $value;
|
||||
|
||||
if ('daily' === $value) {
|
||||
return;
|
||||
@@ -68,7 +68,7 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
|
||||
|
||||
private function validateMonthly(string $value): bool
|
||||
{
|
||||
$dayOfMonth = (int)substr($value, 8);
|
||||
$dayOfMonth = (int) substr($value, 8);
|
||||
|
||||
return $dayOfMonth > 0 && $dayOfMonth < 32;
|
||||
}
|
||||
@@ -79,8 +79,8 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
|
||||
if (2 !== count($parameters)) {
|
||||
return false;
|
||||
}
|
||||
$nthDay = (int)($parameters[0] ?? 0.0);
|
||||
$dayOfWeek = (int)($parameters[1] ?? 0.0);
|
||||
$nthDay = (int) ($parameters[0] ?? 0.0);
|
||||
$dayOfWeek = (int) ($parameters[1] ?? 0.0);
|
||||
if ($nthDay < 1 || $nthDay > 5) {
|
||||
return false;
|
||||
}
|
||||
@@ -90,7 +90,7 @@ class ValidRecurrenceRepetitionValue implements ValidationRule
|
||||
|
||||
private function validateWeekly(string $value): bool
|
||||
{
|
||||
$dayOfWeek = (int)substr($value, 7);
|
||||
$dayOfWeek = (int) substr($value, 7);
|
||||
|
||||
return $dayOfWeek > 0 && $dayOfWeek < 8;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user