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

midcom_response_xml   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 15.38%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 47
ccs 4
cts 26
cp 0.1538
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _render_tag() 0 24 6
A __construct() 0 4 1
A sendContent() 0 10 2
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