Passed
Push — master ( 24ab68...9218d1 )
by Alexander
04:58 queued 57s
created

XmlRenderer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A cdata() 0 3 1
A render() 0 12 1
A tag() 0 3 1
1
<?php
2
namespace Yiisoft\Yii\Web\ErrorHandler;
3
4
/**
5
 * Formats exception into XML string
6
 */
7
class XmlRenderer implements ErrorRendererInterface
8
{
9
    public function render(\Throwable $e): string
10
    {
11
        $out = '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes" ?' . ">\n";
12
        $out .= "<error>\n";
13
        $out .= $this->tag('type', get_class($e));
14
        $out .= $this->tag('message', $this->cdata($e->getMessage()));
15
        $out .= $this->tag('code', $this->cdata($e->getCode()));
16
        $out .= $this->tag('file', $e->getFile());
17
        $out .= $this->tag('line', $e->getLine());
18
        $out .= $this->tag('trace', $e->getTraceAsString());
19
        $out .= '</error>';
20
        return $out;
21
    }
22
23
    private function tag(string $name, string $value): string
24
    {
25
        return "<$name>" . $value . "</$name>\n";
26
    }
27
28
    private function cdata(string $value): string
29
    {
30
        return '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $value) . ']]>';
31
    }
32
}
33