-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced WorkingHours interface and added ShiftsWorking Hours - add…
…ed simple readme (#2)
- Loading branch information
1 parent
3ccd3c6
commit e8b51a9
Showing
13 changed files
with
312 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Aeon/Calendar/Gregorian/BusinessHours/WorkingHours/LinearWorkingHours.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Aeon\Calendar\Gregorian\BusinessHours\WorkingHours; | ||
|
||
use Aeon\Calendar\Gregorian\BusinessHours\WorkingHours; | ||
use Aeon\Calendar\Gregorian\Time; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class LinearWorkingHours implements WorkingHours | ||
{ | ||
private Time $startHour; | ||
|
||
private Time $endHour; | ||
|
||
public function __construct(Time $startHour, Time $endHour) | ||
{ | ||
Assert::true($endHour->isGreaterThan($startHour), 'End hour needs to be greater than start hour'); | ||
$this->startHour = $startHour; | ||
$this->endHour = $endHour; | ||
} | ||
|
||
public function openFrom() : Time | ||
{ | ||
return $this->startHour; | ||
} | ||
|
||
public function openTo() : Time | ||
{ | ||
return $this->endHour; | ||
} | ||
|
||
public function isOpen(Time $time) : bool | ||
{ | ||
return $time->isGreaterThanEq($this->openFrom()) && | ||
$time->isLessThanEq($this->openTo()); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Aeon/Calendar/Gregorian/BusinessHours/WorkingHours/ShiftsWorkingHours.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Aeon\Calendar\Gregorian\BusinessHours\WorkingHours; | ||
|
||
use Aeon\Calendar\Gregorian\BusinessHours\WorkingHours; | ||
use Aeon\Calendar\Gregorian\Time; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class ShiftsWorkingHours implements WorkingHours | ||
{ | ||
/** | ||
* @var array<int, LinearWorkingHours> | ||
*/ | ||
private array $workingHours; | ||
|
||
public function __construct(LinearWorkingHours ...$workingHours) | ||
{ | ||
Assert::greaterThan(\count($workingHours), 0, 'Shifts can\'t be empty'); | ||
|
||
\uasort( | ||
$workingHours, | ||
function (LinearWorkingHours $workingHoursA, LinearWorkingHours $workingHoursB) : int { | ||
return $workingHoursA->openFrom()->isLessThanEq($workingHoursB->openFrom()) | ||
? -1 | ||
: 1; | ||
} | ||
); | ||
|
||
$this->workingHours = \array_values($workingHours); | ||
} | ||
|
||
public function openFrom() : Time | ||
{ | ||
return $this->workingHours[0]->openFrom(); | ||
} | ||
|
||
public function openTo() : Time | ||
{ | ||
return $this->workingHours[\count($this->workingHours) - 1]->openTo(); | ||
} | ||
|
||
public function isOpen(Time $time) : bool | ||
{ | ||
foreach ($this->workingHours as $workingHours) { | ||
if ($workingHours->isOpen($time)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.