feat: add multiple working day determiner
This commit is contained in:
parent
9b907cf25d
commit
da77779525
@ -0,0 +1,42 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Services\WorkingDays\Utils;
|
||||
|
||||
use App\Services\WorkingDays\Utils\MultipleWorkingDayDeterminer;
|
||||
use App\Services\WorkingDays\WorkingDayDeterminerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MultipleWorkingDayDeterminerTest extends TestCase
|
||||
{
|
||||
public function testAllDeterminersReturnTrueForWorkingDay(): void
|
||||
{
|
||||
$date = new \DateTimeImmutable('2021-01-01');
|
||||
$mock = $this->createMock(WorkingDayDeterminerInterface::class);
|
||||
$mock->expects($this->once())->method('isWorkingDay')->with($date)->willReturn(true);
|
||||
$mock1 = $this->createMock(WorkingDayDeterminerInterface::class);
|
||||
$mock1->expects($this->once())->method('isWorkingDay')->with($date)->willReturn(true);
|
||||
$determiner = new MultipleWorkingDayDeterminer([
|
||||
$mock,
|
||||
$mock1,
|
||||
]);
|
||||
|
||||
$this->assertTrue($determiner->isWorkingDay($date));
|
||||
}
|
||||
|
||||
public function testFirstDeterminerReturnsFalseForWorkingDay(): void
|
||||
{
|
||||
$date = new \DateTimeImmutable('2021-01-01');
|
||||
$mock = $this->createMock(WorkingDayDeterminerInterface::class);
|
||||
$mock->expects($this->once())->method('isWorkingDay')->with($date)->willReturn(false);
|
||||
$mock1 = $this->createMock(WorkingDayDeterminerInterface::class);
|
||||
$mock1->expects($this->never())->method('isWorkingDay');
|
||||
$determiner = new MultipleWorkingDayDeterminer([
|
||||
$mock,
|
||||
$mock1,
|
||||
]);
|
||||
|
||||
$this->assertFalse($determiner->isWorkingDay($date));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user