This commit is contained in:
James Cole 2023-12-21 05:06:51 +01:00
parent 135b9fc010
commit 1f7ceb6df6
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
2 changed files with 87 additions and 86 deletions

View File

@ -82,7 +82,8 @@ class ParseDateString
// if regex for YYYY-MM-DD:
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/';
if (false !== preg_match($pattern, $date)) {
$result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
return $this->parseDefaultDate($date);
}
@ -108,7 +109,7 @@ class ParseDateString
return new Carbon('1984-09-17');
}
// maybe a year, nothing else?
if (4 === strlen($date) && is_numeric($date) && (int)$date > 1000 && (int)$date <= 3000) {
if (4 === strlen($date) && is_numeric($date) && (int) $date > 1000 && (int) $date <= 3000) {
return new Carbon(sprintf('%d-01-01', $date));
}
@ -223,15 +224,15 @@ class ParseDateString
// verify if correct
$pattern = '/[+-]\d+[wqmdy]/';
$res = preg_match($pattern, $part);
if (0 === $res || false === $res) {
$result = preg_match($pattern, $part);
if (0 === $result || false === $result) {
app('log')->error(sprintf('Part "%s" does not match regular expression. Will be skipped.', $part));
continue;
}
$direction = str_starts_with($part, '+') ? 1 : 0;
$period = $part[strlen($part) - 1];
$number = (int)substr($part, 1, -1);
$number = (int) substr($part, 1, -1);
if (!array_key_exists($period, $functions[$direction])) {
app('log')->error(sprintf('No method for direction %d and period "%s".', $direction, $period));
@ -246,11 +247,19 @@ class ParseDateString
return $today;
}
/**
* Returns true if this matches regex for xxxx-xx-DD:
*
* @param string $date
*
* @return bool
*/
protected function isDayRange(string $date): bool
{
// if regex for xxxx-xx-DD:
$pattern = '/^xxxx-xx-(0[1-9]|[12]\d|3[01])$/';
if (false !== preg_match($pattern, $date)) {
$result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a day range.', $date));
return true;
@ -276,7 +285,8 @@ class ParseDateString
{
// if regex for xxxx-MM-xx:
$pattern = '/^xxxx-(0[1-9]|1[012])-xx$/';
if (false !== preg_match($pattern, $date)) {
$result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a month range.', $date));
return true;
@ -303,7 +313,8 @@ class ParseDateString
{
// if regex for YYYY-xx-xx:
$pattern = '/^(19|20)\d\d-xx-xx$/';
if (false !== preg_match($pattern, $date)) {
$result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a year range.', $date));
return true;
@ -330,7 +341,8 @@ class ParseDateString
{
// if regex for xxxx-MM-DD:
$pattern = '/^xxxx-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$/';
if (false !== preg_match($pattern, $date)) {
$result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a month/day range.', $date));
return true;
@ -344,7 +356,8 @@ class ParseDateString
{
// if regex for YYYY-xx-DD:
$pattern = '/^(19|20)\d\d-xx-(0[1-9]|[12]\d|3[01])$/';
if (false !== preg_match($pattern, $date)) {
$result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a day/year range.', $date));
return true;
@ -358,7 +371,8 @@ class ParseDateString
{
// if regex for YYYY-MM-xx:
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-xx$/';
if (false !== preg_match($pattern, $date)) {
$result = preg_match($pattern, $date);
if (false !== $result && 0 !== $result) {
app('log')->debug(sprintf('"%s" is a month/year range.', $date));
return true;

View File

@ -74,7 +74,7 @@ class FireflyValidator extends Validator
$secret = '';
}
return (bool)\Google2FA::verifyKey((string)$secret, $value);
return (bool) \Google2FA::verifyKey((string) $secret, $value);
}
/**
@ -88,7 +88,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();
@ -106,10 +106,7 @@ class FireflyValidator extends Validator
{
$regex = '/^[a-z]{6}[0-9a-z]{2}([0-9a-z]{3})?\z/i';
$result = preg_match($regex, $value);
if (false === $result) {
return false;
}
if (0 === $result) {
if (false === $result || 0 === $result) {
return false;
}
@ -180,7 +177,7 @@ class FireflyValidator extends Validator
$value = strtoupper($value);
// replace characters outside of ASCI range.
$value = (string)iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$value = (string) 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 = [
'',
@ -215,7 +212,7 @@ class FireflyValidator extends Validator
// take
$first = substr($value, 0, 4);
$last = substr($value, 4);
$iban = $last.$first;
$iban = $last . $first;
$iban = trim(str_replace($search, $replace, $iban));
if ('' === $iban) {
return false;
@ -231,7 +228,7 @@ class FireflyValidator extends Validator
return false;
}
return 1 === (int)$checksum;
return 1 === (int) $checksum;
}
/**
@ -246,7 +243,7 @@ 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;
}
/**
@ -261,7 +258,7 @@ 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;
}
/**
@ -275,7 +272,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();
@ -288,7 +285,7 @@ class FireflyValidator extends Validator
// first, get the index from this string:
$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';
@ -353,7 +350,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';
@ -398,7 +395,7 @@ class FireflyValidator extends Validator
// 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.
@ -435,7 +432,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 */
@ -470,7 +467,7 @@ class FireflyValidator extends Validator
if (array_key_exists('type', $this->data)) {
app('log')->debug('validateUniqueAccountForUser::typeString');
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)) {
app('log')->debug('validateUniqueAccountForUser::typeId');
@ -481,7 +478,7 @@ class FireflyValidator extends Validator
if (null !== $parameterId) {
app('log')->debug('validateUniqueAccountForUser::paramId');
return $this->validateByParameterId((int)$parameterId, $value);
return $this->validateByParameterId((int) $parameterId, $value);
}
if (array_key_exists('id', $this->data)) {
app('log')->debug('validateUniqueAccountForUser::accountId');
@ -504,17 +501,16 @@ class FireflyValidator extends Validator
*/
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')
->whereNull('accounts.deleted_at')
->where('accounts.user_id', auth()->user()->id)
->where('account_meta.name', 'account_number')
->where('account_meta.data', json_encode($value))
;
->whereNull('accounts.deleted_at')
->where('accounts.user_id', auth()->user()->id)
->where('account_meta.name', 'account_number')
->where('account_meta.data', json_encode($value));
if ($accountId > 0) {
// exclude current account from check.
@ -541,7 +537,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) {
app('log')->debug(sprintf('The other account with this account number is a "%s" so return true.', $otherType));
@ -556,9 +552,9 @@ class FireflyValidator extends Validator
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function validateUniqueCurrencyCode(null|string $attribute, null|string $value): bool
public function validateUniqueCurrencyCode(null | string $attribute, null | string $value): bool
{
return $this->validateUniqueCurrency('code', (string)$attribute, (string)$value);
return $this->validateUniqueCurrency('code', (string) $attribute, (string) $value);
}
/**
@ -569,14 +565,14 @@ class FireflyValidator extends Validator
return 0 === \DB::table('transaction_currencies')->where($field, $value)->whereNull('deleted_at')->count();
}
public function validateUniqueCurrencyName(null|string $attribute, null|string $value): bool
public function validateUniqueCurrencyName(null | string $attribute, null | string $value): bool
{
return $this->validateUniqueCurrency('name', (string)$attribute, (string)$value);
return $this->validateUniqueCurrency('name', (string) $attribute, (string) $value);
}
public function validateUniqueCurrencySymbol(null|string $attribute, null|string $value): bool
public function validateUniqueCurrencySymbol(null | string $attribute, null | string $value): bool
{
return $this->validateUniqueCurrency('symbol', (string)$attribute, (string)$value);
return $this->validateUniqueCurrency('symbol', (string) $attribute, (string) $value);
}
/**
@ -588,7 +584,7 @@ class FireflyValidator extends Validator
*/
public function validateUniqueExistingWebhook($value, $parameters, $something): bool
{
$existingId = (int)($something[0] ?? 0);
$existingId = (int) ($something[0] ?? 0);
$trigger = 0;
$response = 0;
$delivery = 0;
@ -617,12 +613,11 @@ class FireflyValidator extends Validator
$userId = auth()->user()->id;
return 0 === Webhook::whereUserId($userId)
->where('trigger', $trigger)
->where('response', $response)
->where('delivery', $delivery)
->where('id', '!=', $existingId)
->where('url', $url)->count()
;
->where('trigger', $trigger)
->where('response', $response)
->where('delivery', $delivery)
->where('id', '!=', $existingId)
->where('url', $url)->count();
}
return false;
@ -644,22 +639,21 @@ class FireflyValidator extends Validator
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
$result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
->where('id', '!=', $exclude)
->where($field, $value)
->first([$field])
;
->where('id', '!=', $exclude)
->where($field, $value)
->first([$field]);
if (null === $result) {
return true; // not found, so true.
}
@ -679,12 +673,11 @@ class FireflyValidator extends Validator
{
$exclude = $parameters[0] ?? null;
$query = \DB::table('object_groups')
->whereNull('object_groups.deleted_at')
->where('object_groups.user_id', auth()->user()->id)
->where('object_groups.title', $value)
;
->whereNull('object_groups.deleted_at')
->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();
@ -701,10 +694,9 @@ class FireflyValidator extends Validator
{
$exclude = $parameters[0] ?? null;
$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)
;
->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);
@ -732,11 +724,10 @@ class FireflyValidator extends Validator
$userId = auth()->user()->id;
return 0 === Webhook::whereUserId($userId)
->where('trigger', $trigger)
->where('response', $response)
->where('delivery', $delivery)
->where('url', $url)->count()
;
->where('trigger', $trigger)
->where('response', $response)
->where('delivery', $delivery)
->where('url', $url)->count();
}
return false;
@ -762,21 +753,20 @@ class FireflyValidator extends Validator
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
{
/** @var null|array $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 null|Account $result */
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
->where('name', $value)
->first()
;
->where('name', $value)
->first();
return null === $result;
}
@ -788,13 +778,12 @@ class FireflyValidator extends Validator
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 null|Account $result */
$result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first()
;
->where('name', $value)
->first();
return null === $result;
}
@ -811,9 +800,8 @@ class FireflyValidator extends Validator
$ignore = $existingAccount->id;
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first()
;
->where('name', $value)
->first();
return null === $entry;
}
@ -830,9 +818,8 @@ class FireflyValidator extends Validator
$ignore = $existingAccount->id;
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
->where('name', $value)
->first()
;
->where('name', $value)
->first();
return null === $entry;
}