#39 - wip
This commit is contained in:
@@ -4,9 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
use Carbon\CarbonInterface;
|
||||
use Carbon\CarbonPeriod;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Toby\Domain\Enums\VacationRequestState;
|
||||
use Toby\Eloquent\Helpers\YearPeriodRetriever;
|
||||
@@ -20,33 +19,16 @@ class CalendarGenerator
|
||||
) {
|
||||
}
|
||||
|
||||
public function generate(YearPeriod $yearPeriod, string $month): array
|
||||
public function generate(Carbon $month): array
|
||||
{
|
||||
$date = CarbonImmutable::create($yearPeriod->year, $this->monthNameToNumber($month));
|
||||
$period = CarbonPeriod::create($date->startOfMonth(), $date->endOfMonth());
|
||||
$period = CarbonPeriod::create($month->copy()->startOfMonth(), $month->copy()->endOfMonth());
|
||||
$yearPeriod = YearPeriod::findByYear($month->year);
|
||||
|
||||
$holidays = $yearPeriod->holidays()->pluck("date");
|
||||
|
||||
return $this->generateCalendar($period, $holidays);
|
||||
}
|
||||
|
||||
protected function monthNameToNumber(string $name): int
|
||||
{
|
||||
return match ($name) {
|
||||
default => CarbonInterface::JANUARY,
|
||||
"february" => CarbonInterface::FEBRUARY,
|
||||
"march" => CarbonInterface::MARCH,
|
||||
"april" => CarbonInterface::APRIL,
|
||||
"may" => CarbonInterface::MAY,
|
||||
"june" => CarbonInterface::JUNE,
|
||||
"july" => CarbonInterface::JULY,
|
||||
"august" => CarbonInterface::AUGUST,
|
||||
"september" => CarbonInterface::SEPTEMBER,
|
||||
"october" => CarbonInterface::OCTOBER,
|
||||
"november" => CarbonInterface::NOVEMBER,
|
||||
"december" => CarbonInterface::DECEMBER,
|
||||
};
|
||||
}
|
||||
|
||||
protected function generateCalendar(CarbonPeriod $period, Collection $holidays): array
|
||||
{
|
||||
$calendar = [];
|
||||
|
59
app/Domain/Enums/Month.php
Normal file
59
app/Domain/Enums/Month.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Enums;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
enum Month: string
|
||||
{
|
||||
case January = "january";
|
||||
case February = "february";
|
||||
case March = "march";
|
||||
case April = "april";
|
||||
case May = "may";
|
||||
case June = "june";
|
||||
case July = "july";
|
||||
case August = "august";
|
||||
case September = "september";
|
||||
case October = "october";
|
||||
case November = "november";
|
||||
case December = "december";
|
||||
|
||||
public function toCarbonNumber(): int
|
||||
{
|
||||
return match ($this) {
|
||||
self::January => CarbonInterface::JANUARY,
|
||||
self::February => CarbonInterface::FEBRUARY,
|
||||
self::March => CarbonInterface::MARCH,
|
||||
self::April => CarbonInterface::APRIL,
|
||||
self::May => CarbonInterface::MAY,
|
||||
self::June => CarbonInterface::JUNE,
|
||||
self::July => CarbonInterface::JULY,
|
||||
self::August => CarbonInterface::AUGUST,
|
||||
self::September => CarbonInterface::SEPTEMBER,
|
||||
self::October => CarbonInterface::OCTOBER,
|
||||
self::November => CarbonInterface::NOVEMBER,
|
||||
self::December => CarbonInterface::DECEMBER,
|
||||
};
|
||||
}
|
||||
|
||||
public static function current(): Month
|
||||
{
|
||||
return Month::from(Str::lower(Carbon::now()->englishMonth));
|
||||
}
|
||||
|
||||
public static function fromNameOrCurrent(string $name): Month
|
||||
{
|
||||
$month = Month::tryFrom($name);
|
||||
|
||||
if ($month === null) {
|
||||
return Month::current();
|
||||
}
|
||||
|
||||
return $month;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user