Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Context | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
11 | |
100.00% |
1 / 1 |
| serialize | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
10 | |||
| sanitize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | /** |
| 18 | * Log context utility class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Log |
| 22 | * @author Nick Sagona, III <dev@noladev.com> |
| 23 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | final class Context |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Serialize a context array (minus timestamp/name/format) as text, JSON, or PHP-serialized, |
| 32 | * depending on context['format'] |
| 33 | * |
| 34 | * @param array $context |
| 35 | * @return string |
| 36 | */ |
| 37 | public static function serialize(array $context): string |
| 38 | { |
| 39 | $messageContext = ''; |
| 40 | |
| 41 | if (isset($context['timestamp'])) { |
| 42 | unset($context['timestamp']); |
| 43 | } |
| 44 | if (isset($context['name'])) { |
| 45 | unset($context['name']); |
| 46 | } |
| 47 | if (isset($context['format'])) { |
| 48 | $format = $context['format']; |
| 49 | unset($context['format']); |
| 50 | } else { |
| 51 | $format = 'text'; |
| 52 | } |
| 53 | |
| 54 | switch ($format) { |
| 55 | // If the data values needs to be preserved, use JSON encoding or PHP serialization |
| 56 | case 'json': |
| 57 | $messageContext = json_encode($context); |
| 58 | break; |
| 59 | case 'php': |
| 60 | $messageContext = serialize($context); |
| 61 | break; |
| 62 | // Else, complex values like arrays and objects will get reduced to a basic string representation, i.e. [Array] |
| 63 | default: |
| 64 | foreach ($context as $key => $value) { |
| 65 | if (is_array($value)) { |
| 66 | $value = '[Array]'; |
| 67 | } |
| 68 | if (is_object($value)) { |
| 69 | $value = '[Object]'; |
| 70 | } |
| 71 | $messageContext .= (string)$key . '=' . (string)$value . ';'; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return $messageContext; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Strip CR/LF from a value before it's written into a delimited/line-based log format, to prevent an |
| 80 | * embedded newline from forging a fake extra line (or, for writers that build header-style text like |
| 81 | * Mail's Subject line, a fake extra header). |
| 82 | * |
| 83 | * @param string $value |
| 84 | * @return string |
| 85 | */ |
| 86 | public static function sanitize(string $value): string |
| 87 | { |
| 88 | return str_replace(["\r", "\n"], ' ', $value); |
| 89 | } |
| 90 | |
| 91 | } |