Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
24 / 24 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| Semester | |
100.00% |
24 / 24 |
|
100.00% |
8 / 8 |
16 | |
100.00% |
1 / 1 |
| description | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| months | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| quarters | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| startMonth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| endMonth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| includes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromMonth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| fromQuarter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Ergonomic utilities for PHP enums, including names, values, lookups, and option maps. |
| 7 | * |
| 8 | * This file is part of fast-forward/enum project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/enum |
| 14 | * @see https://github.com/php-fast-forward/enum/issues |
| 15 | * @see https://php-fast-forward.github.io/enum/ |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Enum\Calendar; |
| 20 | |
| 21 | use FastForward\Enum\DescribedEnumInterface; |
| 22 | use FastForward\Enum\LabeledEnumInterface; |
| 23 | use FastForward\Enum\Trait\Comparable; |
| 24 | use FastForward\Enum\Trait\HasLabel; |
| 25 | use FastForward\Enum\Trait\HasNameLookup; |
| 26 | use FastForward\Enum\Trait\HasNameMap; |
| 27 | use FastForward\Enum\Trait\HasNames; |
| 28 | use FastForward\Enum\Trait\HasOptions; |
| 29 | use FastForward\Enum\Trait\HasValueMap; |
| 30 | use FastForward\Enum\Trait\HasValues; |
| 31 | |
| 32 | enum Semester: int implements DescribedEnumInterface, LabeledEnumInterface |
| 33 | { |
| 34 | use Comparable; |
| 35 | use HasLabel; |
| 36 | use HasNameLookup; |
| 37 | use HasNameMap; |
| 38 | use HasNames; |
| 39 | use HasOptions; |
| 40 | use HasValueMap; |
| 41 | use HasValues; |
| 42 | |
| 43 | case H1 = 1; |
| 44 | case H2 = 2; |
| 45 | |
| 46 | /** |
| 47 | * @return string |
| 48 | */ |
| 49 | public function description(): string |
| 50 | { |
| 51 | return match ($this) { |
| 52 | self::H1 => 'First half of the year, covering January through June.', |
| 53 | self::H2 => 'Second half of the year, covering July through December.', |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return list<Month> |
| 59 | */ |
| 60 | public function months(): array |
| 61 | { |
| 62 | return match ($this) { |
| 63 | self::H1 => [Month::January, Month::February, Month::March, Month::April, Month::May, Month::June], |
| 64 | self::H2 => [ |
| 65 | Month::July, |
| 66 | Month::August, |
| 67 | Month::September, |
| 68 | Month::October, |
| 69 | Month::November, |
| 70 | Month::December, |
| 71 | ], |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return list<Quarter> |
| 77 | */ |
| 78 | public function quarters(): array |
| 79 | { |
| 80 | return match ($this) { |
| 81 | self::H1 => [Quarter::Q1, Quarter::Q2], |
| 82 | self::H2 => [Quarter::Q3, Quarter::Q4], |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return Month |
| 88 | */ |
| 89 | public function startMonth(): Month |
| 90 | { |
| 91 | return $this->months()[0]; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @return Month |
| 96 | */ |
| 97 | public function endMonth(): Month |
| 98 | { |
| 99 | return $this->months()[5]; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param Month $month |
| 104 | * |
| 105 | * @return bool |
| 106 | */ |
| 107 | public function includes(Month $month): bool |
| 108 | { |
| 109 | return $this->is(self::fromMonth($month)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param Month $month |
| 114 | * |
| 115 | * @return self |
| 116 | */ |
| 117 | public static function fromMonth(Month $month): self |
| 118 | { |
| 119 | return $month->value <= Month::June->value ? self::H1 : self::H2; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param Quarter $quarter |
| 124 | * |
| 125 | * @return self |
| 126 | */ |
| 127 | public static function fromQuarter(Quarter $quarter): self |
| 128 | { |
| 129 | return $quarter->value <= Quarter::Q2->value ? self::H1 : self::H2; |
| 130 | } |
| 131 | } |