-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputBase.class.php
More file actions
113 lines (103 loc) · 2.65 KB
/
Copy pathOutputBase.class.php
File metadata and controls
113 lines (103 loc) · 2.65 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Class OutputBase
* Class for fetching data from DB and offering them as a XML
*/
class OutputBase
{
protected $db;
protected $xml = '';
public function __construct()
{
$this->db = db_mysql::singleton();
}
/**
* This returns XML for given query
* @param string $query
* @param string $rowBracket
* @return string
*/
public function queryToXML($query, $rowBracket)
{
$xml = '';
$result = $this->db->query($query);
while (false !== ($row = $this->db->fetchAssoc($result))) {
$xml .= $this->createElement($rowBracket, $this->parseRow($row));
}
return $xml;
}
/**
* Apply callbacks for one row
* @param $row
* @return string
*/
protected function parseRow($row)
{
$xml = '';
foreach ($row as $colName => $value) {
if (isset($this->callbacks[$colName])) {
foreach ($this->callbacks[$colName] as $callback) {
if (is_callable(array($this, $callback['function']))) {
$this->$callback['function']($value, $callback['parameters']);
}
}
}
$xml .= sprintf('<%1$s>%2$s</%1$s>', $colName, $value);
}
return $xml;
}
/**
* Set callback function
* @param $colName
* @param $function
* @param null $parameters
*/
public function addCallback($colName, $function, $parameters = null)
{
if (!isset($this->callbacks[$colName])) {
$this->callbacks[$colName] = array();
}
$this->callbacks[$colName][] = array('function' => $function, 'parameters' => $parameters);
}
/**
* Purge all callback functions
*/
public function clearAllCallbacks()
{
$this->callbacks = array();
}
/**
* Creates a XML document
* @param $name
* @param null $content
* @param bool $addCDATA
* @return string
*/
public static function createElement($name, $content = null, $addCDATA = false)
{
if ($content === null) {
return sprintf('<%1$s />', $name);
}
if ($addCDATA) {
self::addCDATA($content);
}
return sprintf('<%1$s>%2$s</%1$s>', $name, $content);
}
/**
* Callback function to add CDATA
* @param $value
*/
public static function addCDATA(&$value)
{
$value = '<![CDATA[' . $value . ']]>';
}
/**
* Callback function urlencode
* @param $value
*/
public static function URLEncode(&$url)
{
$url = urlencode($url);
}
}
?>