|
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 $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 $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 |
|
193
|
|
|
if (empty($timePeriods1) || 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
|
|
|
} elseif ($sub[0] <= $ori[0] && $ori[1] <= $sub[1]) { |
|
207
|
|
|
// Subtract all: --sub0--ori0--ori1--sub1-- |
|
208
|
|
|
return true; |
|
209
|
|
|
} elseif ($ori[0] < $sub[0] && $sub[1] < $ori[1]) { |
|
210
|
|
|
// Delete internal: --ori0--sub0--sub1--ori1-- |
|
211
|
|
|
return true; |
|
212
|
|
|
//} elseif ($sub[0] <= $ori[0] && $sub[1] <= $ori[1]) { // Complete condition |
|
213
|
|
|
} elseif ($sub[0] <= $ori[0]) { // Equivalent condition |
|
214
|
|
|
// Delete overlap: --sub0--ori0--sub1--ori1-- |
|
215
|
|
|
return true; |
|
216
|
|
|
//} elseif ($ori[0] <= $sub[0] && $ori[1] <= $sub[1]) { // Complete condition |
|
217
|
|
|
//} elseif ($ori[1] <= $sub[1]) { // Equivalent condition |
|
218
|
|
|
} else { // Equivalent condition |
|
219
|
|
|
// Delete overlap: --ori0--sub0--ori1--sub1-- |
|
220
|
|
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* The time period is in contact with the specified time (time period) |
|
230
|
|
|
* |
|
231
|
|
|
* @param array $timePeriods |
|
232
|
|
|
* @param string $sDateTime |
|
233
|
|
|
* @param string $eDateTime |
|
234
|
|
|
* @param string $sortOut |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
|
|
public static function contact(array $timePeriods, $sDateTime, $eDateTime = null, $sortOut = 'default') |
|
238
|
|
|
{ |
|
239
|
|
|
// Subject is empty, do nothing |
|
240
|
|
|
if (empty($timePeriods)) { |
|
241
|
|
|
return []; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
// Data sorting out |
|
245
|
|
|
self::dataSortOut($sortOut, $timePeriods); |
|
246
|
|
|
// Set $eDateTime |
|
247
|
|
|
$eDateTime = $eDateTime ?: $sDateTime; |
|
248
|
|
|
$sTime = min($sDateTime, $eDateTime); |
|
249
|
|
|
$eTime = max($sDateTime, $eDateTime); |
|
250
|
|
|
|
|
251
|
|
|
// Get Contact time periods |
|
252
|
|
|
$opt = []; |
|
253
|
|
|
foreach ($timePeriods as $k => $tp) { |
|
254
|
|
|
if ($eTime <= $tp[0]) { |
|
255
|
|
|
// No overlap && Passed: --$sTime--$eTime--$tp0--$tp1-- |
|
256
|
|
|
if ($sTime == $tp[0]) { |
|
257
|
|
|
// But |
|
258
|
|
|
$opt[] = $tp; |
|
259
|
|
|
} |
|
260
|
|
|
} elseif ($tp[1] <= $sTime) { |
|
261
|
|
|
// No overlap: --$tp0--$tp1--$sTime--$eTime-- |
|
262
|
|
|
} else { |
|
263
|
|
|
// Overlap |
|
264
|
|
|
$opt[] = $tp; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return $opt; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Time period greater than the specified time |
|
273
|
|
|
* |
|
274
|
|
|
* @param array $timePeriods |
|
275
|
|
|
* @param string $refDatetime |
|
276
|
|
|
* @param string $intactTime |
|
277
|
|
|
* @param string $sortOut |
|
278
|
|
|
* @return array |
|
279
|
|
|
*/ |
|
280
|
|
|
public static function greaterThan(array $timePeriods, $refDatetime, $intactTime = true, $sortOut = 'default') |
|
281
|
|
|
{ |
|
282
|
|
|
// Subject is empty, do nothing |
|
283
|
|
|
if (empty($timePeriods)) { |
|
284
|
|
|
return []; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
// Data sorting out |
|
288
|
|
|
self::dataSortOut($sortOut, $timePeriods); |
|
289
|
|
|
|
|
290
|
|
|
// Get Contact time periods |
|
291
|
|
|
$opt = []; |
|
292
|
|
|
foreach ($timePeriods as $k => $tp) { |
|
293
|
|
|
if ($intactTime) { |
|
294
|
|
|
// Time period is intact |
|
295
|
|
|
if ($tp[0] >= $refDatetime) { |
|
296
|
|
|
$opt[] = $tp; |
|
297
|
|
|
} |
|
298
|
|
|
} else { |
|
299
|
|
|
// Time period not intact |
|
300
|
|
|
if ($tp[1] > $refDatetime) { |
|
301
|
|
|
$opt[] = $tp; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
return $opt; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Time period less than the specified time |
|
311
|
|
|
* |
|
312
|
|
|
* @param array $timePeriods |
|
313
|
|
|
* @param string $refDatetime |
|
314
|
|
|
* @param string $intactTime |
|
315
|
|
|
* @param string $sortOut |
|
316
|
|
|
* @return array |
|
317
|
|
|
*/ |
|
318
|
|
|
public static function lessThan(array $timePeriods, $refDatetime, $intactTime = true, $sortOut = 'default') |
|
319
|
|
|
{ |
|
320
|
|
|
// Subject is empty, do nothing |
|
321
|
|
|
if (empty($timePeriods)) { |
|
322
|
|
|
return []; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
// Data sorting out |
|
326
|
|
|
self::dataSortOut($sortOut, $timePeriods); |
|
327
|
|
|
|
|
328
|
|
|
// Get Contact time periods |
|
329
|
|
|
$opt = []; |
|
330
|
|
|
foreach ($timePeriods as $k => $tp) { |
|
331
|
|
|
if ($intactTime) { |
|
332
|
|
|
// Time period is intact |
|
333
|
|
|
if ($tp[1] <= $refDatetime) { |
|
334
|
|
|
$opt[] = $tp; |
|
335
|
|
|
} |
|
336
|
|
|
} else { |
|
337
|
|
|
// Time period not intact |
|
338
|
|
|
if ($tp[0] < $refDatetime) { |
|
339
|
|
|
$opt[] = $tp; |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
return $opt; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* ******************************************** |
|
349
|
|
|
* ************** Tools Function ************** |
|
350
|
|
|
* ******************************************** |
|
351
|
|
|
*/ |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Data sorting out |
|
355
|
|
|
* |
|
356
|
|
|
* @param string|bool $sortOut |
|
357
|
|
|
* @param array $timePeriods1 |
|
358
|
|
|
* @param array|null $timePeriods2 |
|
359
|
|
|
* @return void |
|
360
|
|
|
*/ |
|
361
|
|
|
public static function dataSortOut(&$sortOut, &$timePeriods1, &$timePeriods2 = null) |
|
362
|
|
|
{ |
|
363
|
|
|
// Data sorting out |
|
364
|
|
|
$sortOut = $sortOut === 'default' ? self::getSortOut() : !!$sortOut; |
|
365
|
|
|
if ($sortOut) { |
|
366
|
|
|
$timePeriods1 = self::union($timePeriods1); |
|
367
|
|
|
if (!is_null($timePeriods2)) { |
|
368
|
|
|
$timePeriods2 = self::union($timePeriods2); |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
|