engelsystem/includes/view/ShiftCalendarLane.php

76 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2016-11-05 10:06:06 +01:00
<?php
namespace Engelsystem;
2023-01-03 22:19:03 +01:00
use Engelsystem\Models\Shifts\Shift;
2017-07-28 19:28:11 +02:00
use Exception;
2016-11-05 10:06:06 +01:00
/**
* Represents a single lane in a shifts calendar.
*/
2017-01-02 03:57:23 +01:00
class ShiftCalendarLane
{
2023-01-03 22:19:03 +01:00
/** @var Shift[] */
2017-01-02 03:57:23 +01:00
private $shifts = [];
2016-11-05 10:06:06 +01:00
2017-01-03 03:22:48 +01:00
/**
* ShiftCalendarLane constructor.
*
* @param string $header
*/
2023-01-03 22:19:03 +01:00
public function __construct(private $header)
2017-01-02 03:57:23 +01:00
{
}
2016-11-05 10:06:06 +01:00
2017-01-02 15:43:36 +01:00
/**
* Adds a shift to the lane, but only if it fits.
* Returns true on success.
*
2023-01-03 22:19:03 +01:00
* @param Shift $shift The shift to add
2017-07-28 19:28:11 +02:00
* @throws Exception if the shift doesn't fit into the lane.
2017-01-02 15:43:36 +01:00
*/
2023-01-03 22:19:03 +01:00
public function addShift(Shift $shift)
2017-01-02 15:43:36 +01:00
{
if ($this->shiftFits($shift)) {
$this->shifts[] = $shift;
return;
2017-01-02 15:43:36 +01:00
}
2017-12-25 23:12:52 +01:00
throw new Exception('Unable to add shift to shift calendar lane.');
2017-01-02 15:43:36 +01:00
}
2016-11-05 10:06:06 +01:00
2017-01-02 15:43:36 +01:00
/**
* Returns true if given shift fits into this lane.
*
2023-01-03 22:19:03 +01:00
* @param Shift $newShift
2017-01-03 03:22:48 +01:00
* @return bool
* @internal param array $shift The shift to fit into this lane
2017-01-02 15:43:36 +01:00
*/
2023-01-03 22:19:03 +01:00
public function shiftFits(Shift $newShift)
2017-01-02 15:43:36 +01:00
{
foreach ($this->shifts as $laneShift) {
2023-01-03 22:19:03 +01:00
if (!($newShift->start >= $laneShift->end || $newShift->end <= $laneShift->start)) {
2017-01-02 15:43:36 +01:00
return false;
}
}
2017-12-25 23:12:52 +01:00
2017-01-02 15:43:36 +01:00
return true;
}
2016-11-05 10:06:06 +01:00
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
public function getHeader()
{
return $this->header;
}
2016-11-05 10:06:06 +01:00
2017-01-03 03:22:48 +01:00
/**
2023-01-03 22:19:03 +01:00
* @return Shift[]
2017-01-03 03:22:48 +01:00
*/
2017-01-02 03:57:23 +01:00
public function getShifts()
{
return $this->shifts;
}
2016-11-05 10:06:06 +01:00
}