Skip to content

Commit f6d167b

Browse files
committed
Merge pull request peej#5 from jkulak/json-output
Added json output
2 parents 46ff2c1 + 7509fcf commit f6d167b

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

json.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* PHP REST SQL JSON renderer class
5+
* This class renders the REST response data as JSON.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation; either version 2 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program; if not, write to the Free Software
19+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+
21+
*/
22+
class PHPRestSQLRenderer {
23+
24+
/**
25+
* @var PHPRestSQL PHPRestSQL
26+
*/
27+
var $PHPRestSQL;
28+
29+
/**
30+
* Constructor.
31+
* @param PHPRestSQL PHPRestSQL
32+
*/
33+
function render($PHPRestSQL) {
34+
$this->PHPRestSQL = $PHPRestSQL;
35+
switch($PHPRestSQL->display) {
36+
case 'database':
37+
$this->database();
38+
break;
39+
case 'table':
40+
$this->table();
41+
break;
42+
case 'row':
43+
$this->row();
44+
break;
45+
}
46+
}
47+
48+
/**
49+
* Output the top level table listing.
50+
*/
51+
function database() {
52+
header('Content-Type: application/json');
53+
if (isset($this->PHPRestSQL->output['database'])) {
54+
echo json_encode($this->PHPRestSQL->output['database']);
55+
}
56+
}
57+
58+
/**
59+
* Output the rows within a table.
60+
*/
61+
function table() {
62+
header('Content-Type: application/json');
63+
if (isset($this->PHPRestSQL->output['table'])) {
64+
echo json_encode($this->PHPRestSQL->output['table']);
65+
}
66+
}
67+
68+
/**
69+
* Output the entry in a table row.
70+
*/
71+
function row() {
72+
header('Content-Type: application/json');
73+
if (isset($this->PHPRestSQL->output['row'])) {
74+
echo json_encode($this->PHPRestSQL->output['row']);
75+
}
76+
}
77+
78+
}

phprestsql.ini

+2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ foreignKeyPostfix = "_uid"
1414
text/xml = xml.php
1515
text/plain = plain.php
1616
text/html = html.php
17+
application/json = json.php
1718

1819
[mimetypes]
1920
xml = text/xml
2021
txt = text/plain
2122
html = text/html
23+
json = application/json

0 commit comments

Comments
 (0)