1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace WyriHaximus\PSR3; |
||
6 | |||
7 | use DateTimeInterface; |
||
8 | use JsonSerializable; |
||
9 | use Psr\Log\InvalidArgumentException; |
||
10 | use Psr\Log\LogLevel; |
||
11 | |||
12 | use function array_key_exists; |
||
13 | use function array_keys; |
||
14 | use function get_resource_type; |
||
15 | use function gettype; |
||
16 | use function implode; |
||
17 | use function is_array; |
||
18 | use function is_object; |
||
19 | use function is_resource; |
||
20 | use function is_scalar; |
||
21 | use function method_exists; |
||
22 | use function sprintf; |
||
23 | use function strpos; |
||
24 | use function strtolower; |
||
25 | use function strtr; |
||
26 | |||
27 | final readonly class Utils |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
28 | { |
||
29 | /** |
||
30 | * Logging levels PSR-3 LogLevel enum. |
||
31 | */ |
||
32 | public const array LOG_LEVELS = [ |
||
33 | LogLevel::DEBUG => 'DEBUG', |
||
34 | LogLevel::INFO => 'INFO', |
||
35 | LogLevel::NOTICE => 'NOTICE', |
||
36 | LogLevel::WARNING => 'WARNING', |
||
37 | LogLevel::ERROR => 'ERROR', |
||
38 | LogLevel::CRITICAL => 'CRITICAL', |
||
39 | LogLevel::ALERT => 'ALERT', |
||
40 | LogLevel::EMERGENCY => 'EMERGENCY', |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Functions in this file are a continuation of the code from this |
||
45 | * https://github.com/Seldaek/monolog/blob/6e6586257d9fb231bf039563632e626cdef594e5/src/Monolog/Processor/PsrLogMessageProcessor.php file. |
||
46 | */ |
||
47 | |||
48 | /** @param array<string, mixed> $context */ |
||
49 | public static function processPlaceHolders(string $message, array $context): string |
||
50 | { |
||
51 | if (strpos($message, '{') === false) { |
||
52 | return $message; |
||
53 | } |
||
54 | |||
55 | $replacements = []; |
||
56 | foreach ($context as $key => $value) { |
||
57 | $replacements['{' . $key . '}'] = self::formatValue($value); |
||
58 | } |
||
59 | |||
60 | return strtr($message, $replacements); |
||
61 | } |
||
62 | |||
63 | public static function formatValue(mixed $value): string |
||
64 | { |
||
65 | if ($value === null || is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) { |
||
66 | return (string) $value; |
||
67 | } |
||
68 | |||
69 | if (is_object($value)) { |
||
70 | return '[object ' . $value::class . ']'; |
||
71 | } |
||
72 | |||
73 | return '[' . gettype($value) . ']'; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param array<string, mixed> $context |
||
78 | * |
||
79 | * @return array<string, mixed> |
||
80 | */ |
||
81 | public static function normalizeContext(array $context): array |
||
82 | { |
||
83 | foreach ($context as $index => $value) { |
||
84 | if ($value instanceof JsonSerializable) { |
||
85 | $value = $value->jsonSerialize(); |
||
86 | } |
||
87 | |||
88 | if ($value instanceof DateTimeInterface) { |
||
89 | /** @phpstan-ignore-next-line */ |
||
90 | $value = (array) $value; |
||
91 | } |
||
92 | |||
93 | if (is_array($value)) { |
||
94 | /** @phpstan-ignore-next-line */ |
||
95 | $context[$index] = self::normalizeContext($value); |
||
96 | continue; |
||
97 | } |
||
98 | |||
99 | if (is_resource($value)) { |
||
100 | $context[$index] = sprintf('[resource] (%s)', get_resource_type($value)); |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | $context[$index] = self::formatValue($value); |
||
105 | } |
||
106 | |||
107 | return $context; |
||
108 | } |
||
109 | |||
110 | public static function checkCorrectLogLevel(string $level): bool |
||
111 | { |
||
112 | $level = strtolower($level); |
||
113 | if (! array_key_exists($level, self::LOG_LEVELS)) { |
||
114 | throw new InvalidArgumentException( |
||
115 | 'Level "' . $level . '" is not defined, use one of: ' . implode(', ', array_keys(self::LOG_LEVELS)), |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | return true; |
||
120 | } |
||
121 | } |
||
122 |