1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class CalendarTime |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
// Constants |
7
|
|
|
|
8
|
|
|
private static $hours_max = 23; |
9
|
|
|
private static $minutes_max = 59; |
10
|
|
|
private static $seconds_max = 59; |
11
|
|
|
|
12
|
|
|
// Attributes |
13
|
|
|
|
14
|
|
|
private $hours; |
15
|
|
|
private $minutes; |
16
|
|
|
private $seconds; |
17
|
|
|
|
18
|
|
|
// Constructor |
19
|
|
|
|
20
|
|
|
public function __construct($hours = 0, $minutes = 0, $seconds = 0) |
21
|
|
|
{ |
22
|
|
|
$this->setHours($hours); |
23
|
|
|
$this->setMinutes($minutes); |
24
|
|
|
$this->setSeconds($seconds); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// Functions |
28
|
|
|
|
29
|
|
|
public function getHours() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
return $this->hours; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getMinutes() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
return $this->minutes; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getSeconds() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
return $this->seconds; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setHours($hours) |
45
|
|
|
{ |
46
|
|
|
$this->setAttribute('hours', $hours, self::$hours_max); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setMinutes($minutes) |
50
|
|
|
{ |
51
|
|
|
$this->setAttribute('minutes', $minutes, self::$minutes_max); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setSeconds($seconds) |
55
|
|
|
{ |
56
|
|
|
$this->setAttribute('seconds', $seconds, self::$seconds_max); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function __less(CalendarTime $time) |
60
|
|
|
{ |
61
|
|
|
return $this->hours < $time->hours || ($this->hours == $time->hours && ($this->minutes < $time->minutes || ($this->minutes == $time->minutes && $this->seconds < $time->seconds))); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function add(CalendarTime $time) |
65
|
|
|
{ |
66
|
|
|
$hours = $this->hours + $time->hours; |
67
|
|
|
$minutes = $this->minutes + $time->minutes; |
68
|
|
|
$seconds = $this->seconds + $time->seconds; |
69
|
|
|
|
70
|
|
View Code Duplication |
if ($seconds > self::$seconds_max) { |
|
|
|
|
71
|
|
|
$factor = self::$seconds_max + 1; |
72
|
|
|
$minutes += intval($seconds / $factor); |
73
|
|
|
$seconds %= $factor; |
74
|
|
|
} |
75
|
|
View Code Duplication |
if ($minutes > self::$minutes_max) { |
|
|
|
|
76
|
|
|
$factor = self::$minutes_max + 1; |
77
|
|
|
$hours += intval($minutes / $factor); |
78
|
|
|
$minutes %= $factor; |
79
|
|
|
} |
80
|
|
|
$factor = self::$hours_max + 1; |
81
|
|
|
$hours %= $factor; |
82
|
|
|
|
83
|
|
|
return new CalendarTime($hours, $minutes, $seconds); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Private Functions |
87
|
|
|
|
88
|
|
|
private function setAttribute($name, $value, $max) |
89
|
|
|
{ |
90
|
|
|
if (is_numeric($value)) { |
91
|
|
|
if (is_int($value + 0)) { |
92
|
|
|
if ($value >= 0 && $value <= $max) { |
93
|
|
|
$this->$name = $value; |
94
|
|
|
} elseif ($value < 0) { |
95
|
|
|
user_error("CalendarTime::setAttribute() : you cannot set the \$$name attribute with a negative value", E_USER_ERROR); |
96
|
|
|
} else { |
97
|
|
|
user_error("CalendarTime::setAttribute() : you cannot set the \$$name attribute with a value more important than $max", E_USER_ERROR); |
98
|
|
|
} |
99
|
|
|
} else { |
100
|
|
|
user_error("CalendarTime::setAttribute() : you cannot set the \$$name attribute with a non integer value", E_USER_ERROR); |
101
|
|
|
} |
102
|
|
|
} else { |
103
|
|
|
user_error("CalendarTime::setAttribute() : you cannot set the \$$name attribute with a non numeric value", E_USER_ERROR); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.