43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Services\WorkingDays\Utils;
|
||
|
|
||
|
use App\Services\WorkingDays\WorkingDayDeterminerInterface;
|
||
|
|
||
|
/**
|
||
|
* Determines if a given date is a working day -- on multiple working day determiners.
|
||
|
*/
|
||
|
class MultipleWorkingDayDeterminer implements WorkingDayDeterminerInterface
|
||
|
{
|
||
|
/**
|
||
|
* @param array<int, WorkingDayDeterminerInterface> $workingDayDeterminers
|
||
|
*/
|
||
|
public function __construct(
|
||
|
protected readonly array $workingDayDeterminers,
|
||
|
) {
|
||
|
}
|
||
|
|
||
|
public function isWorkingDay(\DateTimeImmutable $date): bool
|
||
|
{
|
||
|
foreach ($this->workingDayDeterminers as $workingDayDeterminer) {
|
||
|
if (!$workingDayDeterminer->isWorkingDay($date)) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return true; // All working day determiners returned true, so the date is a working day.
|
||
|
}
|
||
|
|
||
|
public function getWorkingDaysCount(\DateTimeImmutable $startDate, int $workingDays): int
|
||
|
{
|
||
|
$workingDaysCount = $workingDays;
|
||
|
|
||
|
foreach ($this->workingDayDeterminers as $workingDayDeterminer) {
|
||
|
$workingDaysCount = min($workingDaysCount, $workingDayDeterminer->getWorkingDaysCount($startDate, $workingDays));
|
||
|
}
|
||
|
|
||
|
return $workingDaysCount;
|
||
|
}
|
||
|
}
|