-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Duration.php
191 lines (142 loc) · 4.96 KB
/
Duration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
namespace Gamez;
use DateInterval;
use DateTimeImmutable;
use Gamez\Duration\Exception\InvalidDuration;
class Duration implements \JsonSerializable
{
private const NONE = 'PT0S';
/**
* @var DateInterval
*/
private $value;
private function __construct(DateInterval $dateInterval)
{
$this->value = $this->normalizeInterval($dateInterval);
}
public static function make($value): self
{
if ($value instanceof DateInterval) {
return new self($value);
}
$stringValue = (string) $value;
if (preg_match('/^(\d+):(\d+)$/', $stringValue)) {
[$minutes, $seconds] = array_map('intval', explode(':', $stringValue));
return new self(new DateInterval("PT{$minutes}M{$seconds}S"));
}
if (preg_match('/^(\d+):(\d+):(\d+)$/', $stringValue)) {
[$hours, $minutes, $seconds] = array_map('intval', explode(':', $stringValue));
return new self(new DateInterval("PT{$hours}H{$minutes}M{$seconds}S"));
}
if (0 === strpos($stringValue, 'P')) {
return new self(new DateInterval($stringValue));
}
return new self(DateInterval::createFromDateString($stringValue));
}
public static function none(): self
{
return new self(new DateInterval(self::NONE));
}
public function withAdded($duration): self
{
$duration = $duration instanceof self ? $duration : self::make($duration);
$now = new DateTimeImmutable();
$then = $now->add($this->toDateInterval())->add($duration->toDateInterval());
return new self($then->diff($now, true));
}
public function withSubtracted($duration): self
{
$duration = $duration instanceof self ? $duration : self::make($duration);
$now = new DateTimeImmutable();
$then = $now->add($this->toDateInterval())->sub($duration->toDateInterval());
if ($then < $now) {
throw InvalidDuration::because('A duration cannot be smaller than zero');
}
return new self($then->diff($now, true));
}
public function toDateInterval(): DateInterval
{
return $this->value;
}
public function isLargerThan($other): bool
{
return 1 === $this->compareTo($other);
}
public function equals($other): bool
{
return 0 === $this->compareTo($other);
}
public function isSmallerThan($other): bool
{
return -1 === $this->compareTo($other);
}
public function multipliedBy($multiplicator): self
{
if ($multiplicator < 0) {
throw InvalidDuration::because('A duration cannot be multiplied with a value smaller than zero');
}
$now = new DateTimeImmutable();
$there = $now->add($this->toDateInterval());
$durationInSeconds = $there->getTimestamp() - $now->getTimestamp();
$result = (int) round($durationInSeconds * $multiplicator);
return self::make($result.' seconds');
}
public function dividedBy($divisor): self
{
$now = new DateTimeImmutable();
$there = $now->add($this->toDateInterval());
$durationInSeconds = $there->getTimestamp() - $now->getTimestamp();
$result = (int) round($durationInSeconds / $divisor);
return self::make($result.' seconds');
}
public function diff($other): self
{
$other = $other instanceof self ? $other : self::make($other);
$now = new DateTimeImmutable();
$here = $now->add($this->toDateInterval());
$there = $now->add($other->toDateInterval());
return new self($here->diff($there, true));
}
public function compareTo($other): int
{
$other = $other instanceof self ? $other : self::make($other);
$now = new DateTimeImmutable();
$here = $now->add($this->toDateInterval());
$there = $now->add($other->toDateInterval());
return $here <=> $there;
}
private function normalizeInterval(DateInterval $value): DateInterval
{
$now = new DateTimeImmutable();
$then = $now->add($value);
return $now->diff($then);
}
private function toDateIntervalSpec(): string
{
$value = $this->toDateInterval();
$spec = 'P';
$spec .= 0 !== $value->y ? '%yY' : '';
$spec .= 0 !== $value->m ? '%mM' : '';
$spec .= 0 !== $value->d ? '%dD' : '';
$spec .= 'T';
$spec .= 0 !== $value->h ? '%hH' : '';
$spec .= 0 !== $value->i ? '%iM' : '';
$spec .= 0 !== $value->s ? '%sS' : '';
if ('T' === substr($spec, -1)) {
$spec = substr($spec, 0, -1);
}
if ('P' === $spec) {
return self::NONE;
}
return $value->format($spec);
}
public function jsonSerialize()
{
return $this->toDateIntervalSpec();
}
public function __toString()
{
return $this->toDateIntervalSpec();
}
}