File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Availability/Types/TimeSlot.php
<?php
namespace App\Komma\Availability\Types;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;
/**
* @property Carbon $start
* @property Carbon $end
* @property Carbon $visualEnd
* @property bool $locked
*
* Class TimeSlot
*/
final class TimeSlot implements JsonSerializable, Arrayable
{
/** @var string */
private $timeSlotValue;
/** @var Carbon */
public $start;
/** @var Carbon */
public $end;
/** @var Carbon */
public $visualEnd;
/** @var bool */
public $locked = false;
/** @var bool */
public $selected = false;
/**
* Make the Time Slot
*
* TimeSlot constructor.
* @param Carbon $start
* @param Carbon $end
* @param Carbon $visualEnd
*/
public function __construct(Carbon $start, Carbon $end, Carbon $visualEnd)
{
$this->start = $start;
$this->end = $end;
$this->visualEnd = $visualEnd;
$this->makeTimeSlotValue();
}
/**
* Create TimeSlot by date and timeSlotValue
*
* @param Carbon $date
* @param string $timeSlotValue
* @param float $visualDuration
* @return TimeSlot
*/
public static function create(Carbon $date, string $timeSlotValue, float $visualDuration): self
{
[$startTime, $endTime] = explode('-', $timeSlotValue);
$startDateTime = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $date->format('Y-m-d').' '.$startTime.':00');
$endDateTime = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $date->format('Y-m-d').' '.$endTime.':00');
$visualEndTime = clone $startDateTime;
$visualEndTime->addMinutes($visualDuration * 60);
return new self($startDateTime, $endDateTime, $visualEndTime);
}
/**
* Makes the time slot value
*/
public function makeTimeSlotValue()
{
// Make sure that the location and the date is defined before calling the method
if (! isset($this->start) || ! isset($this->end)) {
throw new \UnderflowException(self::class.': The start and end Carbon DateTime should be defined before calling this method.', 500);
}
$this->timeSlotValue = $this->start->format('H:i').'-'.$this->end->format('H:i');
}
/**
* Get the time slot value
*
* @return string
*/
public function getTimeSlotValue()
{
if (! isset($this->timeSlotValue)) {
throw new \UnderflowException(self::class.': The time slot value has not been generated. Make sure that it is before calling this function.', 500);
}
return $this->timeSlotValue;
}
/**
* @param TimeSlot $timeSlot
* @return object
*/
public static function vueOption(self $timeSlot)
{
return (object) [
'slot' => $timeSlot->getTimeSlotValue(),
'label' => $timeSlot->start->format('H.i').'u - '.$timeSlot->visualEnd->format('H.i').'u',
'available' => ! $timeSlot->locked,
];
}
public function toArray(): array
{
return [
'timeSlotValue' => $this->timeSlotValue,
'start' => $this->start->toArray(),
'end' => $this->end->toArray(),
'visualEnd' => $this->visualEnd->toArray(),
'locked' => $this->locked,
'selected' => $this->selected,
];
}
public function jsonSerialize()
{
return $this->toArray();
}
}