blocked_period.php
3 months ago
booked_period.php
3 months ago
booking_request.php
3 months ago
booking_resource.php
3 months ago
booking_slot.php
3 months ago
filter.php
3 months ago
process_action.php
3 months ago
process_event.php
3 months ago
role.php
3 months ago
router.php
3 months ago
step.php
3 months ago
stripe_connect_customer.php
3 months ago
time_period.php
3 months ago
user.php
3 months ago
work_period.php
3 months ago
time_period.php
87 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 ) ) { |
| 16 | $this->$key = $arg; |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | |
| 22 | public static function merge_periods( array $time_periods ): array { |
| 23 | if ( ! $time_periods ) { |
| 24 | return []; |
| 25 | } |
| 26 | |
| 27 | $result = [ $time_periods[0] ]; |
| 28 | |
| 29 | for ( $i = 0; $i < count( $time_periods ); $i++ ) { |
| 30 | $x1 = $time_periods[ $i ]->start_time; |
| 31 | $y1 = $time_periods[ $i ]->end_time; |
| 32 | $x2 = $result[ count( $result ) - 1 ]->start_time; |
| 33 | $y2 = $result[ count( $result ) - 1 ]->end_time; |
| 34 | |
| 35 | if ( $y2 >= $x1 ) { |
| 36 | $result[ count( $result ) - 1 ]->end_time = max( $y1, $y2 ); |
| 37 | } else { |
| 38 | $result[] = new TimePeriod( |
| 39 | [ |
| 40 | 'start_time' => $x1, |
| 41 | 'end_time' => $y1, |
| 42 | ] |
| 43 | ); |
| 44 | } |
| 45 | } |
| 46 | return $result; |
| 47 | } |
| 48 | |
| 49 | public static function create_from_work_period( WorkPeriod $work_period ) { |
| 50 | return new TimePeriod( |
| 51 | [ |
| 52 | 'start_time' => $work_period->start_time, |
| 53 | 'end_time' => $work_period->end_time, |
| 54 | ] |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param array $overlapped_time_periods |
| 60 | * @return TimePeriod |
| 61 | */ |
| 62 | public static function get_unified_period_from_overlapped_periods( array $overlapped_time_periods ): TimePeriod { |
| 63 | $bounds = []; |
| 64 | foreach ( $overlapped_time_periods as $time_period ) { |
| 65 | $bounds[] = $time_period->start_time; |
| 66 | $bounds[] = $time_period->end_time; |
| 67 | } |
| 68 | return new TimePeriod( |
| 69 | [ |
| 70 | 'start_time' => min( $bounds ), |
| 71 | 'end_time' => max( $bounds ), |
| 72 | ] |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | public function check_if_overlaps( TimePeriod $check_time_period ) { |
| 77 | return ( ( $this->start_time < $check_time_period->end_time ) && ( $check_time_period->start_time < $this->end_time ) ); |
| 78 | } |
| 79 | |
| 80 | public static function allowed_props(): array { |
| 81 | return [ |
| 82 | 'start_time', |
| 83 | 'end_time', |
| 84 | ]; |
| 85 | } |
| 86 | } |
| 87 |