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 __construct(bool $safe = false) |
37
|
|
|
{ |
38
|
|
|
$this->safe = $safe; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @deprecated Use the constructor injected safety flag */ |
42
|
|
|
public function safe($safe = true): void |
43
|
|
|
{ |
44
|
|
|
$this->safe = $safe; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* COmpare the two date intervals. |
49
|
|
|
* |
50
|
|
|
* If the first contains a larger timespan we return 1, if the second contains more |
51
|
|
|
* we return -1 and when they are equals we return 0. |
52
|
|
|
* |
53
|
|
|
* @param DateInterval $first |
54
|
|
|
* @param DateInterval $second |
55
|
|
|
* |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
|
|
public function compare(DateInterval $first, DateInterval $second): int |
59
|
|
|
{ |
60
|
|
|
if ($this->safe) { |
61
|
|
|
$this->safecheck($first); |
62
|
|
|
$this->safecheck($second); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (0 !== $year = $this->compareValue($first->y, $second->y)) { |
66
|
|
|
return $year; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (0 !== $month = $this->compareValue($first->m, $second->m)) { |
70
|
|
|
return $month; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (0 !== $day = $this->compareValue($first->d, $second->d)) { |
74
|
|
|
return $day; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (0 !== $hour = $this->compareValue($first->h, $second->h)) { |
78
|
|
|
return $hour; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (0 !== $minute = $this->compareValue($first->i, $second->i)) { |
82
|
|
|
return $minute; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (0 !== $second = $this->compareValue($first->s, $second->s)) { |
86
|
|
|
return $second; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return 0; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function compareValue(int $a, int $b): int |
93
|
|
|
{ |
94
|
|
|
if ($a < $b) { |
95
|
|
|
return -1; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($a > $b) { |
99
|
|
|
return 1; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return 0; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function safecheck(DateInterval $interval): void |
106
|
|
|
{ |
107
|
|
|
if ($interval->m > 12) { |
108
|
|
|
throw new UnexpectedValueException('Month exceeds value 12'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($interval->d > 31) { |
112
|
|
|
throw new UnexpectedValueException('Day exceeds value 31'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// 25 due to DST-Transitions |
116
|
|
|
if ($interval->h > 25) { |
117
|
|
|
throw new UnexpectedValueException('Hour exceeds value 25'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($interval->i > 60) { |
121
|
|
|
throw new UnexpectedValueException('Minute exceeds value 60'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// 61 due to leap-seconds |
125
|
|
|
if ($interval->s > 61) { |
126
|
|
|
throw new UnexpectedValueException('Second exceeds value 61'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|