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

XmlRenderer::cdata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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