Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| IntervalUnit | |
100.00% |
28 / 28 |
|
100.00% |
4 / 4 |
25 | |
100.00% |
1 / 1 |
| description | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
8 | |||
| shortLabel | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
8 | |||
| seconds | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
8 | |||
| isCalendarAware | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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\DateTime; |
| 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 IntervalUnit: string 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 Second = 'second'; |
| 44 | case Minute = 'minute'; |
| 45 | case Hour = 'hour'; |
| 46 | case Day = 'day'; |
| 47 | case Week = 'week'; |
| 48 | case Month = 'month'; |
| 49 | case Year = 'year'; |
| 50 | |
| 51 | /** |
| 52 | * @return string |
| 53 | */ |
| 54 | public function description(): string |
| 55 | { |
| 56 | return match ($this) { |
| 57 | self::Second => 'Represents one-second intervals for fine-grained timing and retry policies.', |
| 58 | self::Minute => 'Represents one-minute intervals for short-lived scheduling and cache policies.', |
| 59 | self::Hour => 'Represents one-hour intervals for operational windows and batch execution.', |
| 60 | self::Day => 'Represents one-day intervals for daily schedules and retention policies.', |
| 61 | self::Week => 'Represents one-week intervals for weekly planning, reports, and cleanup jobs.', |
| 62 | self::Month => 'Represents one-month intervals for monthly billing, reporting, and rotation schedules.', |
| 63 | self::Year => 'Represents one-year intervals for annual cycles, compliance, and archival horizons.', |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return string |
| 69 | */ |
| 70 | public function shortLabel(): string |
| 71 | { |
| 72 | return match ($this) { |
| 73 | self::Second => 's', |
| 74 | self::Minute => 'min', |
| 75 | self::Hour => 'h', |
| 76 | self::Day => 'd', |
| 77 | self::Week => 'w', |
| 78 | self::Month => 'mo', |
| 79 | self::Year => 'y', |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param int $amount |
| 85 | * |
| 86 | * @return int |
| 87 | */ |
| 88 | public function seconds(int $amount = 1): int |
| 89 | { |
| 90 | return $amount * match ($this) { |
| 91 | self::Second => 1, |
| 92 | self::Minute => 60, |
| 93 | self::Hour => 3600, |
| 94 | self::Day => 86400, |
| 95 | self::Week => 604800, |
| 96 | self::Month => 2628000, |
| 97 | self::Year => 31536000, |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function isCalendarAware(): bool |
| 105 | { |
| 106 | return $this->in([self::Month, self::Year]); |
| 107 | } |
| 108 | } |