Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Traits/Week.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\CarbonInterval;
17
18 /**
19 * Trait Week.
20 *
21 * week and ISO week number, year and count in year.
22 *
23 * Depends on the following properties:
24 *
25 * @property int $daysInYear
26 * @property int $dayOfWeek
27 * @property int $dayOfYear
28 * @property int $year
29 *
30 * Depends on the following methods:
31 *
32 * @method static addWeeks(int $weeks = 1)
33 * @method static copy()
34 * @method static dayOfYear(int $dayOfYear)
35 * @method string getTranslationMessage(string $key, ?string $locale = null, ?string $default = null, $translator = null)
36 * @method static next(int|string $modifier = null)
37 * @method static startOfWeek(int $day = null)
38 * @method static subWeeks(int $weeks = 1)
39 * @method static year(int $year = null)
40 */
41 trait Week
42 {
43 /**
44 * Set/get the week number of year using given first day of week and first
45 * day of year included in the first week. Or use ISO format if no settings
46 * given.
47 *
48 * @param int|null $year if null, act as a getter, if not null, set the year and return current instance.
49 * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday)
50 * @param int|null $dayOfYear first day of year included in the week #1
51 *
52 * @return int|static
53 */
54 public function isoWeekYear($year = null, $dayOfWeek = null, $dayOfYear = null)
55 {
56 return $this->weekYear(
57 $year,
58 $dayOfWeek ?? static::MONDAY,
59 $dayOfYear ?? static::THURSDAY,
60 );
61 }
62
63 /**
64 * Set/get the week number of year using given first day of week and first
65 * day of year included in the first week. Or use US format if no settings
66 * given (Sunday / Jan 6).
67 *
68 * @param int|null $year if null, act as a getter, if not null, set the year and return current instance.
69 * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday)
70 * @param int|null $dayOfYear first day of year included in the week #1
71 *
72 * @return int|static
73 */
74 public function weekYear($year = null, $dayOfWeek = null, $dayOfYear = null)
75 {
76 $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? static::SUNDAY;
77 $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1;
78
79 if ($year !== null) {
80 $year = (int) round($year);
81
82 if ($this->weekYear(null, $dayOfWeek, $dayOfYear) === $year) {
83 return $this->avoidMutation();
84 }
85
86 $week = $this->week(null, $dayOfWeek, $dayOfYear);
87 $day = $this->dayOfWeek;
88 $date = $this->year($year);
89
90 $date = match ($date->weekYear(null, $dayOfWeek, $dayOfYear) - $year) {
91 CarbonInterval::POSITIVE => $date->subWeeks(static::WEEKS_PER_YEAR / 2),
92 CarbonInterval::NEGATIVE => $date->addWeeks(static::WEEKS_PER_YEAR / 2),
93 default => $date,
94 };
95
96 $date = $date
97 ->addWeeks($week - $date->week(null, $dayOfWeek, $dayOfYear))
98 ->startOfWeek($dayOfWeek);
99
100 if ($date->dayOfWeek === $day) {
101 return $date;
102 }
103
104 return $date->next($day);
105 }
106
107 $year = $this->year;
108 $day = $this->dayOfYear;
109 $date = $this->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
110
111 if ($date->year === $year && $day < $date->dayOfYear) {
112 return $year - 1;
113 }
114
115 $date = $this->avoidMutation()->addYear()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
116
117 if ($date->year === $year && $day >= $date->dayOfYear) {
118 return $year + 1;
119 }
120
121 return $year;
122 }
123
124 /**
125 * Get the number of weeks of the current week-year using given first day of week and first
126 * day of year included in the first week. Or use ISO format if no settings
127 * given.
128 *
129 * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday)
130 * @param int|null $dayOfYear first day of year included in the week #1
131 *
132 * @return int
133 */
134 public function isoWeeksInYear($dayOfWeek = null, $dayOfYear = null)
135 {
136 return $this->weeksInYear(
137 $dayOfWeek ?? static::MONDAY,
138 $dayOfYear ?? static::THURSDAY,
139 );
140 }
141
142 /**
143 * Get the number of weeks of the current week-year using given first day of week and first
144 * day of year included in the first week. Or use US format if no settings
145 * given (Sunday / Jan 6).
146 *
147 * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday)
148 * @param int|null $dayOfYear first day of year included in the week #1
149 *
150 * @return int
151 */
152 public function weeksInYear($dayOfWeek = null, $dayOfYear = null)
153 {
154 $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? static::SUNDAY;
155 $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1;
156 $year = $this->year;
157 $start = $this->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
158 $startDay = $start->dayOfYear;
159 if ($start->year !== $year) {
160 $startDay -= $start->daysInYear;
161 }
162 $end = $this->avoidMutation()->addYear()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
163 $endDay = $end->dayOfYear;
164 if ($end->year !== $year) {
165 $endDay += $this->daysInYear;
166 }
167
168 return (int) round(($endDay - $startDay) / static::DAYS_PER_WEEK);
169 }
170
171 /**
172 * Get/set the week number using given first day of week and first
173 * day of year included in the first week. Or use US format if no settings
174 * given (Sunday / Jan 6).
175 *
176 * @param int|null $week
177 * @param int|null $dayOfWeek
178 * @param int|null $dayOfYear
179 *
180 * @return int|static
181 */
182 public function week($week = null, $dayOfWeek = null, $dayOfYear = null)
183 {
184 $date = $this;
185 $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0;
186 $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1;
187
188 if ($week !== null) {
189 return $date->addWeeks(round($week) - $this->week(null, $dayOfWeek, $dayOfYear));
190 }
191
192 $start = $date->avoidMutation()->shiftTimezone('UTC')->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
193 $end = $date->avoidMutation()->shiftTimezone('UTC')->startOfWeek($dayOfWeek);
194
195 if ($start > $end) {
196 $start = $start->subWeeks(static::WEEKS_PER_YEAR / 2)->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
197 }
198
199 $week = (int) ($start->diffInDays($end) / static::DAYS_PER_WEEK + 1);
200
201 return $week > $end->weeksInYear($dayOfWeek, $dayOfYear) ? 1 : $week;
202 }
203
204 /**
205 * Get/set the week number using given first day of week and first
206 * day of year included in the first week. Or use ISO format if no settings
207 * given.
208 *
209 * @param int|null $week
210 * @param int|null $dayOfWeek
211 * @param int|null $dayOfYear
212 *
213 * @return int|static
214 */
215 public function isoWeek($week = null, $dayOfWeek = null, $dayOfYear = null)
216 {
217 return $this->week(
218 $week,
219 $dayOfWeek ?? static::MONDAY,
220 $dayOfYear ?? static::THURSDAY,
221 );
222 }
223 }