* change layout * change layout * #22 - wip * wip * wip * #22 - wip * #22 - wip * #22 - wip * #22 - wip * #22 - fix * #22 - wip * #22 - added some tests * #22 - wip * #22 - wip * #22 - fix * #22 - wip * #22 - wip * #22 - wip * #22 - fix * #22 - fix * #22 - fix * #22 - fix * #22 - fix * #22 - fix * #22 - cr fixes * #22 - cr fix Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
This commit is contained in:
80
app/Domain/CalendarGenerator.php
Normal file
80
app/Domain/CalendarGenerator.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
use Carbon\CarbonInterface;
|
||||
use Carbon\CarbonPeriod;
|
||||
use Illuminate\Support\Collection;
|
||||
use Toby\Domain\Enums\VacationRequestState;
|
||||
use Toby\Eloquent\Helpers\YearPeriodRetriever;
|
||||
use Toby\Eloquent\Models\Vacation;
|
||||
use Toby\Eloquent\Models\YearPeriod;
|
||||
|
||||
class CalendarGenerator
|
||||
{
|
||||
public function __construct(
|
||||
protected YearPeriodRetriever $yearPeriodRetriever,
|
||||
) {
|
||||
}
|
||||
|
||||
public function generate(YearPeriod $yearPeriod, string $month): array
|
||||
{
|
||||
$date = CarbonImmutable::create($yearPeriod->year, $this->monthNameToNumber($month));
|
||||
$period = CarbonPeriod::create($date->startOfMonth(), $date->endOfMonth());
|
||||
$holidays = $yearPeriod->holidays()->pluck("date");
|
||||
|
||||
return $this->generateCalendar($period, $holidays);
|
||||
}
|
||||
|
||||
protected function monthNameToNumber($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 = [];
|
||||
$vacations = $this->getVacationsForPeriod($period);
|
||||
|
||||
foreach ($period as $day) {
|
||||
$vacationsForDay = $vacations[$day->toDateString()] ?? new Collection();
|
||||
|
||||
$calendar[] = [
|
||||
"date" => $day->toDateString(),
|
||||
"dayOfMonth" => $day->translatedFormat("j"),
|
||||
"dayOfWeek" => $day->translatedFormat("D"),
|
||||
"isToday" => $day->isToday(),
|
||||
"isWeekend" => $day->isWeekend(),
|
||||
"isHoliday" => $holidays->contains($day),
|
||||
"vacations" => $vacationsForDay->pluck("user_id"),
|
||||
];
|
||||
}
|
||||
|
||||
return $calendar;
|
||||
}
|
||||
|
||||
protected function getVacationsForPeriod(CarbonPeriod $period): Collection
|
||||
{
|
||||
return Vacation::query()
|
||||
->whereBetween("date", [$period->start, $period->end])
|
||||
->whereRelation("vacationRequest", "state", VacationRequestState::Approved->value)
|
||||
->get()
|
||||
->groupBy(fn(Vacation $vacation) => $vacation->date->toDateString());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user