Completed
Push — master ( 9d0a53...ecd7bc )
by Maik
01:48
created

Interpolator::interpolate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
cc 3
eloc 6
nc 2
nop 2
crap 12
1
<?php
2
3
/**
4
 * This file is part of the PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Util;
9
10
/**
11
 *
12
 * @author Maik Greubel <[email protected]>
13
 */
14
trait Interpolator
15
{
16
17
    /**
18
     * Interpolates context values into the message placeholders.
19
     *
20
     * @param string $message
21
     *            The message containing placeholders
22
     * @param array $context
23
     *            The context array containing the replacers
24
     *            
25
     * @return string The interpolated message
26
     */
27
    private static function interpolate($message, array $context = array()): string
28
    {
29
        $replace = array();
30
        
31
        if ($context !== null) {
32
            foreach ($context as $key => $val) {
33
                $replace['{' . $key . '}'] = $val;
34
            }
35
        }
36
        
37
        return strtr($message, $replace);
38
    }
39
}
40