Created PHP and JS examples

This commit is contained in:
Maurits van der Schee 2015-02-17 22:47:38 +01:00
commit 8107de953a
5 changed files with 97 additions and 157 deletions

View file

@ -1,8 +1,15 @@
<html>
<head>
<script src="client.js"></script>
<script src="mysql_crud_api_transform.js"></script>
<script>
function callback(jsonObject) {
jsonObject = mysql_crud_api_transform(jsonObject);
document.getElementById('output').innerHTML = JSON.stringify(jsonObject, undefined, 4);
}
</script>
<script src="http://localhost/api.php/posts,categories,tags,comments?filter=id:1&callback=callback" defer="defer"></script>
</head>
<body>
client.js
<pre id="output"></pre>
</body>
</html>

120
client.js
View file

@ -1,120 +0,0 @@
/*
Copyright (c) 2008 Stefan Lange-Hegermann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
function microAjax(url, callbackFunction)
{
this.bindFunction = function (caller, object) {
return function() {
return caller.apply(object, [object]);
};
};
this.stateChange = function (object) {
if (this.request.readyState==4)
this.callbackFunction(this.request.responseText);
};
this.getRequest = function() {
if (window.ActiveXObject)
return new ActiveXObject('Microsoft.XMLHTTP');
else if (window.XMLHttpRequest)
return new XMLHttpRequest();
return false;
};
this.postBody = (arguments[2] || "");
this.callbackFunction=callbackFunction;
this.url=url;
this.request = this.getRequest();
if(this.request) {
var req = this.request;
req.onreadystatechange = this.bindFunction(this.stateChange, this);
if (this.postBody!=="") {
req.open("POST", url, true);
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.setRequestHeader('Connection', 'close');
} else {
req.open("GET", url, true);
}
req.send(this.postBody);
}
}
// discuss at: http://phpjs.org/functions/array_flip/
function array_flip(trans) {
var key, tmp_ar = {};
for (key in trans) {
if (!trans.hasOwnProperty(key)) {
continue;
}
tmp_ar[trans[key]] = key;
}
return tmp_ar;
}
// other
function get_objects(tables,table_name,where_index,match_value) {
var objects = [];
for (var record in tables[table_name]['records']) {
record = tables[table_name]['records'][record];
if (!where_index || record[where_index]==match_value) {
var object = {};
for (var index in tables[table_name]['columns']) {
var column = tables[table_name]['columns'][index];
object[column] = record[index];
for (var relation in tables) {
var reltable = tables[relation];
for (var key in reltable['relations']) {
var target = reltable['relations'][key];
if (target == table_name+'.'+column) {
column_indices = array_flip(reltable['columns']);
object[relation] = get_objects(tables,relation,column_indices[key],record[index]);
}
}
}
}
objects.push(object);
}
}
return objects;
}
function get_tree(tables) {
tree = {};
for (var name in tables) {
var table = tables[name];
if (!table['relations']) {
tree[name] = get_objects(tables,name);
}
}
return tree;
}
microAjax("http://localhost/api.php/posts,categories,tags,comments?filter=id:1", function (response) {
var jsonObject = eval('(' + response + ')');
console.log(get_tree(jsonObject));
});

View file

@ -1,4 +1,5 @@
<?php
require "mysql_crud_api_transform.php";
function call($method, $url, $data = false) {
$ch = curl_init();
@ -12,40 +13,18 @@ function call($method, $url, $data = false) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return json_decode(curl_exec($ch),true);
return curl_exec($ch);
}
function get_objects(&$tables,$table_name,$where_index=false,$match_value=false) {
$objects = array();
foreach ($tables[$table_name]['records'] as $record) {
if ($where_index===false || $record[$where_index]==$match_value) {
$object = array();
foreach ($tables[$table_name]['columns'] as $index=>$column) {
$object[$column] = $record[$index];
foreach ($tables as $relation=>$reltable) {
foreach ($reltable['relations'] as $key=>$target) {
if ($target == "$table_name.$column") {
$column_indices = array_flip($reltable['columns']);
$object[$relation] = get_objects($tables,$relation,$column_indices[$key],$record[$index]);
}
}
}
}
$objects[] = $object;
}
}
return $objects;
}
function get_tree(&$tables) {
$tree = array();
foreach ($tables as $name=>$table) {
if (!isset($table['relations'])) {
$tree[$name] = get_objects($tables,$name);
}
}
return $tree;
}
header('Content-Type: text/plain');
print_r(get_tree(call('GET','http://localhost/api.php/posts,categories,tags,comments?filter=id:1')));
$response = call('GET','http://localhost/api.php/posts,categories,tags,comments?filter=id:1');
$jsonObject = json_decode($response,true);
$jsonObject = mysql_crud_api_transform($jsonObject);
$output = json_encode($jsonObject,JSON_PRETTY_PRINT);
?>
<html>
<head>
</head>
<body>
<pre><?php echo $output ?></pre>
</body>
</html>

View file

@ -0,0 +1,42 @@
function mysql_crud_api_transform(tables) {
var array_flip = function (trans) {
var key, tmp_ar = {};
for (key in trans) {
tmp_ar[trans[key]] = key;
}
return tmp_ar;
};
var get_objects = function (tables,table_name,where_index,match_value) {
var objects = [];
for (var record in tables[table_name]['records']) {
record = tables[table_name]['records'][record];
if (!where_index || record[where_index]==match_value) {
var object = {};
for (var index in tables[table_name]['columns']) {
var column = tables[table_name]['columns'][index];
object[column] = record[index];
for (var relation in tables) {
var reltable = tables[relation];
for (var key in reltable['relations']) {
var target = reltable['relations'][key];
if (target == table_name+'.'+column) {
column_indices = array_flip(reltable['columns']);
object[relation] = get_objects(tables,relation,column_indices[key],record[index]);
}
}
}
}
objects.push(object);
}
}
return objects;
};
tree = {};
for (var name in tables) {
var table = tables[name];
if (!table['relations']) {
tree[name] = get_objects(tables,name);
}
}
return tree;
}

View file

@ -0,0 +1,32 @@
<?php
function mysql_crud_api_transform(&$tables) {
$get_objects = function (&$tables,$table_name,$where_index=false,$match_value=false) use (&$get_objects) {
$objects = array();
foreach ($tables[$table_name]['records'] as $record) {
if ($where_index===false || $record[$where_index]==$match_value) {
$object = array();
foreach ($tables[$table_name]['columns'] as $index=>$column) {
$object[$column] = $record[$index];
foreach ($tables as $relation=>$reltable) {
foreach ($reltable['relations'] as $key=>$target) {
if ($target == "$table_name.$column") {
$column_indices = array_flip($reltable['columns']);
$object[$relation] = $get_objects($tables,$relation,$column_indices[$key],$record[$index]);
}
}
}
}
$objects[] = $object;
}
}
return $objects;
};
$tree = array();
foreach ($tables as $name=>$table) {
if (!isset($table['relations'])) {
$tree[$name] = $get_objects($tables,$name);
}
}
return $tree;
}