1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: namespace Jyxo\Time;
15:
16: /**
17: * Time composer used to compose a date/time part by part.
18: *
19: * @category Jyxo
20: * @package Jyxo\Time
21: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
22: * @license https://github.com/jyxo/php/blob/master/license.txt
23: * @author Martin Šamšula
24: */
25: class Composer
26: {
27: /**
28: * Maximal year.
29: *
30: * @var integer
31: */
32: const YEAR_MAX = 2037;
33:
34: /**
35: * Minimal year.
36: *
37: * @var integer
38: */
39: const YEAR_MIN = 1902;
40:
41: /**
42: * Day.
43: *
44: * @var integer
45: */
46: private $day = 0;
47:
48: /**
49: * Month.
50: *
51: * @var integer
52: */
53: private $month = 0;
54:
55: /**
56: * Year.
57: *
58: * @var integer
59: */
60: private $year = 0;
61:
62: /**
63: * Second.
64: *
65: * @var integer
66: */
67: private $second = 0;
68:
69: /**
70: * Minute.
71: *
72: * @var integer
73: */
74: private $minute = 0;
75:
76: /**
77: * Hour.
78: *
79: * @var integer
80: */
81: private $hour = 0;
82:
83: /**
84: * Returns the composed date/time.
85: *
86: * @return \Jyxo\Time\Time
87: * @throws \Jyxo\Time\ComposerException If the date is incomplete or invalid
88: */
89: public function getTime()
90: {
91: if ($this->month === 0 || $this->year === 0 || $this->day === 0) {
92: throw new ComposerException('Date not complete.', ComposerException::NOT_COMPLETE);
93: }
94:
95: // Checkdate checks if the provided day is valid. Month and year are validated in their getters.
96: // The year is between 1 and 32767 inclusive.
97: if (!checkdate($this->month, $this->day, $this->year)) {
98: throw new ComposerException('Day out of range.', ComposerException::INVALID);
99: }
100:
101: $time = mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
102: return new Time($time);
103: }
104:
105: /**
106: * Sets the day.
107: *
108: * @param integer $day Day of the month
109: * @return \Jyxo\Time\Composer
110: * @throws \Jyxo\Time\ComposerException If the provided day is invalid
111: */
112: public function setDay($day)
113: {
114: $day = (integer) $day;
115:
116: if ($day < 1 || $day > 31) {
117: throw new ComposerException('Day out of range.', ComposerException::DAY);
118: }
119:
120: $this->day = $day;
121:
122: return $this;
123: }
124:
125: /**
126: * Sets the month.
127: *
128: * @param integer $month Month
129: * @return \Jyxo\Time\Composer
130: * @throws \Jyxo\Time\ComposerException If the month is invalid.
131: */
132: public function setMonth($month)
133: {
134: $month = (integer) $month;
135:
136: if ($month < 1 || $month > 12) {
137: throw new ComposerException('Month out of range.', ComposerException::MONTH);
138: }
139:
140: $this->month = $month;
141:
142: return $this;
143: }
144:
145: /**
146: * Sets the year.
147: *
148: * @param integer $year Year
149: * @return \Jyxo\Time\Composer
150: * @throws \Jyxo\Time\ComposerException If the year is invalid.
151: */
152: public function setYear($year)
153: {
154: $year = (integer) $year;
155:
156: if ($year > self::YEAR_MAX || $year < self::YEAR_MIN) {
157: throw new ComposerException('Year out of range.', ComposerException::YEAR);
158: }
159:
160: $this->year = $year;
161:
162: return $this;
163: }
164:
165: /**
166: * Sets seconds.
167: *
168: * @param integer $second Seconds
169: * @return \Jyxo\Time\Composer
170: * @throws \Jyxo\Time\ComposerException If seconds are invalid.
171: */
172: public function setSecond($second)
173: {
174: $second = (integer) $second;
175:
176: if ($second < 0 || $second > 60) {
177: throw new ComposerException('Second out of range.', ComposerException::SECOND);
178: }
179:
180: $this->second = $second;
181:
182: return $this;
183: }
184:
185: /**
186: * Sets minutes.
187: *
188: * @param integer $minute Minutes
189: * @return \Jyxo\Time\Composer
190: * @throws \Jyxo\Time\ComposerException If minutes are invalid.
191: */
192: public function setMinute($minute)
193: {
194: $minute = (integer) $minute;
195:
196: if ($minute < 0 || $minute > 60) {
197: throw new ComposerException('Minute out of range.', ComposerException::MINUTE);
198: }
199:
200: $this->minute = $minute;
201:
202: return $this;
203: }
204:
205: /**
206: * Sets hours.
207: *
208: * @param integer $hour Hours
209: * @return \Jyxo\Time\Composer
210: * @throws \Jyxo\Time\ComposerException If hours are invalid.
211: */
212: public function setHour($hour)
213: {
214: $hour = (integer) $hour;
215:
216: if ($hour < 0 || $hour > 24) {
217: throw new ComposerException('Hour out of range.', ComposerException::HOUR);
218: }
219:
220: $this->hour = $hour;
221:
222: return $this;
223: }
224: }
225: