Code reordering and reformatting. I should really start employing style CI.

This commit is contained in:
James Cole
2021-09-18 10:26:12 +02:00
parent 9b9d52e99f
commit 4003cea759
344 changed files with 2776 additions and 2605 deletions

View File

@@ -159,6 +159,57 @@ class Navigation
return $periods;
}
/**
* @param Carbon $theDate
* @param string $repeatFreq
*
* @return Carbon
*/
public function startOfPeriod(Carbon $theDate, string $repeatFreq): Carbon
{
$date = clone $theDate;
$functionMap = [
'1D' => 'startOfDay',
'daily' => 'startOfDay',
'1W' => 'startOfWeek',
'week' => 'startOfWeek',
'weekly' => 'startOfWeek',
'month' => 'startOfMonth',
'1M' => 'startOfMonth',
'monthly' => 'startOfMonth',
'3M' => 'firstOfQuarter',
'quarter' => 'firstOfQuarter',
'quarterly' => 'firstOfQuarter',
'year' => 'startOfYear',
'yearly' => 'startOfYear',
'1Y' => 'startOfYear',
];
if (array_key_exists($repeatFreq, $functionMap)) {
$function = $functionMap[$repeatFreq];
$date->$function();
return $date;
}
if ('half-year' === $repeatFreq || '6M' === $repeatFreq) {
$month = $date->month;
$date->startOfYear();
if ($month >= 7) {
$date->addMonths(6);
}
return $date;
}
if ('custom' === $repeatFreq) {
return $date; // the date is already at the start.
}
Log::error(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $repeatFreq));
return $theDate;
}
/**
* @param Carbon $end
* @param string $repeatFreq
@@ -313,6 +364,29 @@ class Navigation
return $entries;
}
/**
* If the date difference between start and end is less than a month, method returns "Y-m-d". If the difference is less than a year,
* method returns "Y-m". If the date difference is larger, method returns "Y".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredCarbonFormat(Carbon $start, Carbon $end): string
{
$format = 'Y-m-d';
if ($start->diffInMonths($end) > 1) {
$format = 'Y-m';
}
if ($start->diffInMonths($end) > 12) {
$format = 'Y';
}
return $format;
}
/**
* @param Carbon $theDate
* @param string $repeatFrequency
@@ -354,29 +428,6 @@ class Navigation
}
/**
* If the date difference between start and end is less than a month, method returns "Y-m-d". If the difference is less than a year,
* method returns "Y-m". If the date difference is larger, method returns "Y".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredCarbonFormat(Carbon $start, Carbon $end): string
{
$format = 'Y-m-d';
if ($start->diffInMonths($end) > 1) {
$format = 'Y-m';
}
if ($start->diffInMonths($end) > 12) {
$format = 'Y';
}
return $format;
}
/**
* If the date difference between start and end is less than a month, method returns trans(config.month_and_day). If the difference is less than a year,
* method returns "config.month". If the date difference is larger, method returns "config.year".
@@ -470,57 +521,6 @@ class Navigation
return $format;
}
/**
* @param Carbon $theDate
* @param string $repeatFreq
*
* @return Carbon
*/
public function startOfPeriod(Carbon $theDate, string $repeatFreq): Carbon
{
$date = clone $theDate;
$functionMap = [
'1D' => 'startOfDay',
'daily' => 'startOfDay',
'1W' => 'startOfWeek',
'week' => 'startOfWeek',
'weekly' => 'startOfWeek',
'month' => 'startOfMonth',
'1M' => 'startOfMonth',
'monthly' => 'startOfMonth',
'3M' => 'firstOfQuarter',
'quarter' => 'firstOfQuarter',
'quarterly' => 'firstOfQuarter',
'year' => 'startOfYear',
'yearly' => 'startOfYear',
'1Y' => 'startOfYear',
];
if (array_key_exists($repeatFreq, $functionMap)) {
$function = $functionMap[$repeatFreq];
$date->$function();
return $date;
}
if ('half-year' === $repeatFreq || '6M' === $repeatFreq) {
$month = $date->month;
$date->startOfYear();
if ($month >= 7) {
$date->addMonths(6);
}
return $date;
}
if ('custom' === $repeatFreq) {
return $date; // the date is already at the start.
}
Log::error(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $repeatFreq));
return $theDate;
}
/**
* @param Carbon $theDate
* @param string $repeatFreq