summaryrefslogtreecommitdiffstats
path: root/codebase/mixed_connector.php
blob: bbef8d4ba6fb8a9e1f7045578c4c89db26228b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
	@author dhtmlx.com
	@license GPL, see license.txt
*/
require_once("base_connector.php");

class MixedConnector extends Connector {

    private $_data_type = null;

    function __construct($dataType = "json") {
        $this->_data_type = $dataType;
    }

    protected $attributes = array();
    protected $connectors = array();

    public function add($name, $conn) {
        $this->connectors[$name] = $conn;
        $conn->simple = true;
    }

    public function render() {
        if($this->_data_type == "json")
            $this->render_json();
        else
            $this->render_xml();
    }

    private function render_json() {
        $result = "{";
        $parts = array();
        foreach($this->connectors as $name => $conn) {
            $conn->asString(true);
            $parts[] = "\"".$name."\":".($conn->render())."\n";
        }
        $result .= implode(",\n", $parts)."}";
        echo $result;
    }

    private function render_xml() {
        $result = "";
        $parts = array();

        foreach($this->connectors as $name => $conn) {
            $conn->asString(true);
            $parts[] = "<".$name.">".($conn->render())."</".$name.">\n";
        }
        $result .= implode("", $parts);
        $this->output_as_xml($result);
    }

    protected function output_as_xml($res) {
        echo "<?xml version='1.0' encoding='".$this->encoding."' ?>".$this->xml_start().$res.$this->xml_end();
    }
}

?>