MessageInterpolationTrait::interpolateMessage()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
cc 4
nc 2
nop 2
1
<?php
2
3
namespace SubjectivePHP\Psr\Log;
4
5
/**
6
 * Trait for PSR-3 Message Interpolation.
7
 */
8
trait MessageInterpolationTrait
9
{
10
    /**
11
     * Interpolates context values into the message placeholders.
12
     *
13
     * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#12-message
14
     *
15
     * @param string $message The string containing the placeholders.
16
     * @param array  $context The key/value array of replacement values.
17
     *
18
     * @return string
19
     */
20
    protected function interpolateMessage($message, array $context)//@codingStandardsIgnoreLine Ignore missing type hint
21
    {
22
        $context = array_filter(
23
            $context,
24
            function ($value) {
25
                return (is_scalar($value) || (is_object($value) && method_exists($value, '__toString')));
26
            }
27
        );
28
29
        $search = [];
30
        $replace = [];
31
        foreach ($context as $key => $value) {
32
            $search[] = "{{$key}}";
33
            $replace[] = (string)$value;
34
        }
35
36
        return str_replace($search, $replace, $message);
37
    }
38
}
39