1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace marsapp\helper\timeperiod\classes; |
4
|
|
|
|
5
|
|
|
use marsapp\helper\timeperiod\classes\Base; |
6
|
|
|
use marsapp\helper\timeperiod\classes\DataProcess; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Logical Process for Time Period Helper |
10
|
|
|
* |
11
|
|
|
* @author Mars Hung <[email protected]> |
12
|
|
|
* @see https://github.com/marshung24/TimePeriodHelper |
13
|
|
|
*/ |
14
|
|
|
class LogicalProcess extends Base |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ************************************************ |
19
|
|
|
* ************** Operation Function ************** |
20
|
|
|
* ************************************************ |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Union one or more time periods |
25
|
|
|
* |
26
|
|
|
* 1. Sort and merge one or more time periods with contacts |
27
|
|
|
* 2. TimePeriodHelper::union($timePeriods1, $timePeriods2, $timePeriods3, ......); |
28
|
|
|
* |
29
|
|
|
* @param array $timePeriods |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public static function union() |
33
|
|
|
{ |
34
|
|
|
$opt = []; |
35
|
|
|
|
36
|
|
|
// Combine and sort |
37
|
|
|
$merge = call_user_func_array('array_merge', func_get_args()); |
38
|
|
|
$merge = DataProcess::sort($merge); |
39
|
|
|
|
40
|
|
|
if (empty($merge)) { |
41
|
|
|
return $opt; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$tmp = array_shift($merge); |
45
|
|
|
foreach ($merge as $k => $tp) { |
46
|
|
|
if ($tp[0] > $tmp[1]) { |
47
|
|
|
// Got it, and set next. |
48
|
|
|
$opt[] = $tmp; |
49
|
|
|
$tmp = $tp; |
50
|
|
|
} elseif ($tp[1] > $tmp[1]) { |
51
|
|
|
// Extend end time |
52
|
|
|
$tmp[1] = $tp[1]; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
$opt[] = $tmp; |
56
|
|
|
|
57
|
|
|
return $opt; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Computes the difference of time periods |
62
|
|
|
* |
63
|
|
|
* 1. Compares $timePeriods1 against $timePeriods2 and returns the values in $timePeriods1 that are not present in $timePeriods2. |
64
|
|
|
* 2. e.g. TimePeriodHelper::diff($timePeriods1, $timePeriods2); |
65
|
|
|
* 3. 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. |
66
|
|
|
* |
67
|
|
|
* @param array $timePeriods1 |
68
|
|
|
* @param array $timePeriods2 |
69
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public static function diff(array $timePeriods1, array $timePeriods2, $sortOut = 'default') |
73
|
|
|
{ |
74
|
|
|
/*** Arguments prepare ***/ |
75
|
|
|
// Subject or pattern is empty, do nothing |
76
|
|
|
if (empty($timePeriods1) || empty($timePeriods2)) { |
77
|
|
|
return $timePeriods1; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Data sorting out |
81
|
|
|
self::dataSortOut($sortOut, $timePeriods1, $timePeriods2); |
82
|
|
|
|
83
|
|
|
$opt = []; |
84
|
|
|
foreach ($timePeriods1 as $k1 => $ori) { |
85
|
|
|
foreach ($timePeriods2 as $ko => $sub) { |
86
|
|
|
if ($sub[1] <= $ori[0]) { |
87
|
|
|
// No overlap && Passed: --sub0--sub1--ori0--ori1-- |
88
|
|
|
unset($timePeriods2[$ko]); |
89
|
|
|
continue; |
90
|
|
|
} elseif ($ori[1] <= $sub[0]) { |
91
|
|
|
// No overlap: --ori0--ori1--sub0--sub1-- |
92
|
|
|
continue; |
93
|
|
|
} elseif ($sub[0] <= $ori[0] && $ori[1] <= $sub[1]) { |
94
|
|
|
// Subtract all: --sub0--ori0--ori1--sub1-- |
95
|
|
|
$ori = []; |
96
|
|
|
break; |
97
|
|
|
} elseif ($ori[0] < $sub[0] && $sub[1] < $ori[1]) { |
98
|
|
|
// Delete internal: --ori0--sub0--sub1--ori1-- |
99
|
|
|
$opt[] = [$ori[0], $sub[0]]; |
100
|
|
|
$ori = [$sub[1], $ori[1]]; |
101
|
|
|
//} elseif ($sub[0] <= $ori[0] && $sub[1] <= $ori[1]) { // Complete condition |
102
|
|
|
} elseif ($sub[0] <= $ori[0]) { // Equivalent condition |
103
|
|
|
// Delete overlap: --sub0--ori0--sub1--ori1-- |
104
|
|
|
$ori = [$sub[1], $ori[1]]; |
105
|
|
|
//} elseif ($ori[0] <= $sub[0] && $ori[1] <= $sub[1]) { // Complete condition |
106
|
|
|
//} elseif ($ori[1] <= $sub[1]) { // Equivalent condition |
107
|
|
|
} else { // Equivalent condition |
108
|
|
|
// Delete overlap: --ori0--sub0--ori1--sub1-- |
109
|
|
|
$ori = [$ori[0], $sub[0]]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// All No overlap |
114
|
|
|
if (!empty($ori)) { |
115
|
|
|
$opt[] = $ori; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $opt; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Computes the intersection of time periods |
124
|
|
|
* |
125
|
|
|
* 1. e.g. TimePeriodHelper::intersect($timePeriods1, $timePeriods2); |
126
|
|
|
* 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. |
127
|
|
|
* |
128
|
|
|
* @param array $timePeriods1 |
129
|
|
|
* @param array $timePeriods2 |
130
|
|
|
* @param bool|string $sortOut Whether the input needs to be rearranged. Value: true, false, 'default'. If it is 'default', see getSortOut() |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public static function intersect(array $timePeriods1, array $timePeriods2, $sortOut = 'default') |
134
|
|
|
{ |
135
|
|
|
// Subject or pattern is empty, do nothing |
136
|
|
|
if (empty($timePeriods1) || empty($timePeriods2)) { |
137
|
|
|
return []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Data sorting out |
141
|
|
|
self::dataSortOut($sortOut, $timePeriods1, $timePeriods2); |
142
|
|
|
|
143
|
|
|
$opt = []; |
144
|
|
|
foreach ($timePeriods1 as $k1 => $ori) { |
145
|
|
|
foreach ($timePeriods2 as $ko => $sub) { |
146
|
|
|
if ($sub[1] <= $ori[0]) { |
147
|
|
|
// No overlap && Passed: --sub0--sub1--ori0--ori1-- |
148
|
|
|
unset($timePeriods2[$ko]); |
149
|
|
|
continue; |
150
|
|
|
} elseif ($ori[1] <= $sub[0]) { |
151
|
|
|
// No overlap: --ori0--ori1--sub0--sub1-- |
152
|
|
|
continue; |
153
|
|
|
} elseif ($sub[0] <= $ori[0] && $ori[1] <= $sub[1]) { |
154
|
|
|
// Subtract all: --sub0--ori0--ori1--sub1-- |
155
|
|
|
$opt[] = [$ori[0], $ori[1]]; |
156
|
|
|
break; |
157
|
|
|
} elseif ($ori[0] < $sub[0] && $sub[1] < $ori[1]) { |
158
|
|
|
// Delete internal: --ori0--sub0--sub1--ori1-- |
159
|
|
|
$opt[] = [$sub[0], $sub[1]]; |
160
|
|
|
$ori = [$sub[1], $ori[1]]; |
161
|
|
|
//} elseif ($sub[0] <= $ori[0] && $sub[1] <= $ori[1]) { // Complete condition |
162
|
|
|
} elseif ($sub[0] <= $ori[0]) { // Equivalent condition |
163
|
|
|
// Delete overlap: --sub0--ori0--sub1--ori1-- |
164
|
|
|
$opt[] = [$ori[0], $sub[1]]; |
165
|
|
|
$ori = [$sub[1], $ori[1]]; |
166
|
|
|
//} elseif ($ori[0] <= $sub[0] && $ori[1] <= $sub[1]) { // Complete condition |
167
|
|
|
//} elseif ($ori[1] <= $sub[1]) { // Equivalent condition |
168
|
|
|
} else { // Equivalent condition |
169
|
|
|
// Delete overlap: --ori0--sub0--ori1--sub1-- |
170
|
|
|
$opt[] = [$sub[0], $ori[1]]; |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $opt; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Time period is overlap |
181
|
|
|
* |
182
|
|
|
* 1. Determine if there is overlap between the two time periods |
183
|
|
|
* 2. Only when there is no intersection, no data is needed. |
184
|
|
|
* 3. Logic is similar to intersect. |
185
|
|
|
* |
186
|
|
|
* @param array $timePeriods1 |
187
|
|
|
* @param array $timePeriods2 |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public static function isOverlap(array $timePeriods1, array $timePeriods2) |
191
|
|
|
{ |
192
|
|
|
// Subject or pattern is empty, do nothing ($timePeriods1 is first loop, not need check) |
193
|
|
|
if (empty($timePeriods2)) { |
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
foreach ($timePeriods1 as $k1 => $ori) { |
198
|
|
|
foreach ($timePeriods2 as $ko => $sub) { |
199
|
|
|
if ($sub[1] <= $ori[0]) { |
200
|
|
|
// No overlap && Passed: --sub0--sub1--ori0--ori1-- |
201
|
|
|
unset($timePeriods2[$ko]); |
202
|
|
|
continue; |
203
|
|
|
} elseif ($ori[1] <= $sub[0]) { |
204
|
|
|
// No overlap: --ori0--ori1--sub0--sub1-- |
205
|
|
|
continue; |
206
|
|
|
} |
207
|
|
|
// --sub0--ori0--ori1--sub1-- |
208
|
|
|
// --ori0--sub0--sub1--ori1-- |
209
|
|
|
// --sub0--ori0--sub1--ori1-- |
210
|
|
|
// --ori0--sub0--ori1--sub1-- |
211
|
|
|
return true; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* The time period is in contact with the specified time (time period) |
220
|
|
|
* |
221
|
|
|
* @param array $timePeriods |
222
|
|
|
* @param string $sDateTime |
223
|
|
|
* @param string $eDateTime |
224
|
|
|
* @param bool|string $sortOut |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
public static function contact(array $timePeriods, $sDateTime, $eDateTime = null, $sortOut = 'default') |
228
|
|
|
{ |
229
|
|
|
// Subject is empty, do nothing |
230
|
|
|
if (empty($timePeriods)) { |
231
|
|
|
return []; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// Data sorting out |
235
|
|
|
self::dataSortOut($sortOut, $timePeriods); |
236
|
|
|
// Set $eDateTime |
237
|
|
|
$eDateTime = $eDateTime ?: $sDateTime; |
238
|
|
|
$sTime = min($sDateTime, $eDateTime); |
239
|
|
|
$eTime = max($sDateTime, $eDateTime); |
240
|
|
|
|
241
|
|
|
// Strip: No overlap && Passed: --$sTime--$eTime--$tp0--$tp1-- |
242
|
|
|
$timePeriods = array_filter($timePeriods, function ($tp) use ($sTime, $eTime) { |
243
|
|
|
return $eTime <= $tp[0] && $sTime < $tp[0] ? false : true; |
244
|
|
|
}); |
245
|
|
|
|
246
|
|
|
// Strip: No overlap: --$tp0--$tp1--$sTime--$eTime-- |
247
|
|
|
$timePeriods = array_filter($timePeriods, function ($tp) use ($sTime) { |
248
|
|
|
return $tp[1] <= $sTime ? false : true; |
249
|
|
|
}); |
250
|
|
|
|
251
|
|
|
return array_values($timePeriods); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Time period greater than the specified time |
256
|
|
|
* |
257
|
|
|
* @param array $timePeriods |
258
|
|
|
* @param string $refDatetime Specified time to compare against |
259
|
|
|
* @param bool $fullTimePeriod Get only the full time period |
260
|
|
|
* @param bool|string $sortOut |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
public static function greaterThan(array $timePeriods, $refDatetime, $fullTimePeriod = true, $sortOut = 'default') |
264
|
|
|
{ |
265
|
|
|
// Subject is empty, do nothing |
266
|
|
|
if (empty($timePeriods)) { |
267
|
|
|
return []; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
// Data sorting out |
271
|
|
|
self::dataSortOut($sortOut, $timePeriods); |
272
|
|
|
|
273
|
|
|
// Get Contact time periods |
274
|
|
|
$opt = []; |
275
|
|
|
foreach ($timePeriods as $k => $tp) { |
276
|
|
|
if ($fullTimePeriod) { |
277
|
|
|
// Time period is intact |
278
|
|
|
if ($tp[0] >= $refDatetime) { |
279
|
|
|
$opt[] = $tp; |
280
|
|
|
} |
281
|
|
|
} else { |
282
|
|
|
// Time period not intact |
283
|
|
|
if ($tp[1] > $refDatetime) { |
284
|
|
|
$opt[] = $tp; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $opt; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Time period less than the specified time |
294
|
|
|
* |
295
|
|
|
* @param array $timePeriods |
296
|
|
|
* @param string $refDatetime Specified time to compare against |
297
|
|
|
* @param bool $fullTimePeriod Get only the full time period |
298
|
|
|
* @param bool|string $sortOut |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
|
|
public static function lessThan(array $timePeriods, $refDatetime, $fullTimePeriod = true, $sortOut = 'default') |
302
|
|
|
{ |
303
|
|
|
// Subject is empty, do nothing |
304
|
|
|
if (empty($timePeriods)) { |
305
|
|
|
return []; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
// Data sorting out |
309
|
|
|
self::dataSortOut($sortOut, $timePeriods); |
310
|
|
|
|
311
|
|
|
// Get Contact time periods |
312
|
|
|
$opt = []; |
313
|
|
|
foreach ($timePeriods as $k => $tp) { |
314
|
|
|
if ($fullTimePeriod) { |
315
|
|
|
// Time period is intact |
316
|
|
|
if ($tp[1] <= $refDatetime) { |
317
|
|
|
$opt[] = $tp; |
318
|
|
|
} |
319
|
|
|
} else { |
320
|
|
|
// Time period not intact |
321
|
|
|
if ($tp[0] < $refDatetime) { |
322
|
|
|
$opt[] = $tp; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $opt; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* ******************************************** |
332
|
|
|
* ************** Tools Function ************** |
333
|
|
|
* ******************************************** |
334
|
|
|
*/ |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Data sorting out |
338
|
|
|
* |
339
|
|
|
* @param bool|string $sortOut |
340
|
|
|
* @param array $timePeriods1 |
341
|
|
|
* @param array|null $timePeriods2 |
342
|
|
|
* @return void |
343
|
|
|
*/ |
344
|
|
|
public static function dataSortOut(&$sortOut, &$timePeriods1, &$timePeriods2 = null) |
345
|
|
|
{ |
346
|
|
|
// Data sorting out |
347
|
|
|
$sortOut = $sortOut === 'default' ? self::getSortOut() : !!$sortOut; |
348
|
|
|
if ($sortOut) { |
349
|
|
|
$timePeriods1 = self::union($timePeriods1); |
350
|
|
|
if (!is_null($timePeriods2)) { |
351
|
|
|
$timePeriods2 = self::union($timePeriods2); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|