mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 00:27:30 +00:00
Code reordering and reformatting. I should really start employing style CI.
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support;
|
||||
|
||||
use Carbon\Carbon;
|
||||
@@ -47,6 +48,30 @@ class ParseDateString
|
||||
'end of this year',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDateRange(string $date): bool
|
||||
{
|
||||
$date = strtolower($date);
|
||||
// not 10 chars:
|
||||
if (10 !== strlen($date)) {
|
||||
return false;
|
||||
}
|
||||
// all x'es
|
||||
if ('xxxx-xx-xx' === strtolower($date)) {
|
||||
return false;
|
||||
}
|
||||
// no x'es
|
||||
if (!str_contains($date, 'xx') && !str_contains($date, 'xxxx')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*
|
||||
@@ -89,35 +114,99 @@ 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));
|
||||
}
|
||||
|
||||
throw new FireflyException(sprintf('[d]Not a recognised date format: "%s"', $date));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $keyword
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function parseKeyword(string $keyword): Carbon
|
||||
{
|
||||
$today = Carbon::today()->startOfDay();
|
||||
|
||||
return match ($keyword) {
|
||||
default => $today,
|
||||
'yesterday' => $today->subDay(),
|
||||
'tomorrow' => $today->addDay(),
|
||||
'start of this week' => $today->startOfWeek(),
|
||||
'end of this week' => $today->endOfWeek(),
|
||||
'start of this month' => $today->startOfMonth(),
|
||||
'end of this month' => $today->endOfMonth(),
|
||||
'start of this quarter' => $today->startOfQuarter(),
|
||||
'end of this quarter' => $today->endOfQuarter(),
|
||||
'start of this year' => $today->startOfYear(),
|
||||
'end of this year' => $today->endOfYear(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*
|
||||
* @return bool
|
||||
* @return Carbon
|
||||
*/
|
||||
public function isDateRange(string $date): bool
|
||||
protected function parseDefaultDate(string $date): Carbon
|
||||
{
|
||||
$date = strtolower($date);
|
||||
// not 10 chars:
|
||||
if (10 !== strlen($date)) {
|
||||
return false;
|
||||
}
|
||||
// all x'es
|
||||
if ('xxxx-xx-xx' === strtolower($date)) {
|
||||
return false;
|
||||
}
|
||||
// no x'es
|
||||
if (!str_contains($date, 'xx') && !str_contains($date, 'xxxx')) {
|
||||
return false;
|
||||
return Carbon::createFromFormat('Y-m-d', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function parseRelativeDate(string $date): Carbon
|
||||
{
|
||||
Log::debug(sprintf('Now in parseRelativeDate("%s")', $date));
|
||||
$parts = explode(' ', $date);
|
||||
$today = Carbon::today()->startOfDay();
|
||||
$functions = [
|
||||
[
|
||||
'd' => 'subDays',
|
||||
'w' => 'subWeeks',
|
||||
'm' => 'subMonths',
|
||||
'q' => 'subQuarters',
|
||||
'y' => 'subYears',
|
||||
], [
|
||||
'd' => 'addDays',
|
||||
'w' => 'addWeeks',
|
||||
'm' => 'addMonths',
|
||||
'q' => 'addQuarters',
|
||||
'y' => 'addYears',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($parts as $part) {
|
||||
Log::debug(sprintf('Now parsing part "%s"', $part));
|
||||
$part = trim($part);
|
||||
|
||||
// verify if correct
|
||||
$pattern = '/[+-]\d+[wqmdy]/';
|
||||
$res = preg_match($pattern, $part);
|
||||
if (0 === $res || false === $res) {
|
||||
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);
|
||||
if (!array_key_exists($period, $functions[$direction])) {
|
||||
Log::error(sprintf('No method for direction %d and period "%s".', $direction, $period));
|
||||
continue;
|
||||
}
|
||||
$func = $functions[$direction][$period];
|
||||
Log::debug(sprintf('Will now do %s(%d) on %s', $func, $number, $today->format('Y-m-d')));
|
||||
$today->$func($number);
|
||||
Log::debug(sprintf('Resulting date is %s', $today->format('Y-m-d')));
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
return $today;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,21 +302,29 @@ class ParseDateString
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param Carbon $journalDate
|
||||
*
|
||||
* @return bool
|
||||
* @return array
|
||||
*/
|
||||
protected function isMonthYearRange(string $date): bool
|
||||
protected function parseMonthRange(string $date, Carbon $journalDate): array
|
||||
{
|
||||
// if regex for YYYY-MM-xx:
|
||||
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-xx$/';
|
||||
if (preg_match($pattern, $date)) {
|
||||
Log::debug(sprintf('"%s" is a month/year range.', $date));
|
||||
|
||||
return true;
|
||||
// because 31 would turn February into March unexpectedly and the exact day is irrelevant here.
|
||||
$day = $journalDate->format('d');
|
||||
if ((int)$day > 28) {
|
||||
$day = '28';
|
||||
}
|
||||
Log::debug(sprintf('"%s" is not a month/year range.', $date));
|
||||
|
||||
return false;
|
||||
// format of string is xxxx-MM-xx
|
||||
$validDate = str_replace(['xxxx'], [$journalDate->year], $date);
|
||||
$validDate = str_replace(['xx'], [$day], $validDate);
|
||||
Log::debug(sprintf('parseMonthRange: Parsed "%s" into "%s"', $date, $validDate));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $validDate)->startOfMonth();
|
||||
$end = Carbon::createFromFormat('Y-m-d', $validDate)->endOfMonth();
|
||||
|
||||
return [
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,146 +345,6 @@ class ParseDateString
|
||||
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param string $date
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function parseDefaultDate(string $date): Carbon
|
||||
{
|
||||
return Carbon::createFromFormat('Y-m-d', $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $keyword
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function parseKeyword(string $keyword): Carbon
|
||||
{
|
||||
$today = Carbon::today()->startOfDay();
|
||||
|
||||
return match ($keyword) {
|
||||
default => $today,
|
||||
'yesterday' => $today->subDay(),
|
||||
'tomorrow' => $today->addDay(),
|
||||
'start of this week' => $today->startOfWeek(),
|
||||
'end of this week' => $today->endOfWeek(),
|
||||
'start of this month' => $today->startOfMonth(),
|
||||
'end of this month' => $today->endOfMonth(),
|
||||
'start of this quarter' => $today->startOfQuarter(),
|
||||
'end of this quarter' => $today->endOfQuarter(),
|
||||
'start of this year' => $today->startOfYear(),
|
||||
'end of this year' => $today->endOfYear(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param Carbon $journalDate
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function parseMonthRange(string $date, Carbon $journalDate): array
|
||||
{
|
||||
// because 31 would turn February into March unexpectedly and the exact day is irrelevant here.
|
||||
$day = $journalDate->format('d');
|
||||
if ((int) $day > 28) {
|
||||
$day = '28';
|
||||
}
|
||||
|
||||
// format of string is xxxx-MM-xx
|
||||
$validDate = str_replace(['xxxx'], [$journalDate->year], $date);
|
||||
$validDate = str_replace(['xx'], [$day], $validDate);
|
||||
Log::debug(sprintf('parseMonthRange: Parsed "%s" into "%s"', $date, $validDate));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $validDate)->startOfMonth();
|
||||
$end = Carbon::createFromFormat('Y-m-d', $validDate)->endOfMonth();
|
||||
|
||||
return [
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param Carbon $journalDate
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function parseMonthYearRange(string $date, Carbon $journalDate): array
|
||||
{
|
||||
// because 31 would turn February into March unexpectedly and the exact day is irrelevant here.
|
||||
$day = $journalDate->format('d');
|
||||
if ((int) $day > 28) {
|
||||
$day = '28';
|
||||
}
|
||||
|
||||
// format of string is YYYY-MM-xx
|
||||
$validDate = str_replace(['xx'], [$day], $date);
|
||||
Log::debug(sprintf('parseMonthYearRange: Parsed "%s" into "%s"', $date, $validDate));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $validDate)->startOfMonth();
|
||||
$end = Carbon::createFromFormat('Y-m-d', $validDate)->endOfMonth();
|
||||
|
||||
return [
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
protected function parseRelativeDate(string $date): Carbon
|
||||
{
|
||||
Log::debug(sprintf('Now in parseRelativeDate("%s")', $date));
|
||||
$parts = explode(' ', $date);
|
||||
$today = Carbon::today()->startOfDay();
|
||||
$functions = [
|
||||
[
|
||||
'd' => 'subDays',
|
||||
'w' => 'subWeeks',
|
||||
'm' => 'subMonths',
|
||||
'q' => 'subQuarters',
|
||||
'y' => 'subYears',
|
||||
], [
|
||||
'd' => 'addDays',
|
||||
'w' => 'addWeeks',
|
||||
'm' => 'addMonths',
|
||||
'q' => 'addQuarters',
|
||||
'y' => 'addYears',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($parts as $part) {
|
||||
Log::debug(sprintf('Now parsing part "%s"', $part));
|
||||
$part = trim($part);
|
||||
|
||||
// verify if correct
|
||||
$pattern = '/[+-]\d+[wqmdy]/';
|
||||
$res = preg_match($pattern, $part);
|
||||
if (0 === $res || false === $res) {
|
||||
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);
|
||||
if (!array_key_exists($period, $functions[$direction])) {
|
||||
Log::error(sprintf('No method for direction %d and period "%s".', $direction, $period));
|
||||
continue;
|
||||
}
|
||||
$func = $functions[$direction][$period];
|
||||
Log::debug(sprintf('Will now do %s(%d) on %s', $func, $number, $today->format('Y-m-d')));
|
||||
$today->$func($number);
|
||||
Log::debug(sprintf('Resulting date is %s', $today->format('Y-m-d')));
|
||||
|
||||
}
|
||||
|
||||
return $today;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
@@ -430,6 +387,27 @@ class ParseDateString
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param Carbon $journalDate
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function parseMonthDayRange(string $date, Carbon $journalDate): array
|
||||
{
|
||||
// Any year.
|
||||
// format of string is xxxx-MM-DD
|
||||
$validDate = str_replace(['xxxx'], [$journalDate->year], $date);
|
||||
Log::debug(sprintf('parseMonthDayRange: Parsed "%s" into "%s"', $date, $validDate));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $validDate)->startOfDay();
|
||||
$end = Carbon::createFromFormat('Y-m-d', $validDate)->endOfDay();
|
||||
|
||||
return [
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*
|
||||
@@ -449,27 +427,6 @@ class ParseDateString
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param Carbon $journalDate
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function parseMonthDayRange(string $date, Carbon $journalDate): array
|
||||
{
|
||||
// Any year.
|
||||
// format of string is xxxx-MM-DD
|
||||
$validDate = str_replace(['xxxx'], [$journalDate->year], $date);
|
||||
Log::debug(sprintf('parseMonthDayRange: Parsed "%s" into "%s"', $date, $validDate));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $validDate)->startOfDay();
|
||||
$end = Carbon::createFromFormat('Y-m-d', $validDate)->endOfDay();
|
||||
|
||||
return [
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param Carbon $journalDate
|
||||
@@ -491,4 +448,49 @@ class ParseDateString
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isMonthYearRange(string $date): bool
|
||||
{
|
||||
// if regex for YYYY-MM-xx:
|
||||
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-xx$/';
|
||||
if (preg_match($pattern, $date)) {
|
||||
Log::debug(sprintf('"%s" is a month/year range.', $date));
|
||||
|
||||
return true;
|
||||
}
|
||||
Log::debug(sprintf('"%s" is not a month/year range.', $date));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param Carbon $journalDate
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function parseMonthYearRange(string $date, Carbon $journalDate): array
|
||||
{
|
||||
// because 31 would turn February into March unexpectedly and the exact day is irrelevant here.
|
||||
$day = $journalDate->format('d');
|
||||
if ((int)$day > 28) {
|
||||
$day = '28';
|
||||
}
|
||||
|
||||
// format of string is YYYY-MM-xx
|
||||
$validDate = str_replace(['xx'], [$day], $date);
|
||||
Log::debug(sprintf('parseMonthYearRange: Parsed "%s" into "%s"', $date, $validDate));
|
||||
$start = Carbon::createFromFormat('Y-m-d', $validDate)->startOfMonth();
|
||||
$end = Carbon::createFromFormat('Y-m-d', $validDate)->endOfMonth();
|
||||
|
||||
return [
|
||||
'start' => $start,
|
||||
'end' => $end,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user