Completed
Pull Request — master (#4)
by Andreas
02:02
created

DateIntervalComparator::compare()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.1475
c 0
b 0
f 0
cc 8
nc 14
nop 2
1
<?php
2
/**
3
 * Copyright (c) Andreas Heigl<[email protected]>
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to deal
6
 * in the Software without restriction, including without limitation the rights
7
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 * copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 * The above copyright notice and this permission notice shall be included in
11
 * all copies or substantial portions of the Software.
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 *
20
 * @author    Andreas Heigl<[email protected]>
21
 * @copyright Andreas Heigl
22
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
23
 * @since     11.10.2016
24
 * @link      http://github.com/heiglandreas/org.heigl.DateIntervalComparator
25
 */
26
27
namespace Org_Heigl\DateIntervalComparator;
28
29
use DateInterval;
30
use UnexpectedValueException;
31
32
class DateIntervalComparator
33
{
34
    private $safe = false;
35
36
    public function safe($safe = true)
37
    {
38
        $this->safe = $safe;
39
    }
40
41
    /**
42
     * COmpare the two date intervals.
43
     *
44
     * If the first contains a larger timespan we return 1, if the second contains more
45
     * we return -1 and when they are equals we return 0.
46
     *
47
     * @param DateInterval $first
48
     * @param DateInterval $second
49
     *
50
     * @return int
51
     */
52
    public function compare(DateInterval $first, DateInterval $second)
53
    {
54
        if ($this->safe) {
55
            $this->safecheck($first);
56
            $this->safecheck($second);
57
        }
58
59
        if (0 !== $year = $this->compareValue($first->y, $second->y)) {
60
            return $year;
61
        }
62
63
        if (0 !== $month = $this->compareValue($first->m, $second->m)) {
64
            return $month;
65
        }
66
67
        if (0 !== $day = $this->compareValue($first->d, $second->d)) {
68
            return $day;
69
        }
70
71
        if (0 !== $hour = $this->compareValue($first->h, $second->h)) {
72
            return $hour;
73
        }
74
75
        if (0 !== $minute = $this->compareValue($first->i, $second->i)) {
76
            return $minute;
77
        }
78
79
        if (0 !== $second = $this->compareValue($first->s, $second->s)) {
80
            return $second;
81
        }
82
83
        return 0;
84
    }
85
86
    private function compareValue(int $a, int $b): int
87
    {
88
        if ($a < $b) {
89
            return -1;
90
        }
91
92
        if ($a > $b) {
93
            return 1;
94
        }
95
96
        return 0;
97
    }
98
99
    private function safecheck(DateInterval $interval): void
100
    {
101
        if ($interval->m > 12) {
102
            throw new UnexpectedValueException('Month exceeds value 12');
103
        }
104
105
        if ($interval->d > 31) {
106
            throw new UnexpectedValueException('Day exceeds value 31');
107
        }
108
109
        // 25 due to DST-Transitions
110
        if ($interval->h > 25) {
111
            throw new UnexpectedValueException('Hour exceeds value 25');
112
        }
113
114
        if ($interval->i > 60) {
115
            throw new UnexpectedValueException('Minute exceeds value 60');
116
        }
117
118
        // 61 due to leap-seconds
119
        if ($interval->s > 61) {
120
            throw new UnexpectedValueException('Second exceeds value 61');
121
        }
122
    }
123
}
124