Passed
Push — develop ( 899798...74e1f6 )
by nguereza
02:00
created

AbstractFormatter::interpolate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Logger
5
 *
6
 * Platine Logger is the implementation of PSR 3
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Logger
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file AbstractFormatter.php
33
 *
34
 *  The base logger formatter class
35
 *
36
 *  @package    Platine\Logger\Formatter
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Logger\Formatter;
48
49
use DateTimeImmutable;
50
use Platine\Logger\LoggerFormatterInterface;
51
use Platine\Stdlib\Helper\Str;
52
use Throwable;
53
54
/**
55
 * Class AbstractFormatter
56
 * @package Platine\Logger\Formatter
57
 */
58
abstract class AbstractFormatter implements LoggerFormatterInterface
59
{
60
    /**
61
     * Log fields separated by tabs to form a TSV (CSV with tabs).
62
     * @var string
63
     */
64
    protected string $tab = "\t";
65
66
    /**
67
     * Get Exception information data
68
     * @param  Throwable $exception
69
     * @return array<string, mixed>        the exception data
70
     */
71
    protected function getExceptionData(Throwable $exception): array
72
    {
73
        $data = [
74
            'message' => $exception->getMessage(),
75
            'code' => $exception->getCode(),
76
            'file' => $exception->getFile(),
77
            'line' => $exception->getLine()
78
        ];
79
80
        $traces = $exception->getTrace();
81
        $traceStr = "\n";
82
        foreach ($traces as $i => $trace) {
83
            $traceStr .= sprintf(
84
                '%d. %s:%s%s%s(%s)::%d',
85
                $i + 1,
86
                $trace['file'] ?? '',
87
                $trace['class'] ?? '',
88
                $trace['type'] ?? '',
89
                $trace['function'] ?? '',
90
                isset($trace['args']) ? '...' : '',
91
                $trace['line'] ?? ''
92
            ) . "\n";
93
        }
94
95
        $data['trace'] = $traceStr;
96
97
        return $data;
98
    }
99
100
    /**
101
     * Interpolates context values into the message placeholders.
102
     * @param  string $message
103
     * @param  array<string, mixed>  $context
104
     * @return string
105
     */
106
    protected function interpolate(string $message, array $context): string
107
    {
108
        if (strpos($message, '{') === false) {
109
            return $message;
110
        }
111
112
        $replacements = [];
113
        foreach ($context as $key => $value) {
114
            $replacements['{' . $key . '}'] = Str::stringify($value);
115
        }
116
117
        return strtr($message, $replacements);
118
    }
119
120
    /**
121
     * Get the current time for logging
122
     * Format: YYYY-mm-dd HH:ii:ss.uuuuuu
123
     * Microsecond precision for PHP 7.1 and greater
124
     *
125
     * @return string
126
     */
127
    protected function getLogTime(): string
128
    {
129
        return (new DateTimeImmutable('now'))->format('Y-m-d H:i:s.u');
130
    }
131
}
132