blocked_period.php
1 year ago
booked_period.php
1 year ago
booking_request.php
1 year ago
booking_resource.php
1 year ago
booking_slot.php
1 year ago
filter.php
1 year ago
process_action.php
1 year ago
process_event.php
1 year ago
role.php
1 year ago
router.php
1 year ago
step.php
1 year ago
stripe_connect_customer.php
1 year ago
time_period.php
1 year ago
user.php
1 year ago
work_period.php
1 year ago
time_period.php
65 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2022 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | namespace LatePoint\Misc; |
| 7 | |
| 8 | class TimePeriod{ |
| 9 | public int $start_time = 0; |
| 10 | public int $end_time = 0; |
| 11 | |
| 12 | function __construct($args = []){ |
| 13 | $allowed_props = self::allowed_props(); |
| 14 | foreach($args as $key => $arg){ |
| 15 | if(in_array($key, $allowed_props)) $this->$key = $arg; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | |
| 20 | public static function merge_periods(array $time_periods): array{ |
| 21 | if(!$time_periods) return []; |
| 22 | |
| 23 | $result = [$time_periods[0]]; |
| 24 | |
| 25 | for ($i = 0; $i < count($time_periods); $i++) { |
| 26 | $x1 = $time_periods[$i]->start_time; |
| 27 | $y1 = $time_periods[$i]->end_time; |
| 28 | $x2 = $result[count($result) - 1]->start_time; |
| 29 | $y2 = $result[count($result) - 1]->end_time; |
| 30 | |
| 31 | if ($y2 >= $x1) { |
| 32 | $result[count($result) - 1]->end_time = max($y1, $y2); |
| 33 | } else { |
| 34 | $result[] = new TimePeriod(['start_time' => $x1, 'end_time' => $y1]); |
| 35 | } |
| 36 | } |
| 37 | return $result; |
| 38 | } |
| 39 | |
| 40 | public static function create_from_work_period(WorkPeriod $work_period){ |
| 41 | return new TimePeriod(['start_time' => $work_period->start_time, 'end_time' => $work_period->end_time]); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param array $overlapped_time_periods |
| 46 | * @return TimePeriod |
| 47 | */ |
| 48 | public static function get_unified_period_from_overlapped_periods(array $overlapped_time_periods): TimePeriod{ |
| 49 | $bounds = []; |
| 50 | foreach($overlapped_time_periods as $time_period){ |
| 51 | $bounds[] = $time_period->start_time; |
| 52 | $bounds[] = $time_period->end_time; |
| 53 | } |
| 54 | return new TimePeriod(['start_time' => min($bounds), 'end_time' => max($bounds)]); |
| 55 | } |
| 56 | |
| 57 | public function check_if_overlaps(TimePeriod $check_time_period){ |
| 58 | return (($this->start_time < $check_time_period->end_time) && ($check_time_period->start_time < $this->end_time)); |
| 59 | } |
| 60 | |
| 61 | public static function allowed_props(): array{ |
| 62 | return ['start_time', |
| 63 | 'end_time']; |
| 64 | } |
| 65 | } |