Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
97 / 97
100.00% covered (success)
100.00%
19 / 19
CRAP
100.00% covered (success)
100.00%
1 / 1
Month
100.00% covered (success)
100.00%
97 / 97
100.00% covered (success)
100.00%
19 / 19
25
100.00% covered (success)
100.00%
1 / 1
 label
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 fullNamePtBr
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 shortName
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 shortNamePtBr
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 daysCount
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 quarter
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 isFirstQuarter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSecondQuarter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isThirdQuarter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFourthQuarter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFirstHalf
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSecondHalf
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
 isLeapYear
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 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
1<?php
2
3declare(strict_types=1);
4
5namespace DevToolbelt\Enums\Calendar;
6
7use DevToolbelt\Enums\EnumInterface;
8
9enum Month: int implements EnumInterface
10{
11    case JANUARY = 1;
12    case FEBRUARY = 2;
13    case MARCH = 3;
14    case APRIL = 4;
15    case MAY = 5;
16    case JUNE = 6;
17    case JULY = 7;
18    case AUGUST = 8;
19    case SEPTEMBER = 9;
20    case OCTOBER = 10;
21    case NOVEMBER = 11;
22    case DECEMBER = 12;
23
24    public function label(): string
25    {
26        return match ($this) {
27            self::JANUARY => 'January',
28            self::FEBRUARY => 'February',
29            self::MARCH => 'March',
30            self::APRIL => 'April',
31            self::MAY => 'May',
32            self::JUNE => 'June',
33            self::JULY => 'July',
34            self::AUGUST => 'August',
35            self::SEPTEMBER => 'September',
36            self::OCTOBER => 'October',
37            self::NOVEMBER => 'November',
38            self::DECEMBER => 'December',
39        };
40    }
41
42    public function fullNamePtBr(): string
43    {
44        return match ($this) {
45            self::JANUARY => 'Janeiro',
46            self::FEBRUARY => 'Fevereiro',
47            self::MARCH => 'Março',
48            self::APRIL => 'Abril',
49            self::MAY => 'Maio',
50            self::JUNE => 'Junho',
51            self::JULY => 'Julho',
52            self::AUGUST => 'Agosto',
53            self::SEPTEMBER => 'Setembro',
54            self::OCTOBER => 'Outubro',
55            self::NOVEMBER => 'Novembro',
56            self::DECEMBER => 'Dezembro',
57        };
58    }
59
60    public function shortName(): string
61    {
62        return match ($this) {
63            self::JANUARY => 'Jan',
64            self::FEBRUARY => 'Feb',
65            self::MARCH => 'Mar',
66            self::APRIL => 'Apr',
67            self::MAY => 'May',
68            self::JUNE => 'Jun',
69            self::JULY => 'Jul',
70            self::AUGUST => 'Aug',
71            self::SEPTEMBER => 'Sep',
72            self::OCTOBER => 'Oct',
73            self::NOVEMBER => 'Nov',
74            self::DECEMBER => 'Dec',
75        };
76    }
77
78    public function shortNamePtBr(): string
79    {
80        return match ($this) {
81            self::JANUARY => 'Jan',
82            self::FEBRUARY => 'Fev',
83            self::MARCH => 'Mar',
84            self::APRIL => 'Abr',
85            self::MAY => 'Mai',
86            self::JUNE => 'Jun',
87            self::JULY => 'Jul',
88            self::AUGUST => 'Ago',
89            self::SEPTEMBER => 'Set',
90            self::OCTOBER => 'Out',
91            self::NOVEMBER => 'Nov',
92            self::DECEMBER => 'Dez',
93        };
94    }
95
96    public function daysCount(?int $year = null): int
97    {
98        $year = $year ?? (int) date('Y');
99
100        return match ($this) {
101            self::JANUARY, self::MARCH, self::MAY, self::JULY,
102            self::AUGUST, self::OCTOBER, self::DECEMBER => 31,
103            self::APRIL, self::JUNE, self::SEPTEMBER, self::NOVEMBER => 30,
104            self::FEBRUARY => $this->isLeapYear($year) ? 29 : 28,
105        };
106    }
107
108    public function quarter(): int
109    {
110        return match ($this) {
111            self::JANUARY, self::FEBRUARY, self::MARCH => 1,
112            self::APRIL, self::MAY, self::JUNE => 2,
113            self::JULY, self::AUGUST, self::SEPTEMBER => 3,
114            self::OCTOBER, self::NOVEMBER, self::DECEMBER => 4,
115        };
116    }
117
118    public function isFirstQuarter(): bool
119    {
120        return $this->quarter() === 1;
121    }
122
123    public function isSecondQuarter(): bool
124    {
125        return $this->quarter() === 2;
126    }
127
128    public function isThirdQuarter(): bool
129    {
130        return $this->quarter() === 3;
131    }
132
133    public function isFourthQuarter(): bool
134    {
135        return $this->quarter() === 4;
136    }
137
138    public function isFirstHalf(): bool
139    {
140        return $this->value <= 6;
141    }
142
143    public function isSecondHalf(): bool
144    {
145        return $this->value > 6;
146    }
147
148    public function next(): self
149    {
150        return match ($this) {
151            self::DECEMBER => self::JANUARY,
152            default => self::from($this->value + 1),
153        };
154    }
155
156    public function previous(): self
157    {
158        return match ($this) {
159            self::JANUARY => self::DECEMBER,
160            default => self::from($this->value - 1),
161        };
162    }
163
164    private function isLeapYear(int $year): bool
165    {
166        return ($year % 4 === 0 && $year % 100 !== 0) || ($year % 400 === 0);
167    }
168
169    /**
170     * @return string[]
171     */
172    public function labelList(): array
173    {
174        return array_map(fn (self $case) => $case->label(), self::cases());
175    }
176
177    /**
178     * @return array<int, int>
179     */
180    public static function toArray(): array
181    {
182        $result = [];
183
184        foreach (self::cases() as $month) {
185            $result[$month->value] = $month->value;
186        }
187
188        return $result;
189    }
190
191    /**
192     * @return array<int, string>
193     */
194    public static function toArrayWithLabels(): array
195    {
196        $result = [];
197
198        foreach (self::cases() as $month) {
199            $result[$month->value] = $month->label();
200        }
201
202        return $result;
203    }
204
205    /**
206     * @return array<int, string>
207     */
208    public static function toArrayPtBr(): array
209    {
210        $result = [];
211
212        foreach (self::cases() as $month) {
213            $result[$month->value] = $month->fullNamePtBr();
214        }
215
216        return $result;
217    }
218}