Passed
Push — master ( 7349d0...85c6cc )
by Nikolaos
04:18
created

AbstractFormatter::interpolate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * For the full copyright and license information, please view the LICENSE.md
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Phalcon\Logger\Formatter;
13
14
use function is_array;
15
16
/**
17
 * Class AbstractFormatter
18
 *
19
 * @package Phalcon\Logger\Formatter
20
 */
21
abstract class AbstractFormatter implements FormatterInterface
22
{
23
    /**
24
     * Interpolates context values into the message placeholders
25
     *
26
     * @see http://www.php-fig.org/psr/psr-3/ Section 1.2 Message
27
     *
28
     * @param string     $message
29
     * @param array|null $context
30
     *
31
     * @return string
32
     */
33 30
    public function interpolate(string $message, $context = null): string
34
    {
35 30
        if (is_array($context) && count($context) > 0) {
36 4
            $replace = [];
37 4
            foreach ($context as $key => $value) {
38 4
                $replace["{" . $key . "}"] = $value;
39
            }
40
41 4
            return strtr($message, $replace);
42
        }
43
44 26
        return $message;
45
    }
46
    /**
47
     * Returns the date formatted for the logger.
48
     * @todo Not using the set time from the Item since we have interface
49
     * misalignment which will break semver This will change in the future
50
     */
51 27
    protected function getFormattedDate(): string
52
    {
53 27
        $timezone = date_default_timezone_get();
54 27
        $date = new \DateTimeImmutable("now", new \DateTimeZone($timezone));
55
56 27
        return $date->format($this->dateFormat);
57
    }
58
}
59