Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Traits/Modifiers.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of the Carbon package.
7 *
8 * (c) Brian Nesbitt <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Carbon\Traits;
15
16 use Carbon\CarbonInterface;
17 use Carbon\Exceptions\InvalidFormatException;
18 use ReturnTypeWillChange;
19
20 /**
21 * Trait Modifiers.
22 *
23 * Returns dates relative to current date using modifier short-hand.
24 */
25 trait Modifiers
26 {
27 /**
28 * Midday/noon hour.
29 *
30 * @var int
31 */
32 protected static $midDayAt = 12;
33
34 /**
35 * get midday/noon hour
36 *
37 * @return int
38 */
39 public static function getMidDayAt()
40 {
41 return static::$midDayAt;
42 }
43
44 /**
45 * @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
46 * You should rather consider mid-day is always 12pm, then if you need to test if it's an other
47 * hour, test it explicitly:
48 * $date->format('G') == 13
49 * or to set explicitly to a given hour:
50 * $date->setTime(13, 0, 0, 0)
51 *
52 * Set midday/noon hour
53 *
54 * @param int $hour midday hour
55 *
56 * @return void
57 */
58 public static function setMidDayAt($hour)
59 {
60 static::$midDayAt = $hour;
61 }
62
63 /**
64 * Modify to midday, default to self::$midDayAt
65 *
66 * @return static
67 */
68 public function midDay()
69 {
70 return $this->setTime(static::$midDayAt, 0, 0, 0);
71 }
72
73 /**
74 * Modify to the next occurrence of a given modifier such as a day of
75 * the week. If no modifier is provided, modify to the next occurrence
76 * of the current day of the week. Use the supplied constants
77 * to indicate the desired dayOfWeek, ex. static::MONDAY.
78 *
79 * @param string|int|null $modifier
80 *
81 * @return static
82 */
83 public function next($modifier = null)
84 {
85 if ($modifier === null) {
86 $modifier = $this->dayOfWeek;
87 }
88
89 return $this->change(
90 'next '.(\is_string($modifier) ? $modifier : static::$days[$modifier]),
91 );
92 }
93
94 /**
95 * Go forward or backward to the next week- or weekend-day.
96 *
97 * @param bool $weekday
98 * @param bool $forward
99 *
100 * @return static
101 */
102 private function nextOrPreviousDay($weekday = true, $forward = true)
103 {
104 /** @var CarbonInterface $date */
105 $date = $this;
106 $step = $forward ? 1 : -1;
107
108 do {
109 $date = $date->addDays($step);
110 } while ($weekday ? $date->isWeekend() : $date->isWeekday());
111
112 return $date;
113 }
114
115 /**
116 * Go forward to the next weekday.
117 *
118 * @return static
119 */
120 public function nextWeekday()
121 {
122 return $this->nextOrPreviousDay();
123 }
124
125 /**
126 * Go backward to the previous weekday.
127 *
128 * @return static
129 */
130 public function previousWeekday()
131 {
132 return $this->nextOrPreviousDay(true, false);
133 }
134
135 /**
136 * Go forward to the next weekend day.
137 *
138 * @return static
139 */
140 public function nextWeekendDay()
141 {
142 return $this->nextOrPreviousDay(false);
143 }
144
145 /**
146 * Go backward to the previous weekend day.
147 *
148 * @return static
149 */
150 public function previousWeekendDay()
151 {
152 return $this->nextOrPreviousDay(false, false);
153 }
154
155 /**
156 * Modify to the previous occurrence of a given modifier such as a day of
157 * the week. If no dayOfWeek is provided, modify to the previous occurrence
158 * of the current day of the week. Use the supplied constants
159 * to indicate the desired dayOfWeek, ex. static::MONDAY.
160 *
161 * @param string|int|null $modifier
162 *
163 * @return static
164 */
165 public function previous($modifier = null)
166 {
167 if ($modifier === null) {
168 $modifier = $this->dayOfWeek;
169 }
170
171 return $this->change(
172 'last '.(\is_string($modifier) ? $modifier : static::$days[$modifier]),
173 );
174 }
175
176 /**
177 * Modify to the first occurrence of a given day of the week
178 * in the current month. If no dayOfWeek is provided, modify to the
179 * first day of the current month. Use the supplied constants
180 * to indicate the desired dayOfWeek, ex. static::MONDAY.
181 *
182 * @param int|null $dayOfWeek
183 *
184 * @return static
185 */
186 public function firstOfMonth($dayOfWeek = null)
187 {
188 $date = $this->startOfDay();
189
190 if ($dayOfWeek === null) {
191 return $date->day(1);
192 }
193
194 return $date->modify('first '.static::$days[$dayOfWeek].' of '.$date->rawFormat('F').' '.$date->year);
195 }
196
197 /**
198 * Modify to the last occurrence of a given day of the week
199 * in the current month. If no dayOfWeek is provided, modify to the
200 * last day of the current month. Use the supplied constants
201 * to indicate the desired dayOfWeek, ex. static::MONDAY.
202 *
203 * @param int|null $dayOfWeek
204 *
205 * @return static
206 */
207 public function lastOfMonth($dayOfWeek = null)
208 {
209 $date = $this->startOfDay();
210
211 if ($dayOfWeek === null) {
212 return $date->day($date->daysInMonth);
213 }
214
215 return $date->modify('last '.static::$days[$dayOfWeek].' of '.$date->rawFormat('F').' '.$date->year);
216 }
217
218 /**
219 * Modify to the given occurrence of a given day of the week
220 * in the current month. If the calculated occurrence is outside the scope
221 * of the current month, then return false and no modifications are made.
222 * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY.
223 *
224 * @param int $nth
225 * @param int $dayOfWeek
226 *
227 * @return mixed
228 */
229 public function nthOfMonth($nth, $dayOfWeek)
230 {
231 $date = $this->avoidMutation()->firstOfMonth();
232 $check = $date->rawFormat('Y-m');
233 $date = $date->modify('+'.$nth.' '.static::$days[$dayOfWeek]);
234
235 return $date->rawFormat('Y-m') === $check ? $this->modify((string) $date) : false;
236 }
237
238 /**
239 * Modify to the first occurrence of a given day of the week
240 * in the current quarter. If no dayOfWeek is provided, modify to the
241 * first day of the current quarter. Use the supplied constants
242 * to indicate the desired dayOfWeek, ex. static::MONDAY.
243 *
244 * @param int|null $dayOfWeek day of the week default null
245 *
246 * @return static
247 */
248 public function firstOfQuarter($dayOfWeek = null)
249 {
250 return $this->setDate($this->year, $this->quarter * static::MONTHS_PER_QUARTER - 2, 1)->firstOfMonth($dayOfWeek);
251 }
252
253 /**
254 * Modify to the last occurrence of a given day of the week
255 * in the current quarter. If no dayOfWeek is provided, modify to the
256 * last day of the current quarter. Use the supplied constants
257 * to indicate the desired dayOfWeek, ex. static::MONDAY.
258 *
259 * @param int|null $dayOfWeek day of the week default null
260 *
261 * @return static
262 */
263 public function lastOfQuarter($dayOfWeek = null)
264 {
265 return $this->setDate($this->year, $this->quarter * static::MONTHS_PER_QUARTER, 1)->lastOfMonth($dayOfWeek);
266 }
267
268 /**
269 * Modify to the given occurrence of a given day of the week
270 * in the current quarter. If the calculated occurrence is outside the scope
271 * of the current quarter, then return false and no modifications are made.
272 * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY.
273 *
274 * @param int $nth
275 * @param int $dayOfWeek
276 *
277 * @return mixed
278 */
279 public function nthOfQuarter($nth, $dayOfWeek)
280 {
281 $date = $this->avoidMutation()->day(1)->month($this->quarter * static::MONTHS_PER_QUARTER);
282 $lastMonth = $date->month;
283 $year = $date->year;
284 $date = $date->firstOfQuarter()->modify('+'.$nth.' '.static::$days[$dayOfWeek]);
285
286 return ($lastMonth < $date->month || $year !== $date->year) ? false : $this->modify((string) $date);
287 }
288
289 /**
290 * Modify to the first occurrence of a given day of the week
291 * in the current year. If no dayOfWeek is provided, modify to the
292 * first day of the current year. Use the supplied constants
293 * to indicate the desired dayOfWeek, ex. static::MONDAY.
294 *
295 * @param int|null $dayOfWeek day of the week default null
296 *
297 * @return static
298 */
299 public function firstOfYear($dayOfWeek = null)
300 {
301 return $this->month(1)->firstOfMonth($dayOfWeek);
302 }
303
304 /**
305 * Modify to the last occurrence of a given day of the week
306 * in the current year. If no dayOfWeek is provided, modify to the
307 * last day of the current year. Use the supplied constants
308 * to indicate the desired dayOfWeek, ex. static::MONDAY.
309 *
310 * @param int|null $dayOfWeek day of the week default null
311 *
312 * @return static
313 */
314 public function lastOfYear($dayOfWeek = null)
315 {
316 return $this->month(static::MONTHS_PER_YEAR)->lastOfMonth($dayOfWeek);
317 }
318
319 /**
320 * Modify to the given occurrence of a given day of the week
321 * in the current year. If the calculated occurrence is outside the scope
322 * of the current year, then return false and no modifications are made.
323 * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY.
324 *
325 * @param int $nth
326 * @param int $dayOfWeek
327 *
328 * @return mixed
329 */
330 public function nthOfYear($nth, $dayOfWeek)
331 {
332 $date = $this->avoidMutation()->firstOfYear()->modify('+'.$nth.' '.static::$days[$dayOfWeek]);
333
334 return $this->year === $date->year ? $this->modify((string) $date) : false;
335 }
336
337 /**
338 * Modify the current instance to the average of a given instance (default now) and the current instance
339 * (second-precision).
340 *
341 * @param \Carbon\Carbon|\DateTimeInterface|null $date
342 *
343 * @return static
344 */
345 public function average($date = null)
346 {
347 return $this->addRealMicroseconds((int) ($this->diffInMicroseconds($this->resolveCarbon($date), false) / 2));
348 }
349
350 /**
351 * Get the closest date from the instance (second-precision).
352 *
353 * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
354 * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
355 *
356 * @return static
357 */
358 public function closest($date1, $date2)
359 {
360 return $this->diffInMicroseconds($date1, true) < $this->diffInMicroseconds($date2, true) ? $date1 : $date2;
361 }
362
363 /**
364 * Get the farthest date from the instance (second-precision).
365 *
366 * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1
367 * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2
368 *
369 * @return static
370 */
371 public function farthest($date1, $date2)
372 {
373 return $this->diffInMicroseconds($date1, true) > $this->diffInMicroseconds($date2, true) ? $date1 : $date2;
374 }
375
376 /**
377 * Get the minimum instance between a given instance (default now) and the current instance.
378 *
379 * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
380 *
381 * @return static
382 */
383 public function min($date = null)
384 {
385 $date = $this->resolveCarbon($date);
386
387 return $this->lt($date) ? $this : $date;
388 }
389
390 /**
391 * Get the minimum instance between a given instance (default now) and the current instance.
392 *
393 * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
394 *
395 * @see min()
396 *
397 * @return static
398 */
399 public function minimum($date = null)
400 {
401 return $this->min($date);
402 }
403
404 /**
405 * Get the maximum instance between a given instance (default now) and the current instance.
406 *
407 * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
408 *
409 * @return static
410 */
411 public function max($date = null)
412 {
413 $date = $this->resolveCarbon($date);
414
415 return $this->gt($date) ? $this : $date;
416 }
417
418 /**
419 * Get the maximum instance between a given instance (default now) and the current instance.
420 *
421 * @param \Carbon\Carbon|\DateTimeInterface|mixed $date
422 *
423 * @see max()
424 *
425 * @return static
426 */
427 public function maximum($date = null)
428 {
429 return $this->max($date);
430 }
431
432 /**
433 * Calls \DateTime::modify if mutable or \DateTimeImmutable::modify else.
434 *
435 * @see https://php.net/manual/en/datetime.modify.php
436 *
437 * @return static
438 */
439 #[ReturnTypeWillChange]
440 public function modify($modify)
441 {
442 return parent::modify((string) $modify)
443 ?: throw new InvalidFormatException('Could not modify with: '.var_export($modify, true));
444 }
445
446 /**
447 * Similar to native modify() method of DateTime but can handle more grammars.
448 *
449 * @example
450 * ```
451 * echo Carbon::now()->change('next 2pm');
452 * ```
453 *
454 * @link https://php.net/manual/en/datetime.modify.php
455 *
456 * @param string $modifier
457 *
458 * @return static
459 */
460 public function change($modifier)
461 {
462 return $this->modify(preg_replace_callback('/^(next|previous|last)\s+(\d{1,2}(h|am|pm|:\d{1,2}(:\d{1,2})?))$/i', function ($match) {
463 $match[2] = str_replace('h', ':00', $match[2]);
464 $test = $this->avoidMutation()->modify($match[2]);
465 $method = $match[1] === 'next' ? 'lt' : 'gt';
466 $match[1] = $test->$method($this) ? $match[1].' day' : 'today';
467
468 return $match[1].' '.$match[2];
469 }, strtr(trim($modifier), [
470 ' at ' => ' ',
471 'just now' => 'now',
472 'after tomorrow' => 'tomorrow +1 day',
473 'before yesterday' => 'yesterday -1 day',
474 ])));
475 }
476 }