Passed
Pull Request — master (#25)
by Evgeniy
02:23
created

XmlRenderer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 0
loc 33
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cdata() 0 3 1
A tag() 0 3 1
A renderVerbose() 0 12 1
A render() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ErrorHandler\Renderer;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Throwable;
9
use Yiisoft\ErrorHandler\ThrowableRenderer;
10
11
/**
12
 * Formats exception into XML string.
13
 */
14
final class XmlRenderer extends ThrowableRenderer
15
{
16
    public function render(Throwable $t, ServerRequestInterface $request = null): string
17
    {
18
        $out = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
19
        $out .= "<error>\n";
20
        $out .= $this->tag('message', 'An internal server error occurred');
21
        $out .= '</error>';
22
        return $out;
23
    }
24
25
    public function renderVerbose(Throwable $t, ServerRequestInterface $request = null): string
26
    {
27
        $out = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
28
        $out .= "<error>\n";
29
        $out .= $this->tag('type', get_class($t));
30
        $out .= $this->tag('message', $this->cdata($t->getMessage()));
31
        $out .= $this->tag('code', $this->cdata((string) $t->getCode()));
32
        $out .= $this->tag('file', $t->getFile());
33
        $out .= $this->tag('line', (string) $t->getLine());
34
        $out .= $this->tag('trace', $t->getTraceAsString());
35
        $out .= '</error>';
36
        return $out;
37
    }
38
39
    private function tag(string $name, string $value): string
40
    {
41
        return "<$name>" . $value . "</$name>\n";
42
    }
43
44
    private function cdata(string $value): string
45
    {
46
        return '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $value) . ']]>';
47
    }
48
}
49