Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
98 / 98
100.00% covered (success)
100.00%
25 / 25
CRAP
100.00% covered (success)
100.00%
1 / 1
DayOfWeek
100.00% covered (success)
100.00%
98 / 98
100.00% covered (success)
100.00%
25 / 25
30
100.00% covered (success)
100.00%
1 / 1
 label
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 fullNamePtBr
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 shortName
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 shortNamePtBr
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 minName
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 minNamePtBr
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 isWeekend
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isWeekday
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSunday
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isMonday
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isTuesday
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isWednesday
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isThursday
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFriday
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSaturday
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 next
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 previous
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 isoValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 fromIso
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 labelList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 toArrayWithLabels
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 toArrayPtBr
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 weekdays
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 weekend
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace DevToolbelt\Enums\Calendar;
6
7use DevToolbelt\Enums\EnumInterface;
8
9enum DayOfWeek: int implements EnumInterface
10{
11    case SUNDAY = 0;
12    case MONDAY = 1;
13    case TUESDAY = 2;
14    case WEDNESDAY = 3;
15    case THURSDAY = 4;
16    case FRIDAY = 5;
17    case SATURDAY = 6;
18
19    public function label(): string
20    {
21        return match ($this) {
22            self::SUNDAY => 'Sunday',
23            self::MONDAY => 'Monday',
24            self::TUESDAY => 'Tuesday',
25            self::WEDNESDAY => 'Wednesday',
26            self::THURSDAY => 'Thursday',
27            self::FRIDAY => 'Friday',
28            self::SATURDAY => 'Saturday',
29        };
30    }
31
32    public function fullNamePtBr(): string
33    {
34        return match ($this) {
35            self::SUNDAY => 'Domingo',
36            self::MONDAY => 'Segunda-feira',
37            self::TUESDAY => 'Terça-feira',
38            self::WEDNESDAY => 'Quarta-feira',
39            self::THURSDAY => 'Quinta-feira',
40            self::FRIDAY => 'Sexta-feira',
41            self::SATURDAY => 'Sábado',
42        };
43    }
44
45    public function shortName(): string
46    {
47        return match ($this) {
48            self::SUNDAY => 'Sun',
49            self::MONDAY => 'Mon',
50            self::TUESDAY => 'Tue',
51            self::WEDNESDAY => 'Wed',
52            self::THURSDAY => 'Thu',
53            self::FRIDAY => 'Fri',
54            self::SATURDAY => 'Sat',
55        };
56    }
57
58    public function shortNamePtBr(): string
59    {
60        return match ($this) {
61            self::SUNDAY => 'Dom',
62            self::MONDAY => 'Seg',
63            self::TUESDAY => 'Ter',
64            self::WEDNESDAY => 'Qua',
65            self::THURSDAY => 'Qui',
66            self::FRIDAY => 'Sex',
67            self::SATURDAY => 'Sáb',
68        };
69    }
70
71    public function minName(): string
72    {
73        return match ($this) {
74            self::SUNDAY => 'Su',
75            self::MONDAY => 'Mo',
76            self::TUESDAY => 'Tu',
77            self::WEDNESDAY => 'We',
78            self::THURSDAY => 'Th',
79            self::FRIDAY => 'Fr',
80            self::SATURDAY => 'Sa',
81        };
82    }
83
84    public function minNamePtBr(): string
85    {
86        return match ($this) {
87            self::SUNDAY => 'D',
88            self::MONDAY => 'S',
89            self::TUESDAY => 'T',
90            self::WEDNESDAY => 'Q',
91            self::THURSDAY => 'Q',
92            self::FRIDAY => 'S',
93            self::SATURDAY => 'S',
94        };
95    }
96
97    public function isWeekend(): bool
98    {
99        return $this === self::SATURDAY || $this === self::SUNDAY;
100    }
101
102    public function isWeekday(): bool
103    {
104        return !$this->isWeekend();
105    }
106
107    public function isSunday(): bool
108    {
109        return $this === self::SUNDAY;
110    }
111
112    public function isMonday(): bool
113    {
114        return $this === self::MONDAY;
115    }
116
117    public function isTuesday(): bool
118    {
119        return $this === self::TUESDAY;
120    }
121
122    public function isWednesday(): bool
123    {
124        return $this === self::WEDNESDAY;
125    }
126
127    public function isThursday(): bool
128    {
129        return $this === self::THURSDAY;
130    }
131
132    public function isFriday(): bool
133    {
134        return $this === self::FRIDAY;
135    }
136
137    public function isSaturday(): bool
138    {
139        return $this === self::SATURDAY;
140    }
141
142    public function next(): self
143    {
144        return match ($this) {
145            self::SATURDAY => self::SUNDAY,
146            default => self::from($this->value + 1),
147        };
148    }
149
150    public function previous(): self
151    {
152        return match ($this) {
153            self::SUNDAY => self::SATURDAY,
154            default => self::from($this->value - 1),
155        };
156    }
157
158    /**
159     * Returns the ISO-8601 numeric representation (1 = Monday, 7 = Sunday)
160     */
161    public function isoValue(): int
162    {
163        return $this === self::SUNDAY ? 7 : $this->value;
164    }
165
166    /**
167     * Create from ISO-8601 numeric representation (1 = Monday, 7 = Sunday)
168     */
169    public static function fromIso(int $isoDay): self
170    {
171        return match ($isoDay) {
172            7 => self::SUNDAY,
173            default => self::from($isoDay),
174        };
175    }
176
177    /**
178     * @return string[]
179     */
180    public function labelList(): array
181    {
182        return array_map(fn (self $case) => $case->label(), self::cases());
183    }
184
185    /**
186     * @return array<int, int>
187     */
188    public static function toArray(): array
189    {
190        $result = [];
191
192        foreach (self::cases() as $day) {
193            $result[$day->value] = $day->value;
194        }
195
196        return $result;
197    }
198
199    /**
200     * @return array<int, string>
201     */
202    public static function toArrayWithLabels(): array
203    {
204        $result = [];
205
206        foreach (self::cases() as $day) {
207            $result[$day->value] = $day->label();
208        }
209
210        return $result;
211    }
212
213    /**
214     * @return array<int, string>
215     */
216    public static function toArrayPtBr(): array
217    {
218        $result = [];
219
220        foreach (self::cases() as $day) {
221            $result[$day->value] = $day->fullNamePtBr();
222        }
223
224        return $result;
225    }
226
227    /**
228     * @return self[]
229     */
230    public static function weekdays(): array
231    {
232        return [
233            self::MONDAY,
234            self::TUESDAY,
235            self::WEDNESDAY,
236            self::THURSDAY,
237            self::FRIDAY,
238        ];
239    }
240
241    /**
242     * @return self[]
243     */
244    public static function weekend(): array
245    {
246        return [
247            self::SATURDAY,
248            self::SUNDAY,
249        ];
250    }
251}