Passed
Push — master ( b06595...38eb84 )
by Mars
01:42
created

TimePeriodHelper::intersect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
namespace marsapp\helper\timeperiod;
4
5
use marsapp\helper\timeperiod\classes\LogicalProcess;
6
use marsapp\helper\timeperiod\classes\DataProcess;
7
8
/**
9
 * Time Period Helper
10
 * 
11
 * Note:
12
 * 1. Format: $timePeriods = [[$startDatetime1, $endDatetime1], [$startDatetime2, $endDatetime2], ...];
13
 * - $Datetime = Y-m-d H:i:s ; Y-m-d H:i:00 ; Y-m-d H:00:00 ;
14
 * 2. If it is hour/minute/second, the end point is usually not included, for example, 8 o'clock to 9 o'clock is 1 hour.
15
 * - ●=====○
16
 * 3. If it is a day/month/year, it usually includes an end point, for example, January to March is 3 months.
17
 * - ●=====●
18
 * 4. When processing, assume that the $timePeriods format is correct. If necessary, you need to call the verification function to verify the data.
19
 * 5. Ensure performance by keeping the $timePeriods format correct:
20
 * - a. When getting the raw $timePeriods, sort out it by format(), filter(), union().
21
 * - b. Handle $timePeriods using only the functions provided by TimePeriodHelper (Will not break the format, sort)
22
 * - c. When you achieve the two operations described above, you can turn off auto sort out (TimePeriodHelper::setSortOut(false)) to improve performance.
23
 * 
24
 * @version 0.5.3
25
 * @author Mars Hung <[email protected]>
26
 * @see https://github.com/marshung24/TimePeriodHelper
27
 */
28
class TimePeriodHelper extends LogicalProcess
29
{
30
31
    /**
32
     * ************************************************
33
     * ************** Operation Function **************
34
     * ************************************************
35
     */
36
37
    /**
38
     * Sort time periods (Order by ASC)
39
     * 
40
     * 1. When sorting, sort the start time first, if the start time is the same, then sort the end time
41
     * 2. Sort Priority: Start Time => End Time
42
     * 
43
     * @param array $timePeriods
44
     * @return array
45
     */
46
    public static function sort(array $timePeriods)
47
    {
48
        return DataProcess::sort($timePeriods);
49
    }
50
51
    /**
52
     * Fill time periods
53
     * 
54
     * Leaving only the first start time and the last end time
55
     * 
56
     * @param array $timePeriods
57
     * @return array
58
     */
59
    public static function fill(array $timePeriods)
60
    {
61
        return DataProcess::fill($timePeriods);
62
    }
63
64
    /**
65
     * Get gap time periods of multiple sets of time periods
66
     * 
67
     * 1. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct.
68
     * 
69
     * @param array $timePeriods
70
     * @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut()
71
     * @return array
72
     */
73
    public static function gap(array $timePeriods, $sortOut = 'default')
74
    {
75
        return DataProcess::gap($timePeriods, $sortOut);
76
    }
77
78
    /**
79
     * Calculation period total time
80
     *
81
     * 1. You can specify the smallest unit (from setUnit())
82
     * 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct.
83
     * 3. approximation: chop off
84
     *
85
     * @param array $timePeriods            
86
     * @param int $precision
87
     *            Optional decimal places for the decimal point
88
     * @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut()
89
     * @return number
90
     */
91
    public static function time(array $timePeriods, $precision = 0, $sortOut = 'default')
92
    {
93
        return DataProcess::time($timePeriods, $precision, $sortOut);
94
    }
95
96
    /**
97
     * Cut the time period of the specified length of time
98
     *
99
     * 1. You can specify the smallest unit (from setUnit())
100
     * 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct.
101
     * 
102
     * @param array $timePeriods            
103
     * @param number $time
104
     *            Specified length of time
105
     * @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut()
106
     * @return array
107
     */
108
    public static function cut(array $timePeriods, $time, $sortOut = 'default')
109
    {
110
        return DataProcess::cut($timePeriods, $time, $sortOut);
111
    }
112
113
    /**
114
     * Increase the time period of the specified length of time after the last time period
115
     *
116
     * 1. You can specify the smallest unit (from setUnit())
117
     * 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct.
118
     * 
119
     * @param array $timePeriods            
120
     * @param number $time
121
     *            Specified length of time (default uint:second)
122
     * @param number $interval
123
     *            Interval with existing time period
124
     * @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut()
125
     * @return array
126
     */
127
    public static function extend(array $timePeriods, $time, $interval = 0, $sortOut = 'default')
128
    {
129
        return DataProcess::extend($timePeriods, $time, $interval, $sortOut);
130
    }
131
132
    /**
133
     * Shorten the specified length of time from behind
134
     *
135
     * 1. You can specify the smallest unit (from setUnit())
136
     * 2. Whether $timePeriods is sorted out will affect the correctness of the results. Please refer to Note 5. Ensure performance by keeping the $timePeriods format correct.
137
     * 
138
     * @param array $timePeriods            
139
     * @param number $time
140
     *            Specified length of time (default uint:second)
141
     * @param bool $crossperiod
142
     *            Whether to shorten across time
143
     * @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut()
144
     * @return array
145
     */
146
    public static function shorten(array $timePeriods, $time, $crossperiod = true, $sortOut = 'default')
147
    {
148
        return DataProcess::shorten($timePeriods, $time, $crossperiod, $sortOut);
149
    }
150
151
    /**
152
     * Transform format
153
     * 
154
     * @param array $timePeriods
155
     * @param string $unit Time unit, if default,use class options setting
156
     * @return array
157
     */
158
    public static function format(array $timePeriods, $unit = 'default')
159
    {
160
        return DataProcess::format($timePeriods, $unit);
161
    }
162
163
    /**
164
     * Validate time period
165
     * 
166
     * Verify format, size, start/end time
167
     * 
168
     * @param mixed|array $timePeriods
169
     * @throws \Exception
170
     * @return bool
171
     */
172
    public static function validate($timePeriods)
173
    {
174
        return DataProcess::validate($timePeriods);
175
    }
176
177
    /**
178
     * Remove invalid time period
179
     * 
180
     * 1. Verify format, size, start/end time, and remove invalid.
181
     * 2. time carry problem processing, e.g. 2019-01-01 24:00:00 => 2019-01-02 00:00:00
182
     * 
183
     * @param mixed|array $timePeriods
184
     * @throws \Exception
185
     * @return array
186
     */
187
    public static function filter($timePeriods)
188
    {
189
        return DataProcess::filter($timePeriods);
190
    }
191
}
192