1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\Stepup\DateTime; |
20
|
|
|
|
21
|
|
|
use DateInterval; |
22
|
|
|
use DateTime as CoreDateTime; |
23
|
|
|
use Surfnet\Stepup\Exception\InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @SuppressWarnings(PHPMD.TooManyMethods) |
27
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
28
|
|
|
*/ |
29
|
|
|
class DateTime |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The 'c' format, expanded in separate format characters. This string can also be used with |
33
|
|
|
* `DateTime::createFromString()`. |
34
|
|
|
*/ |
35
|
|
|
const FORMAT = 'Y-m-d\\TH:i:sP'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Allows for mocking of time. |
39
|
|
|
* |
40
|
|
|
* @var self|null |
41
|
|
|
*/ |
42
|
|
|
private static $now; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var CoreDateTime |
46
|
|
|
*/ |
47
|
|
|
private $dateTime; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public static function now() |
53
|
|
|
{ |
54
|
|
|
return self::$now ?: new self(new CoreDateTime); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $string A date-time string formatted using `self::FORMAT` (eg. '2014-11-26T15:20:43+01:00'). |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
|
|
public static function fromString($string) |
62
|
|
|
{ |
63
|
|
|
if (!is_string($string)) { |
64
|
|
|
InvalidArgumentException::invalidType('string', 'dateTime', $string); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$dateTime = CoreDateTime::createFromFormat(self::FORMAT, $string); |
68
|
|
|
|
69
|
|
|
if ($dateTime === false) { |
70
|
|
|
throw new InvalidArgumentException('Date-time string could not be parsed: is it formatted correctly?'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new self($dateTime); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param CoreDateTime|null $dateTime |
78
|
|
|
*/ |
79
|
|
|
public function __construct(CoreDateTime $dateTime = null) |
80
|
|
|
{ |
81
|
|
|
$this->dateTime = $dateTime ?: new CoreDateTime(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param DateInterval $interval |
86
|
|
|
* @return DateTime |
87
|
|
|
*/ |
88
|
|
|
public function add(DateInterval $interval) |
89
|
|
|
{ |
90
|
|
|
$dateTime = clone $this->dateTime; |
91
|
|
|
$dateTime->add($interval); |
92
|
|
|
|
93
|
|
|
return new self($dateTime); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param DateInterval $interval |
98
|
|
|
* @return DateTime |
99
|
|
|
*/ |
100
|
|
|
public function sub(DateInterval $interval) |
101
|
|
|
{ |
102
|
|
|
$dateTime = clone $this->dateTime; |
103
|
|
|
$dateTime->sub($interval); |
104
|
|
|
|
105
|
|
|
return new self($dateTime); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return DateTime |
110
|
|
|
*/ |
111
|
|
|
public function endOfDay() |
112
|
|
|
{ |
113
|
|
|
$dateTime = clone $this->dateTime; |
114
|
|
|
$dateTime->setTime(23, 59, 59); |
115
|
|
|
|
116
|
|
|
return new self($dateTime); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param DateTime $dateTime |
121
|
|
|
* @return boolean |
122
|
|
|
*/ |
123
|
|
|
public function comesBefore(DateTime $dateTime) |
124
|
|
|
{ |
125
|
|
|
return $this->dateTime < $dateTime->dateTime; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param DateTime $dateTime |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
|
|
public function comesBeforeOrIsEqual(DateTime $dateTime) |
133
|
|
|
{ |
134
|
|
|
return $this->dateTime <= $dateTime->dateTime; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param DateTime $dateTime |
139
|
|
|
* @return boolean |
140
|
|
|
*/ |
141
|
|
|
public function comesAfter(DateTime $dateTime) |
142
|
|
|
{ |
143
|
|
|
return $this->dateTime > $dateTime->dateTime; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param DateTime $dateTime |
148
|
|
|
* @return boolean |
149
|
|
|
*/ |
150
|
|
|
public function comesAfterOrIsEqual(DateTime $dateTime) |
151
|
|
|
{ |
152
|
|
|
return $this->dateTime >= $dateTime->dateTime; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param $format |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function format($format) |
160
|
|
|
{ |
161
|
|
|
$formatted = $this->dateTime->format($format); |
162
|
|
|
|
163
|
|
|
if ($formatted === false) { |
164
|
|
|
throw new InvalidArgumentException(sprintf( |
165
|
|
|
'Given format "%s" is not a valid format for DateTime', |
166
|
|
|
$format |
167
|
|
|
)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $formatted; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return string An ISO 8601 representation of this DateTime. |
175
|
|
|
*/ |
176
|
|
|
public function __toString() |
177
|
|
|
{ |
178
|
|
|
return $this->format(self::FORMAT); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|