| 1 |
<?php |
| 2 |
namespace Booktics\Appointment\Schedule; |
| 3 |
|
| 4 |
use DateTime; |
| 5 |
|
| 6 |
/** |
| 7 |
* Schedule Entry Class |
| 8 |
*/ |
| 9 |
class Schedule_Entry { |
| 10 |
/** |
| 11 |
* Store start time |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private string $start_time; |
| 16 |
|
| 17 |
/** |
| 18 |
* Store end time |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private string $end_time; |
| 23 |
|
| 24 |
/** |
| 25 |
* Store day of week |
| 26 |
* |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
private int $day_of_week; |
| 30 |
|
| 31 |
/** |
| 32 |
* Store custom date |
| 33 |
* |
| 34 |
* @var string|null |
| 35 |
*/ |
| 36 |
private ?string $custom_date; |
| 37 |
|
| 38 |
/** |
| 39 |
* Store holiday checking |
| 40 |
* |
| 41 |
* @var bool |
| 42 |
*/ |
| 43 |
private bool $is_holiday; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor for ScheduleEntry |
| 47 |
* |
| 48 |
* @param array $data Schedule data |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function __construct(array $data) { |
| 53 |
$this->start_time = $data['start_time']; |
| 54 |
$this->end_time = $data['end_time']; |
| 55 |
$this->day_of_week = (int) $data['week_day']; |
| 56 |
$this->custom_date = $data['custom_date'] ?? null; |
| 57 |
$this->is_holiday = $this->start_time == 0 && $this->end_time == 0; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get start time |
| 62 |
* |
| 63 |
* @return string Start time value |
| 64 |
*/ |
| 65 |
public function get_start_time(): string { |
| 66 |
return $this->start_time; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get end time |
| 71 |
* |
| 72 |
* @return string End time value |
| 73 |
*/ |
| 74 |
public function get_end_time(): string { |
| 75 |
return $this->end_time; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get day of week |
| 80 |
* |
| 81 |
* @return int Day of week value |
| 82 |
*/ |
| 83 |
public function get_day_of_week(): int { |
| 84 |
return $this->day_of_week; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get custom date |
| 89 |
* |
| 90 |
* @return string|null Custom date value |
| 91 |
*/ |
| 92 |
public function get_custom_date(): ?string { |
| 93 |
return $this->custom_date; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Check if schedule entry is holiday |
| 98 |
* |
| 99 |
* @return bool True if holiday, false otherwise |
| 100 |
*/ |
| 101 |
public function is_holiday(): bool { |
| 102 |
return $this->is_holiday; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Compare date |
| 107 |
* |
| 108 |
* @param DateTime $date Schedule date time |
| 109 |
* |
| 110 |
* @return bool Return True if match date otherwise false |
| 111 |
*/ |
| 112 |
public function matches_date( DateTime $date ): bool { |
| 113 |
$dow = intval($date->format('N')); // 1 = Monday |
| 114 |
return $this->custom_date === $date->format('Y-m-d') || |
| 115 |
( $this->custom_date == 0 && (int)$this->day_of_week == $dow ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|