1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Utility\Time; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Duration |
7
|
|
|
* @package Nip\Utility\Time |
8
|
|
|
*/ |
9
|
|
|
class Duration |
10
|
|
|
{ |
11
|
|
|
protected $value = null; |
12
|
|
|
protected $parts = null; |
13
|
|
|
protected $seconds = null; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Duration constructor. |
17
|
|
|
* |
18
|
|
|
* @param $duration |
19
|
|
|
*/ |
20
|
4 |
|
public function __construct($duration) |
21
|
|
|
{ |
22
|
4 |
|
if (is_numeric($duration)) { |
23
|
4 |
|
$this->setSeconds($duration); |
24
|
|
|
} |
25
|
4 |
|
if (is_string($duration)) { |
26
|
|
|
$this->setValue($duration); |
27
|
|
|
$this->parseSeconds(); |
28
|
|
|
} |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param null $value |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
public function setValue($value) |
35
|
|
|
{ |
36
|
|
|
$this->value = $value; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function parseSeconds() |
40
|
|
|
{ |
41
|
|
|
$parts = $this->getParts(); |
42
|
|
|
if (count($parts) == 3) { |
43
|
|
|
$seconds = 0; |
44
|
|
|
$seconds += $parts['h'] * 3600; |
45
|
|
|
$seconds += $parts['m'] * 60; |
46
|
|
|
$seconds += $parts['s']; |
47
|
|
|
$this->setSeconds($seconds); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return [] |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
public function getParts() |
55
|
|
|
{ |
56
|
|
|
if ($this->parts === null) { |
57
|
|
|
$this->parseParts(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->parts; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $parts |
65
|
|
|
*/ |
66
|
|
|
public function setParts($parts) |
67
|
|
|
{ |
68
|
|
|
$this->parts = $parts; |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
public function parseParts() |
72
|
|
|
{ |
73
|
4 |
|
$this->parts = []; |
74
|
4 |
|
if ($this->value && substr_count($this->value, ':') == 2) { |
75
|
|
|
$this->parsePartsFromString(); |
76
|
|
|
|
77
|
|
|
return; |
78
|
|
|
} |
79
|
4 |
|
if ($this->seconds > 0) { |
80
|
4 |
|
$this->parsePartsFromSeconds(); |
81
|
|
|
|
82
|
4 |
|
return; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function parsePartsFromString() |
87
|
|
|
{ |
88
|
|
|
list($hours, $minutes, $seconds) = explode(':', $this->value); |
89
|
|
|
|
90
|
|
|
$this->setHoursPart($hours); |
91
|
|
|
$this->setMinutesPart($minutes); |
92
|
|
|
$this->setSecondsPart($seconds); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $value |
97
|
|
|
*/ |
98
|
4 |
|
public function setHoursPart($value) |
99
|
|
|
{ |
100
|
4 |
|
$this->setPart('h', $value); |
101
|
4 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $p |
105
|
|
|
* @param string $v |
106
|
|
|
*/ |
107
|
4 |
|
public function setPart($p, $v) |
108
|
|
|
{ |
109
|
4 |
|
$this->parts[$p] = $v; |
110
|
4 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $v |
114
|
|
|
*/ |
115
|
4 |
|
public function setMinutesPart($v) |
116
|
|
|
{ |
117
|
4 |
|
$this->setPart('m', $v); |
118
|
4 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $v |
122
|
|
|
*/ |
123
|
4 |
|
public function setSecondsPart($v) |
124
|
|
|
{ |
125
|
4 |
|
$this->setPart('s', $v); |
126
|
4 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $v |
130
|
|
|
*/ |
131
|
4 |
|
public function setMicroPart($v) |
132
|
|
|
{ |
133
|
4 |
|
$this->setPart('ms', $v); |
134
|
4 |
|
} |
135
|
|
|
|
136
|
4 |
|
public function parsePartsFromSeconds() |
137
|
|
|
{ |
138
|
4 |
|
$seconds = $this->getSeconds(); |
139
|
4 |
|
if ($hours = intval((floor($seconds / 3600)))) { |
140
|
1 |
|
$seconds = $seconds - $hours * 3600; |
141
|
|
|
} |
142
|
|
|
|
143
|
4 |
|
$this->setHoursPart($hours); |
144
|
|
|
|
145
|
4 |
|
if ($minutes = intval((floor($seconds / 60)))) { |
146
|
4 |
|
$seconds = $seconds - $minutes * 60; |
147
|
|
|
} |
148
|
|
|
|
149
|
4 |
|
$this->setMinutesPart($minutes); |
150
|
|
|
|
151
|
4 |
|
$seconds = round($seconds, 2); |
152
|
4 |
|
$this->setSecondsPart(intval($seconds)); |
153
|
|
|
|
154
|
4 |
|
$micro = round($seconds - intval($seconds), 2); |
155
|
4 |
|
$this->setMicroPart($micro); |
156
|
4 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return double |
160
|
|
|
*/ |
161
|
4 |
|
public function getSeconds() |
162
|
|
|
{ |
163
|
4 |
|
if ($this->seconds === null) { |
164
|
|
|
$this->parseSeconds(); |
165
|
|
|
} |
166
|
|
|
|
167
|
4 |
|
return $this->seconds; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param null $seconds |
|
|
|
|
172
|
|
|
*/ |
173
|
4 |
|
public function setSeconds($seconds) |
174
|
|
|
{ |
175
|
4 |
|
$this->seconds = $seconds; |
176
|
4 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
4 |
|
public function getHoursPart() |
182
|
|
|
{ |
183
|
4 |
|
return $this->getPart('h'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $part |
188
|
|
|
* @param int $default |
189
|
|
|
* |
190
|
|
|
* @return int|mixed |
191
|
|
|
*/ |
192
|
4 |
|
public function getPart($part, $default = 0) |
193
|
|
|
{ |
194
|
4 |
|
if ($this->parts === null) { |
195
|
4 |
|
$this->parseParts(); |
196
|
|
|
} |
197
|
|
|
|
198
|
4 |
|
return isset($this->parts[$part]) ? $this->parts[$part] : $default; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
4 |
|
public function getMinutesPart() |
205
|
|
|
{ |
206
|
4 |
|
return $this->getPart('m'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
4 |
|
public function getSecondsPart() |
213
|
|
|
{ |
214
|
4 |
|
return $this->getPart('s'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
4 |
|
public function getMicroPart() |
221
|
|
|
{ |
222
|
4 |
|
return $this->getPart('ms'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
4 |
|
public function getDefaultString() |
229
|
|
|
{ |
230
|
4 |
|
$hours = str_pad($this->getHoursPart(), 2, 0, STR_PAD_LEFT); |
231
|
4 |
|
$minutes = str_pad($this->getMinutesPart(), 2, 0, STR_PAD_LEFT); |
232
|
4 |
|
$seconds = str_pad($this->getSecondsPart(), 2, 0, STR_PAD_LEFT); |
233
|
4 |
|
$micro = str_pad(str_replace('0.', '', $this->getMicroPart()), 2, 0, STR_PAD_LEFT); |
234
|
4 |
|
return $hours . ':' . $minutes . ':' . $seconds . '.' . $micro; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
public function getHTML() |
241
|
|
|
{ |
242
|
|
|
$return = '<time>'; |
243
|
|
|
|
244
|
|
|
$hours = str_pad($this->getHoursPart(), 2, 0, STR_PAD_LEFT); |
245
|
|
|
$return .= '<span class="hour">' . $hours . '</span>'; |
246
|
|
|
|
247
|
|
|
$minutes = str_pad($this->getMinutesPart(), 2, 0, STR_PAD_LEFT); |
248
|
|
|
$return .= '<span class="separator">:</span>'; |
249
|
|
|
$return .= '<span class="minutes">' . $minutes . '</span>'; |
250
|
|
|
|
251
|
|
|
$seconds = str_pad($this->getSecondsPart(), 2, 0, STR_PAD_LEFT); |
252
|
|
|
$return .= '<span class="separator">:</span>'; |
253
|
|
|
$return .= '<span class="seconds">' . $seconds . '</span>'; |
254
|
|
|
|
255
|
|
|
$micro = str_replace('0.', '', $this->getMicroPart()); |
256
|
|
|
$return .= '<span class="micro">.' . $micro . '</span>'; |
257
|
|
|
|
258
|
|
|
$return .= '</time>'; |
259
|
|
|
|
260
|
|
|
return $return; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public function getFormatedString() |
267
|
|
|
{ |
268
|
|
|
$return = ''; |
269
|
|
|
|
270
|
|
|
$hours = $this->getHoursPart(); |
271
|
|
|
if ($hours or $return) { |
272
|
|
|
$return .= ($return ? ' ' : '') . str_pad($hours, 2, 0, STR_PAD_LEFT) . 'h'; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$minutes = $this->getMinutesPart(); |
276
|
|
|
if ($minutes or $return) { |
277
|
|
|
$return .= ($return ? ' ' : '') . str_pad($minutes, 2, 0, STR_PAD_LEFT) . 'm'; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$seconds = $this->getSecondsPart(); |
281
|
|
|
if ($seconds) { |
282
|
|
|
$return .= ($return ? ' ' : '') . str_pad($seconds, 2, 0, STR_PAD_LEFT) . 's'; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $return; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|