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