Completed
Push — master ( 607e96...0530d4 )
by Mars
01:45
created

LogicalProcess::intersect()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 24
c 1
b 0
f 0
nc 9
nop 3
dl 0
loc 44
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
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
                } else {
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
216
        return false;
217
    }
218
219
    /**
220
     * The time period is in contact with the specified time (time period)
221
     * 
222
     * @param array $timePeriods
223
     * @param string $sDateTime
224
     * @param string $eDateTime
225
     * @param bool|string $sortOut
226
     * @return array
227
     */
228
    public static function contact(array $timePeriods, $sDateTime, $eDateTime = null, $sortOut = 'default')
229
    {
230
        // Subject is empty, do nothing
231
        if (empty($timePeriods)) {
232
            return [];
233
        }
234
235
        // Data sorting out
236
        self::dataSortOut($sortOut, $timePeriods);
237
        // Set $eDateTime
238
        $eDateTime = $eDateTime ?: $sDateTime;
239
        $sTime = min($sDateTime, $eDateTime);
240
        $eTime = max($sDateTime, $eDateTime);
241
242
        // Strip: No overlap && Passed: --$sTime--$eTime--$tp0--$tp1--
243
        $timePeriods = array_filter($timePeriods, function ($tp) use ($sTime, $eTime) {
244
            return $eTime <= $tp[0] && $sTime < $tp[0] ? false : true;
245
        });
246
247
        // Strip: No overlap: --$tp0--$tp1--$sTime--$eTime--
248
        $timePeriods = array_filter($timePeriods, function ($tp) use ($sTime, $eTime) {
0 ignored issues
show
Unused Code introduced by
The import $eTime is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
249
            return $tp[1] <= $sTime ? false : true;
250
        });
251
252
        return array_values($timePeriods);
253
    }
254
255
    /**
256
     * Time period greater than the specified time
257
     * 
258
     * @param array $timePeriods
259
     * @param string $refDatetime Specified time to compare against
260
     * @param bool $fullTimePeriod Get only the full time period
261
     * @param bool|string $sortOut
262
     * @return array
263
     */
264
    public static function greaterThan(array $timePeriods, $refDatetime, $fullTimePeriod = true, $sortOut = 'default')
265
    {
266
        // Subject is empty, do nothing
267
        if (empty($timePeriods)) {
268
            return [];
269
        }
270
271
        // Data sorting out
272
        self::dataSortOut($sortOut, $timePeriods);
273
274
        // Get Contact time periods
275
        $opt = [];
276
        foreach ($timePeriods as $k => $tp) {
277
            if ($fullTimePeriod) {
278
                // Time period is intact
279
                if ($tp[0] >= $refDatetime) {
280
                    $opt[] = $tp;
281
                }
282
            } else {
283
                // Time period not intact
284
                if ($tp[1] > $refDatetime) {
285
                    $opt[] = $tp;
286
                }
287
            }
288
        }
289
290
        return $opt;
291
    }
292
293
    /**
294
     * Time period less than the specified time
295
     * 
296
     * @param array $timePeriods
297
     * @param string $refDatetime Specified time to compare against
298
     * @param bool $fullTimePeriod Get only the full time period
299
     * @param bool|string $sortOut
300
     * @return array
301
     */
302
    public static function lessThan(array $timePeriods, $refDatetime, $fullTimePeriod = true, $sortOut = 'default')
303
    {
304
        // Subject is empty, do nothing
305
        if (empty($timePeriods)) {
306
            return [];
307
        }
308
309
        // Data sorting out
310
        self::dataSortOut($sortOut, $timePeriods);
311
312
        // Get Contact time periods
313
        $opt = [];
314
        foreach ($timePeriods as $k => $tp) {
315
            if ($fullTimePeriod) {
316
                // Time period is intact
317
                if ($tp[1] <= $refDatetime) {
318
                    $opt[] = $tp;
319
                }
320
            } else {
321
                // Time period not intact
322
                if ($tp[0] < $refDatetime) {
323
                    $opt[] = $tp;
324
                }
325
            }
326
        }
327
328
        return $opt;
329
    }
330
331
    /**
332
     * ********************************************
333
     * ************** Tools Function **************
334
     * ********************************************
335
     */
336
337
    /**
338
     * Data sorting out
339
     *
340
     * @param bool|string $sortOut
341
     * @param array $timePeriods1
342
     * @param array|null $timePeriods2
343
     * @return void
344
     */
345
    public static function dataSortOut(&$sortOut, &$timePeriods1, &$timePeriods2 = null)
346
    {
347
        // Data sorting out
348
        $sortOut = $sortOut === 'default' ? self::getSortOut() : !!$sortOut;
349
        if ($sortOut) {
350
            $timePeriods1 = self::union($timePeriods1);
351
            if (!is_null($timePeriods2)) {
352
                $timePeriods2 = self::union($timePeriods2);
353
            }
354
        }
355
    }
356
}
357