Completed
Push — master ( 6fc225...0d5e2f )
by Ariel
12:07
created

Duration::fixSingulars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Timegridio\Concierge;
4
5
/**
6
 * This class is an adapted version of DateInterval, original work by Mochamad Gufron.
7
 * 
8
 * A simple class to convert time duration to a human readable string
9
 *
10
 * @class Duration
11
 *
12
 * @author Mochamad Gufron
13
 *
14
 * @link mgufron.com
15
 * @contact [email protected]
16
 */
17
class Duration
18
{
19
    /**
20
     * @var int timestamp interval
21
     */
22
    public $interval = 0;
23
24
    /**
25
     * @var int save interval when using format mode
26
     */
27
    private $tempInterval = 0;
28
29
    /**
30
     * @var string used format for forming the output
31
     */
32
    public $format = '';
33
34
    /**
35
     * @param int $interval timestamp interval
36
     * @param \DateTime $interval the first date
37
     * @param \DateTime $last_date the last date so we can find the interval of the first and the last date
38
     */
39 10
    public function __construct($interval = 0, \DateTime $last_date = null)
0 ignored issues
show
Unused Code introduced by
The parameter $last_date is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41 10
        if ($interval instanceof \DateTime) {
42 1
            $first_interval = strtotime($interval->format('Y-m-d H:i:s'));
43 1
            $last_interval = strtotime($interval->format('Y-m-d H:i:s'));
44 1
            $interval = $last_interval - $first_interval;
45 1
        }
46 10
        $this->interval = $interval;
47 10
    }
48
49
    /**
50
     * @param int $interval set current time interval
51
     *
52
     * @return self object
53
     */
54
    public function setInterval($interval)
55
    {
56
        $this->interval = $interval;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $format set current format
63
     *
64
     * @return self object
65
     */
66
    public function setFormat($format)
67
    {
68
        $this->format = $format;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @param int $round_method rounding method after we reveal the readable 
75
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
76
     *
77
     * @return float in seconds
78
     */
79 6
    public function getSeconds($round_method = null)
80
    {
81 6
        $result = $this->interval / 1000;
82
83 6
        return $this->round($result, $round_method);
84
    }
85
86
    /**
87
     * @param int $round_method rounding method after we reveal the readable 
88
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
89
     *
90
     * @return float in minutes
91
     */
92 6
    public function getMinutes($round_method = null)
93
    {
94 6
        $result = $this->interval / (1000 * 60);
95
96 6
        return $this->round($result, $round_method);
97
    }
98
99
    /**
100
     * @param int $round_method rounding method after we reveal the readable 
101
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
102
     *
103
     * @return float in hours
104
     */
105 6
    public function getHours($round_method = null)
106
    {
107 6
        $result = $this->interval / (1000 * 60 * 60);
108
109 6
        return $this->round($result, $round_method);
110
    }
111
112
    /**
113
     * @param string $format rounding method after we reveal the readable 
114
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
115
     *
116
     * @return well formatted output
117
     */
118 4
    public function format($format = [])
119
    {
120 4
        $this->tempInterval = $this->interval;
121 4
        $hours = $this->getHours(PHP_ROUND_HALF_DOWN);
122 4
        $this->interval = $this->tempInterval % (1000 * 60 * 60);
123 4
        $minutes = $this->getMinutes(PHP_ROUND_HALF_DOWN);
124 4
        $this->interval = $this->tempInterval % (1000 * 60);
125 4
        $seconds = $this->getSeconds(PHP_ROUND_HALF_DOWN);
126 4
        $this->interval = $this->tempInterval;
127 4
        if (is_string($format)) {
128 2
            $result = strtr($format, [
129 2
            '{hours}'  => $hours,
130 2
            '{minutes}' => $minutes,
131 2
            '{seconds}' => $seconds,
132 2
        ]);
133 2
        } else {
134 2
            if ($seconds <= 0) {
135 2
                $format['{seconds}'] = '';
136 2
            }
137 2
            if ($minutes <= 0) {
138 2
                $format['{minutes}'] = '';
139 2
            }
140 2
            if ($hours <= 0) {
141 1
                $format['{hours}'] = '';
142 1
            }
143 2
            $format['{seconds}'] = strtr($format['{seconds}'], ['{seconds}' => $seconds]);
144 2
            $format['{minutes}'] = strtr($format['{minutes}'], ['{minutes}' => $minutes]);
145 2
            $format['{hours}'] = strtr($format['{hours}'], ['{hours}' => $hours]);
146 2
            $result = trim(strtr($format['template'], $format));
147
        }
148
149 4
        $result = $this->fixSingulars($result);
150
151 4
        return $result;
152
    }
153
154
    /**
155
     * @param int $round_method rounding method after we reveal the readable 
156
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
157
     *
158
     * @return float current number of the result. if it is using $round_method,
159
     * it will rounded up or down
160
     */
161 10
    private function round($result, $round_method = null)
162
    {
163 10
        if ($round_method === PHP_ROUND_HALF_UP) {
164 3
            $result = ceil($result);
165 10
        } elseif ($round_method === PHP_ROUND_HALF_DOWN) {
166 7
            $result = floor($result);
167 7
        }
168
169 10
        return $result;
170
    }
171
172
    /**
173
     * @return well formatted output
174
     */
175
    public function __toString()
176
    {
177
        return $this->format($this->format);
178
    }
179
180
    /**
181
     * Fix singulars of time units
182
     * 
183
     * @param  string $string
184
     * @return string  Formatted string with replaced plurals to singular
185
     */
186 4
    protected function fixSingulars($string)
187
    {
188 4
        $plurals = ['/^1 hours/', '/^1 minutes/', '/^1 seconds/'];
189 4
        $singulars = ['1 hour', '1 minute', '1 second'];
190
191 4
        return preg_replace($plurals, $singulars, $string);
192
    }
193
}
194