Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Severity
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
16
100.00% covered (success)
100.00%
1 / 1
 description
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
7
 weight
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
7
 isAtLeast
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
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\Common;
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 Severity: 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 Debug = 'debug';
44    case Info = 'info';
45    case Notice = 'notice';
46    case Warning = 'warning';
47    case Error = 'error';
48    case Critical = 'critical';
49
50    /**
51     * @return string
52     */
53    public function description(): string
54    {
55        return match ($this) {
56            self::Debug => 'Diagnostic information intended for development and troubleshooting.',
57            self::Info => 'Informational event describing normal application flow or notable activity.',
58            self::Notice => 'Significant but expected condition that may deserve attention soon.',
59            self::Warning => 'Potential problem that should be reviewed before it escalates.',
60            self::Error => 'Failure condition that affected part of the current operation.',
61            self::Critical => 'Severe failure requiring urgent human or automated intervention.',
62        };
63    }
64
65    /**
66     * @return int
67     */
68    public function weight(): int
69    {
70        return match ($this) {
71            self::Debug => 10,
72            self::Info => 20,
73            self::Notice => 30,
74            self::Warning => 40,
75            self::Error => 50,
76            self::Critical => 60,
77        };
78    }
79
80    /**
81     * @param self $severity
82     *
83     * @return bool
84     */
85    public function isAtLeast(self $severity): bool
86    {
87        return $this->weight() >= $severity->weight();
88    }
89
90    /**
91     * @return list<self>
92     */
93    public static function ordered(): array
94    {
95        return [self::Debug, self::Info, self::Notice, self::Warning, self::Error, self::Critical];
96    }
97}