Completed
Push — master ( a9076d...92dee0 )
by Andreas
13:58
created

midcom_response_xml::sendContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * @package midcom
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Wrapper for HTTP responses
11
 *
12
 * @package midcom
13
 */
14
class midcom_response_xml extends midcom_response
15
{
16 1
    public function __construct()
17
    {
18 1
        parent::__construct();
19 1
        $this->headers->set('Content-type', 'text/xml; charset=' . $this->encoding);
20 1
    }
21
22
    /**
23
     * Sends the response to the client and shuts down the environment
24
     */
25
    public function sendContent()
26
    {
27
        echo '<?xml version="1.0" encoding="' . $this->encoding . '" standalone="yes"?>' . "\n";
28
        echo "<response>\n";
29
30
        foreach ($this->_data as $field => $value) {
31
            echo $this->_render_tag($field, $value);
32
        }
33
34
        echo "</response>\n";
35
    }
36
37
    private function _render_tag($field, $value)
38
    {
39
        $output = '';
40
        if (is_array($value)) {
41
            $subtypes = [];
42
            foreach ($value as $key => $subvalue) {
43
                if (is_int($key)) {
44
                    $output .= $this->_render_tag($field, $subvalue);
45
                } else {
46
                    $subtypes[$key] = $subvalue;
47
                }
48
            }
49
50
            if (!empty($subtypes)) {
51
                $subtype_string = '';
52
                foreach ($subtypes as $key => $subvalue) {
53
                    $subtype_string .= $this->_render_tag($key, $subvalue);
54
                }
55
                $output .= $this->_render_tag($field, $subtype_string);
56
            }
57
        } else {
58
            $output .= '<' . $field . '>' . $value . '</' . $field . ">\n";
59
        }
60
        return $output;
61
    }
62
}
63