Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Quarter
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
7 / 7
15
100.00% covered (success)
100.00%
1 / 1
 description
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 months
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 startMonth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 endMonth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 includes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ordered
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromMonth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(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
19namespace FastForward\Enum\Calendar;
20
21use FastForward\Enum\DescribedEnumInterface;
22use FastForward\Enum\LabeledEnumInterface;
23use FastForward\Enum\Trait\Comparable;
24use FastForward\Enum\Trait\HasLabel;
25use FastForward\Enum\Trait\HasNameLookup;
26use FastForward\Enum\Trait\HasNameMap;
27use FastForward\Enum\Trait\HasNames;
28use FastForward\Enum\Trait\HasOptions;
29use FastForward\Enum\Trait\HasValueMap;
30use FastForward\Enum\Trait\HasValues;
31
32enum Quarter: 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 Q1 = 1;
44    case Q2 = 2;
45    case Q3 = 3;
46    case Q4 = 4;
47
48    /**
49     * @return string
50     */
51    public function description(): string
52    {
53        return match ($this) {
54            self::Q1 => 'First quarter of the year, covering January through March.',
55            self::Q2 => 'Second quarter of the year, covering April through June.',
56            self::Q3 => 'Third quarter of the year, covering July through September.',
57            self::Q4 => 'Fourth quarter of the year, covering October through December.',
58        };
59    }
60
61    /**
62     * @return list<Month>
63     */
64    public function months(): array
65    {
66        return match ($this) {
67            self::Q1 => [Month::January, Month::February, Month::March],
68            self::Q2 => [Month::April, Month::May, Month::June],
69            self::Q3 => [Month::July, Month::August, Month::September],
70            self::Q4 => [Month::October, Month::November, Month::December],
71        };
72    }
73
74    /**
75     * @return Month
76     */
77    public function startMonth(): Month
78    {
79        return $this->months()[0];
80    }
81
82    /**
83     * @return Month
84     */
85    public function endMonth(): Month
86    {
87        return $this->months()[2];
88    }
89
90    /**
91     * @param Month $month
92     *
93     * @return bool
94     */
95    public function includes(Month $month): bool
96    {
97        return $month->quarter() === $this->value;
98    }
99
100    /**
101     * @return list<self>
102     */
103    public static function ordered(): array
104    {
105        return [self::Q1, self::Q2, self::Q3, self::Q4];
106    }
107
108    /**
109     * @param Month $month
110     *
111     * @return self
112     */
113    public static function fromMonth(Month $month): self
114    {
115        return self::from($month->quarter());
116    }
117}