1 | <?php |
||
18 | class TimeSpan |
||
19 | { |
||
20 | /** @var DateTime */ |
||
21 | private $startDate; |
||
22 | /** @var DateTime */ |
||
23 | private $endDate; |
||
24 | /** @var DateInterval */ |
||
25 | private $dateInterval; |
||
26 | |||
27 | /** |
||
28 | * PRIVATE constructor |
||
29 | */ |
||
30 | private function __construct() |
||
31 | { |
||
32 | |||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param integer $years |
||
37 | * @param integer $months |
||
38 | * @param integer $days |
||
39 | * @return TimeSpan |
||
40 | */ |
||
41 | public static function asDuration( |
||
42 | $years, |
||
43 | $months = 0, |
||
44 | $days = 0 |
||
45 | ) { |
||
46 | foreach (func_get_args() as $arg) { |
||
47 | Helpers::checkIfNotNegativeNumberOrThrowAnException($arg); |
||
48 | } |
||
49 | |||
50 | $newThis = new TimeSpan(); |
||
51 | $newThis->newDateIntervalAbsolute($years, $months, $days); |
||
52 | |||
53 | return $newThis; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param DateTime $startDate |
||
58 | * @param $years |
||
59 | * @param $months |
||
60 | * @param int $days |
||
61 | * @return TimeSpan |
||
62 | */ |
||
63 | public static function asDurationWithStartDate( |
||
74 | |||
75 | /** |
||
76 | * @param DateTime $startDate |
||
77 | * @param DateTime $endDate |
||
78 | * @return TimeSpan |
||
79 | */ |
||
80 | public static function asInterval( |
||
91 | |||
92 | /** |
||
93 | * @param DateTime $startDate |
||
94 | */ |
||
95 | public function setStartDate(DateTime $startDate) |
||
106 | |||
107 | /** |
||
108 | * @param DateTime $endDate |
||
109 | */ |
||
110 | public function setEndDate(DateTime $endDate) |
||
124 | |||
125 | /** |
||
126 | * @return DateTime |
||
127 | */ |
||
128 | public function getStartDate() |
||
132 | |||
133 | /** |
||
134 | * @return DateTime |
||
135 | */ |
||
136 | public function getEndDate() |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getYearsComponent() |
||
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getMonthsComponent() |
||
156 | |||
157 | /** |
||
158 | * @return string |
||
159 | */ |
||
160 | public function getDaysComponent() |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | * @throws Exception |
||
168 | */ |
||
169 | public function toYears() |
||
173 | |||
174 | /** |
||
175 | * @return string |
||
176 | * @throws Exception |
||
177 | */ |
||
178 | public function toMonths() |
||
182 | |||
183 | /** |
||
184 | * @return string |
||
185 | * @throws Exception |
||
186 | */ |
||
187 | public function toDays() |
||
194 | |||
195 | public function clearStartEndDate() |
||
199 | |||
200 | /** PRIVATE methods */ |
||
201 | |||
202 | /** |
||
203 | * @param DateInterval $dateInterval |
||
204 | */ |
||
205 | private function setDateInterval(DateInterval $dateInterval) |
||
209 | |||
210 | /** |
||
211 | * @param $years |
||
212 | * @param $months |
||
213 | * @param $days |
||
214 | */ |
||
215 | private function newDateIntervalAbsolute($years, $months, $days) |
||
226 | |||
227 | /** |
||
228 | * @param DateTime $startDate |
||
229 | * @param DateTime $endDate |
||
230 | */ |
||
231 | private function newDateIntervalDifference(DateTime $startDate, DateTime $endDate) |
||
239 | |||
240 | /** |
||
241 | * @param DateTime $startDate |
||
242 | * @param DateTime $endDate |
||
243 | */ |
||
244 | private function checkStartEndDateAndSetInterval(DateTime $startDate, DateTime $endDate) |
||
252 | |||
253 | /** |
||
254 | * @param DateInterval $dateInterval |
||
255 | * @return DateInterval |
||
256 | */ |
||
257 | private function roundDateInterval(DateInterval $dateInterval) |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | public function __toString() |
||
280 | } |
||
281 | } |
||
282 |