Completed
Branch master (a3fffb)
by Hong
33:51 queued 28:35
created

LogEntry::getProcessors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Logger
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Logger\Entry;
13
14
use Psr\Log\LogLevel;
15
use Psr\Log\InvalidArgumentException;
16
use Phoole\Logger\Processor\ProcessorAwareTrait;
17
18
/**
19
 * Log message prototype
20
 *
21
 * @package Phoole\Logger
22
 */
23
class LogEntry implements LogEntryInterface
24
{
25
    use LogLevelTrait;
26
    use ProcessorAwareTrait;
27
28
    /**
29
     * message template
30
     *
31
     * @var string
32
     */
33
    protected $message = 'log message';
34
35
    /**
36
     * @var string
37
     */
38
    protected $level = LogLevel::INFO;
39
40
    /**
41
     * @var array
42
     */
43
    protected $context;
44
45
    /**
46
     * @param  string $message
47
     * @param  array  $context
48
     */
49
    public function __construct(string $message = '', array $context = [])
50
    {
51
        if (!empty($message)) {
52
            $this->message = $message;
53
        }
54
        $this->context = $context;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function getMessage(): string
61
    {
62
        return $this->message;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function setLevel(string $level)
69
    {
70
        if (!isset($this->convert[$level])) {
71
            throw new InvalidArgumentException("unknown log level");
72
        }
73
        $this->level = $level;
74
        return $this;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function getLevel(): string
81
    {
82
        return $this->level;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function setContext(array $context)
89
    {
90
        $this->context = $context;
91
        return $this;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function getContext(): array
98
    {
99
        return $this->context;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function __toString(): string
106
    {
107
        $this->process();
108
        return $this->interpolate($this->getMessage(), $this->getContext());
109
    }
110
111
    /**
112
     * @param  string $message  message
113
     * @param  array  $context
114
     * @return string result
115
     */
116
    protected function interpolate(string $message, array $context): string
117
    {
118
        $replace = array();
119
        foreach ($context as $key => $val) {
120
            if (
121
                !is_array($val) &&
122
                (!is_object($val) ||
123
                    method_exists($val, '__toString'))
124
            ) {
125
                $replace['{' . $key . '}'] = $val;
126
            }
127
        }
128
        return strtr($message, $replace);
129
    }
130
}