Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Level | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
13 | |
100.00% |
1 / 1 |
| toSeverity | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
9 | |||
| toName | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| fromSeverity | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <dev@noladev.com> |
| 8 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Log; |
| 16 | |
| 17 | use Psr\Log\LogLevel; |
| 18 | use Psr\Log\InvalidArgumentException; |
| 19 | |
| 20 | /** |
| 21 | * Log level class |
| 22 | * |
| 23 | * @category Pop |
| 24 | * @package Pop\Log |
| 25 | * @author Nick Sagona, III <dev@noladev.com> |
| 26 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 27 | * @license https://www.popphp.org/license New BSD License |
| 28 | * @version 5.0.0 |
| 29 | */ |
| 30 | final class Level |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * Severity map: PSR-3 level string => RFC severity int |
| 35 | * @var array |
| 36 | */ |
| 37 | private const SEVERITY = [ |
| 38 | LogLevel::EMERGENCY => 0, |
| 39 | LogLevel::ALERT => 1, |
| 40 | LogLevel::CRITICAL => 2, |
| 41 | LogLevel::ERROR => 3, |
| 42 | LogLevel::WARNING => 4, |
| 43 | LogLevel::NOTICE => 5, |
| 44 | LogLevel::INFO => 6, |
| 45 | LogLevel::DEBUG => 7, |
| 46 | ]; |
| 47 | |
| 48 | /** |
| 49 | * Convert a PSR-3 level string or a legacy severity int into a severity int (0-7) |
| 50 | * |
| 51 | * @param mixed $level |
| 52 | * @throws InvalidArgumentException |
| 53 | * @return int |
| 54 | */ |
| 55 | public static function toSeverity(mixed $level): int |
| 56 | { |
| 57 | if (is_int($level) && in_array($level, self::SEVERITY, true)) { |
| 58 | return $level; |
| 59 | } |
| 60 | |
| 61 | if (is_string($level) && ctype_digit($level) && in_array((int)$level, self::SEVERITY, true)) { |
| 62 | return (int)$level; |
| 63 | } |
| 64 | |
| 65 | if (is_string($level)) { |
| 66 | $name = strtolower($level); |
| 67 | |
| 68 | if (array_key_exists($name, self::SEVERITY)) { |
| 69 | return self::SEVERITY[$name]; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | throw new InvalidArgumentException( |
| 74 | 'Error: The level ' . get_debug_type($level) . (is_scalar($level) ? ' (' . var_export($level, true) . ')' : '') . ' is an invalid level.' |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Convert a PSR-3 level string into its uppercase display name |
| 80 | * |
| 81 | * @param string $level |
| 82 | * @throws InvalidArgumentException |
| 83 | * @return string |
| 84 | */ |
| 85 | public static function toName(string $level): string |
| 86 | { |
| 87 | $name = strtolower($level); |
| 88 | |
| 89 | if (!array_key_exists($name, self::SEVERITY)) { |
| 90 | throw new InvalidArgumentException('Error: The level ' . $name . ' is an invalid level.'); |
| 91 | } |
| 92 | |
| 93 | return strtoupper($name); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Convert a severity int (0-7) into its canonical PSR-3 level string |
| 98 | * |
| 99 | * @param int $severity |
| 100 | * @throws InvalidArgumentException |
| 101 | * @return string |
| 102 | */ |
| 103 | public static function fromSeverity(int $severity): string |
| 104 | { |
| 105 | $level = array_search($severity, self::SEVERITY, true); |
| 106 | |
| 107 | if ($level === false) { |
| 108 | throw new InvalidArgumentException('Error: The severity ' . $severity . ' is an invalid severity.'); |
| 109 | } |
| 110 | |
| 111 | return $level; |
| 112 | } |
| 113 | |
| 114 | } |