Completed
Push — master ( c29848...6d2813 )
by Gabriel
02:24
created

Duration::setPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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
     * @param $duration
18
     */
19 3
    public function __construct($duration)
20
    {
21 3
        if (is_numeric($duration)) {
22 3
            $this->setSeconds($duration);
23
        }
24 3
        if (is_string($duration)) {
25
            $this->setValue($duration);
26
            $this->parseSeconds();
27
        }
28 3
    }
29
30
31
    /**
32
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
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
    public function getParts()
52
    {
53
        if ($this->_parts === null) {
54
            $this->parseParts();
55
        }
56
57
        return $this->_parts;
58
    }
59
60
    /**
61
     * @param array $parts
62
     */
63
    public function setParts($parts)
64
    {
65
        $this->_parts = $parts;
66
    }
67
68 3
    public function parseParts()
69
    {
70 3
        if ($this->_value && substr_count($this->_value, ':') == 2) {
71
            $this->parsePartsFromString();
72 3
        } elseif ($this->_seconds > 0) {
73 3
            $this->parsePartsFromSeconds();
74
        }
75 3
    }
76
77
    public function parsePartsFromString()
78
    {
79
        list($h, $m, $s) = explode(':', $this->_value);
80
81
        $this->setHoursPart($h);
82
        $this->setMinutesPart($m);
83
        $this->setSecondsPart($s);
84
    }
85
86
    /**
87
     * @param string $v
88
     */
89 3
    public function setHoursPart($v)
90
    {
91 3
        $this->setPart('h', $v);
92 3
    }
93
94
    /**
95
     * @param string $p
96
     * @param string $v
97
     */
98 3
    public function setPart($p, $v)
99
    {
100 3
        $this->_parts[$p] = $v;
101 3
    }
102
103
    /**
104
     * @param string $v
105
     */
106 3
    public function setMinutesPart($v)
107
    {
108 3
        $this->setPart('m', $v);
109 3
    }
110
111
    /**
112
     * @param string $v
113
     */
114 3
    public function setSecondsPart($v)
115
    {
116 3
        $this->setPart('s', $v);
117 3
    }
118
119
    /**
120
     * @param $v
121
     */
122 3
    public function setMicroPart($v)
123
    {
124 3
        $this->setPart('ms', $v);
125 3
    }
126
127 3
    public function parsePartsFromSeconds()
128
    {
129 3
        $seconds = $this->getSeconds();
130 3
        if ($hours = intval((floor($seconds / 3600)))) {
131 1
            $seconds = $seconds - $hours * 3600;
132
        }
133
134 3
        $this->setHoursPart($hours);
135
136 3
        if ($minutes = intval((floor($seconds / 60)))) {
137 3
            $seconds = $seconds - $minutes * 60;
138
        }
139
140 3
        $this->setMinutesPart($minutes);
141
142 3
        $seconds = round($seconds, 2);
143 3
        $this->setSecondsPart(intval($seconds));
144
145 3
        $micro = round($seconds - intval($seconds), 2);
146 3
        $this->setMicroPart($micro);
147 3
    }
148
149
    /**
150
     * @return double
151
     */
152 3
    public function getSeconds()
153
    {
154 3
        if ($this->_seconds === null) {
155
            $this->parseSeconds();
156
        }
157
158 3
        return $this->_seconds;
159
    }
160
161
    /**
162
     * @param null $seconds
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $seconds is correct as it would always require null to be passed?
Loading history...
163
     */
164 3
    public function setSeconds($seconds)
165
    {
166 3
        $this->_seconds = $seconds;
167 3
    }
168
169
    /**
170
     * @return string
171
     */
172 3
    public function getHoursPart()
173
    {
174 3
        return $this->getPart('h');
175
    }
176
177
    /**
178
     * @param string $p
179
     */
180 3
    public function getPart($p)
181
    {
182 3
        if ($this->_parts === null) {
183 3
            $this->parseParts();
184
        }
185
186 3
        return $this->_parts[$p];
187
    }
188
189
    /**
190
     * @return string
191
     */
192 3
    public function getMinutesPart()
193
    {
194 3
        return $this->getPart('m');
195
    }
196
197
    /**
198
     * @return string
199
     */
200 3
    public function getSecondsPart()
201
    {
202 3
        return $this->getPart('s');
203
    }
204
205
    /**
206
     * @return string
207
     */
208 3
    public function getMicroPart()
209
    {
210 3
        return $this->getPart('ms');
211
    }
212
213
    /**
214
     * @return string
215
     */
216 3
    public function getDefaultString()
217
    {
218 3
        $hours = str_pad($this->getHoursPart(), 2, 0, STR_PAD_LEFT);
219 3
        $minutes = str_pad($this->getMinutesPart(), 2, 0, STR_PAD_LEFT);
220 3
        $seconds = str_pad($this->getSecondsPart(), 2, 0, STR_PAD_LEFT);
221 3
        $micro = str_replace('0.', '', $this->getMicroPart());
222 3
        return $hours . ':' . $minutes . ':' . $seconds . '.' . $micro;
223
    }
224
225
    /**
226
     * @return string
227
     */
228
    public function getHTML()
229
    {
230
        $return = '<time>';
231
232
        $hours = str_pad($this->getHoursPart(), 2, 0, STR_PAD_LEFT);
233
        $return .= '<span class="hour">' . $hours . '</span>';
234
235
        $minutes = str_pad($this->getMinutesPart(), 2, 0, STR_PAD_LEFT);
236
        $return .= '<span class="separator">:</span>';
237
        $return .= '<span class="minutes">' . $minutes . '</span>';
238
239
        $seconds = str_pad($this->getSecondsPart(), 2, 0, STR_PAD_LEFT);
240
        $return .= '<span class="separator">:</span>';
241
        $return .= '<span class="seconds">' . $seconds . '</span>';
242
243
        $micro = str_replace('0.', '', $this->getMicroPart());
244
        $return .= '<span class="micro">.' . $micro . '</span>';
245
246
        $return .= '</time>';
247
248
        return $return;
249
    }
250
251
    /**
252
     * @return string
253
     */
254
    public function getFormatedString()
255
    {
256
        $return = '';
257
258
        $hours = $this->getHoursPart();
259
        if ($hours or $return) {
260
            $return .= ($return ? ' ' : '') . str_pad($hours, 2, 0, STR_PAD_LEFT) . 'h';
261
        }
262
263
        $minutes = $this->getMinutesPart();
264
        if ($minutes or $return) {
265
            $return .= ($return ? ' ' : '') . str_pad($minutes, 2, 0, STR_PAD_LEFT) . 'm';
266
        }
267
268
        $seconds = $this->getSecondsPart();
269
        if ($seconds) {
270
            $return .= ($return ? ' ' : '') . str_pad($seconds, 2, 0, STR_PAD_LEFT) . 's';
271
        }
272
273
        return $return;
274
    }
275
}
276