Handler::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Tleckie\Log\Formatter\Handler;
4
5
/**
6
 * Class Handler
7
 *
8
 * @package Tleckie\Log\Formatter\Handler
9
 * @author  Teodoro Leckie Westberg <[email protected]>
10
 */
11
class Handler implements HandlerInterface
12
{
13
    /** @var HandlerInterface|null */
14
    protected HandlerInterface|null $nextHandler = null;
15
16
    /**
17
     * @param HandlerInterface $handler
18
     * @return HandlerInterface
19
     */
20
    public function next(HandlerInterface $handler): HandlerInterface
21
    {
22
        $this->nextHandler = $handler;
23
24
        return $handler;
25
    }
26
27
    /**
28
     * @param mixed $message
29
     * @param array $context
30
     * @return string
31
     */
32
    public function handler(mixed $message, array $context = []): string
33
    {
34
        if ($this->nextHandler instanceof HandlerInterface) {
35
            return $this->nextHandler->handler($message, $context);
36
        }
37
38
        $clear = addslashes($this->clearEof($message));
39
40
        return sprintf('"%s" {}', $clear);
41
    }
42
43
    /**
44
     * @param string $value
45
     * @return string
46
     */
47
    protected function clearEof(string $value): string
48
    {
49
        return str_replace(PHP_EOL, '', $value);
50
    }
51
52
    /**
53
     * @param $value
54
     * @return string
55
     */
56
    protected function encode($value): string
57
    {
58
        return json_encode($value, JSON_UNESCAPED_SLASHES) ?? '';
59
    }
60
}
61