firefly-iii/app/Support/Navigation.php
2015-02-07 06:49:24 +01:00

166 lines
4.4 KiB
PHP

<?php
namespace FireflyIII\Support;
use Carbon\Carbon;
use FireflyIII\Exception\FireflyException;
/**
* Class Navigation
*
* @package FireflyIII\Support
*/
class Navigation
{
public function jumpToNext($range, Carbon $date)
{
switch ($range) {
case '1D':
$date->endOfDay()->addDay();
break;
case '1W':
$date->endOfWeek()->addDay()->startOfWeek();
break;
case '1M':
$date->endOfMonth()->addDay()->startOfMonth();
break;
case '3M':
$date->lastOfQuarter()->addDay();
break;
case '6M':
if (intval($date->format('m')) >= 7) {
$date->startOfYear()->addYear();
} else {
$date->startOfYear()->addMonths(6);
}
break;
case '1Y':
$date->startOfYear()->addYear();
break;
default:
throw new FireflyException('Cannot do _next() on ' . $range);
break;
}
return $date;
}
public function jumpToPrevious($range, Carbon $date)
{
$functionMap = [
'1D' => 'Day',
'1W' => 'Week',
'1M' => 'Month',
'1Y' => 'Year'
];
if (isset($functionMap[$range])) {
$startFunction = 'startOf' . $functionMap[$range];
$subFunction = 'sub' . $functionMap[$range];
$date->$startFunction()->$subFunction();
return $date;
}
if ($range == '3M') {
$date->firstOfQuarter()->subMonths(3)->firstOfQuarter();
return $date;
}
if ($range == '6M') {
$month = intval($date->format('m'));
$date->startOfYear();
if ($month <= 6) {
$date->subMonths(6);
}
return $date;
}
throw new FireflyException('Cannot do _previous() on ' . $range);
}
public function periodName($range, Carbon $date)
{
$formatMap = [
'1D' => 'jS F Y',
'1W' => '\w\e\ek W, Y',
'1M' => 'F Y',
'1Y' => 'Y',
];
if (isset($formatMap[$range])) {
return $date->format($formatMap[$range]);
}
if ($range == '3M') {
$month = intval($date->format('m'));
return 'Q' . ceil(($month / 12) * 4) . ' ' . $date->format('Y');
}
if ($range == '6M') {
$month = intval($date->format('m'));
$half = ceil(($month / 12) * 2);
$halfName = $half == 1 ? 'first' : 'second';
return $halfName . ' half of ' . $date->format('Y');
}
throw new FireflyException('No _periodName() for range "' . $range . '"');
}
public function updateEndDate($range, Carbon $start)
{
$functionMap = [
'1D' => 'endOfDay',
'1W' => 'endOfWeek',
'1M' => 'endOfMonth',
'3M' => 'lastOfQuarter',
'1Y' => 'endOfYear',
];
$end = clone $start;
if (isset($functionMap[$range])) {
$function = $functionMap[$range];
$end->$function();
return $end;
}
if ($range == '6M') {
if (intval($start->format('m')) >= 7) {
$end->endOfYear();
} else {
$end->startOfYear()->addMonths(6);
}
return $end;
}
throw new FireflyException('updateEndDate cannot handle $range ' . $range);
}
public function updateStartDate($range, Carbon $start)
{
$functionMap = [
'1D' => 'startOfDay',
'1W' => 'startOfWeek',
'1M' => 'startOfMonth',
'3M' => 'firstOfQuarter',
'1Y' => 'startOfYear',
];
if (isset($functionMap[$range])) {
$function = $functionMap[$range];
$start->$function();
return $start;
}
if ($range == '6M') {
if (intval($start->format('m')) >= 7) {
$start->startOfYear()->addMonths(6);
} else {
$start->startOfYear();
}
return $start;
}
throw new FireflyException('updateStartDate cannot handle $range ' . $range);
}
}