Time24   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 36
eloc 73
c 2
b 0
f 0
dl 0
loc 159
rs 9.52

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setMinutes() 0 9 3
A __construct() 0 12 5
A toString() 0 9 2
C inInterval() 0 34 12
A getHours() 0 3 1
A setSeconds() 0 9 3
A getSeconds() 0 3 1
A getMinutes() 0 3 1
A setHours() 0 9 3
A set() 0 5 1
A validateTimeString() 0 14 4
1
<?php
2
namespace App\BxConsole;
3
4
class Time24 {
5
6
    private int $hours = 0;
7
    private int $minutes = 0;
8
    private int $seconds = 0;
9
10
    public function __construct($time = '') {
11
12
        $time = trim($time);
13
        if(empty($time)) {
14
            \App\BxConsole\EnvHelper::timeZoneSet();
15
            $time = date("H:i:s");
16
        }
17
        $timeparts = array_map('intval', explode(':', $time));
18
19
        if(isset($timeparts[0])) $this->setHours($timeparts[0]);
20
        if(isset($timeparts[1])) $this->setMinutes($timeparts[1]);
21
        if(isset($timeparts[2])) $this->setSeconds($timeparts[2]);
22
    }
23
24
    /**
25
     * @param int $hours
26
     * @param int $minutes
27
     * @param int $seconds
28
     */
29
    public function set(int $hours, int $minutes = 0, int $seconds = 0): void
30
    {
31
        $this->setHours($hours);
32
        $this->setMinutes($minutes);
33
        $this->setSeconds($seconds);
34
    }
35
36
    public function toString($seconds = true): string
37
    {
38
        $timeparts = [];
39
        $timeparts[] = sprintf("%02d", $this->hours);
40
        $timeparts[] = sprintf("%02d", $this->minutes);
41
        if($seconds) {
42
            $timeparts[] = sprintf("%02d", $this->seconds);
43
        }
44
        return join(':', $timeparts);
45
    }
46
47
    /**
48
     * @return int
49
     */
50
    public function getHours(): int
51
    {
52
        return $this->hours;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getMinutes(): int
59
    {
60
        return $this->minutes;
61
    }
62
63
    /**
64
     * @return int
65
     */
66
    public function getSeconds(): int
67
    {
68
        return $this->seconds;
69
    }
70
71
    /**
72
     * @param int $hours
73
     */
74
    public function setHours(int $hours): void
75
    {
76
        $hours = (int) $hours;
77
        if($hours < 0) {
78
            $hours = 0;
79
        } else if($hours > 23) {
80
            $hours = 23;
81
        }
82
        $this->hours = $hours;
83
    }
84
85
    /**
86
     * @param int $minutes
87
     */
88
    public function setMinutes(int $minutes): void
89
    {
90
        $minutes = (int) $minutes;
91
        if($minutes < 0) {
92
            $minutes = 0;
93
        } else if($minutes > 59) {
94
            $minutes = 59;
95
        }
96
        $this->minutes = $minutes;
97
    }
98
99
    /**
100
     * @param int $seconds
101
     */
102
    public function setSeconds(int $seconds): void
103
    {
104
        $seconds = (int) $seconds;
105
        if($seconds < 0) {
106
            $seconds = 0;
107
        } else if($seconds > 59) {
108
            $seconds = 59;
109
        }
110
        $this->seconds = $seconds;
111
    }
112
113
    public static function validateTimeString($str): ?string
114
    {
115
        $str = trim($str);
116
        if(strpos($str, ':') !== false) {
117
            $timeParts = explode(':', $str);
118
            if(count($timeParts) == 2) {
119
                $timeParts[2] = '00';
120
            }
121
            $str = join(':', $timeParts);
122
            if(preg_match("/^(?:2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$/", $str)) {
123
                return $str;
124
            }
125
        }
126
        return null;
127
    }
128
129
    public static function inInterval($minStrTime, $maxStrTime, $checkStrTime = ''): bool
130
    {
131
        $minTime = new self($minStrTime);
132
        if($maxStrTime == '00:00') {
133
            $maxStrTime = '23:59';
134
        }
135
        $maxTime = new self($maxStrTime);
136
        $checkTime = new self($checkStrTime);
137
138
        if($minTime->getHours() == $checkTime->getHours()) {
139
            if($minTime->getMinutes() == $checkTime->getMinutes()) {
140
                if($minTime->getSeconds() > $checkTime->getSeconds()) {
141
                    return false;
142
                }
143
            } else if($minTime->getMinutes() > $checkTime->getMinutes()) {
144
                return false;
145
            }
146
        } else if($minTime->getHours() > $checkTime->getHours()) {
147
            return false;
148
        }
149
150
        if($maxTime->getHours() == $checkTime->getHours()) {
151
            if($maxTime->getMinutes() == $checkTime->getMinutes()) {
152
                if($minTime->getSeconds() < $checkTime->getSeconds()) {
153
                    return false;
154
                }
155
            } else if($maxTime->getMinutes() < $checkTime->getMinutes()) {
156
                return false;
157
            }
158
        } else if($maxTime->getHours() < $checkTime->getHours()) {
159
            return false;
160
        }
161
162
        return true;
163
    }
164
}