Completed
Push — master ( b0b9ef...895ca9 )
by Andreas
02:36
created

DateIntervalComparator::safecheck()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 11
nc 6
nop 1
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
class DateIntervalComparator
30
{
31
    protected $safe = false;
32
33
    public function safe($safe = true)
34
    {
35
        $this->safe = $safe;
36
    }
37
38
    /**
39
     * COmpare the two date intervals.
40
     *
41
     * If the first contains a larger timespan we return 1, if the second contains more
42
     * we return -1 and when they are equals we return 0.
43
     *
44
     * @param \DateInterval $first
45
     * @param \DateInterval $second
46
     *
47
     * @return int
48
     */
49
    public function compare(\DateInterval $first, \DateInterval $second)
50
    {
51
        if ($this->safe) {
52
            $this->safecheck($first);
53
            $this->safecheck($second);
54
        }
55
56
        if ($first->y > $second->y) {
57
            return 1;
58
        }
59
60
        if ($first->y < $second->y) {
61
            return -1;
62
        }
63
64
        if ($first->m > $second->m) {
65
            return 1;
66
        }
67
68
        if ($first->m < $second->m) {
69
            return -1;
70
        }
71
72
        if ($first->d > $second->d) {
73
            return 1;
74
        }
75
76
        if ($first->d < $second->d) {
77
            return -1;
78
        }
79
80
        if ($first->h > $second->h) {
81
            return 1;
82
        }
83
84
        if ($first->h < $second->h) {
85
            return -1;
86
        }
87
88
        if ($first->i > $second->i) {
89
            return 1;
90
        }
91
92
        if ($first->i < $second->i) {
93
            return -1;
94
        }
95
96
        if ($first->s > $second->s) {
97
            return 1;
98
        }
99
100
        if ($first->s < $second->s) {
101
            return -1;
102
        }
103
104
        return 0;
105
    }
106
107
    protected function safecheck(\DateInterval $interval)
108
    {
109
        if ($interval->m > 12) {
110
            throw new \UnexpectedValueException('Month exceeds value 12');
111
        }
112
113
        if ($interval->d > 31) {
114
            throw new \UnexpectedValueException('Day exceeds value 31');
115
        }
116
117
        if ($interval->h > 24) {
118
            throw new \UnexpectedValueException('Hour exceeds value 24');
119
        }
120
121
        if ($interval->i > 60) {
122
            throw new \UnexpectedValueException('Minute exceeds value 60');
123
        }
124
125
        if ($interval->s > 60) {
126
            throw new \UnexpectedValueException('Day exceeds value 60');
127
        }
128
129
    }
130
}
131