Test Failed
Pull Request — master (#25)
by Evgeniy
02:14
created

XmlRenderer::renderVerbose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
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