foreach($reflection->getMethods() as $method){
- if ($method->getName()!="get_function_webservice_description"
- && $method->getName()!="get_descriptions" ) {
- $docBlock = $method->getDocComment();
+ $docBlock = $method->getDocComment();
+ //retrieve the return and add it into the description if not array|object
+ preg_match_all('/@return\s+(\w+)\s+((?:\$)?\w+)/', $docBlock, $returnmatches);
- //retrieve the return and add it into the description if not array|object
- preg_match_all('/@return\s+(\w+)\s+((?:\$)?\w+)/', $docBlock, $returnmatches);
+ //retrieve the subparam and subreturn
+ preg_match_all('/\s*\*\s*@(subparam|subreturn)\s+(\w+)\s+(\$\w+(?::\w+|->\w+)+)((?:\s+(?:optional|required|multiple))*)/', $docBlock, $matches);
- //retrieve the subparam and subreturn
- preg_match_all('/\s*\*\s*@(subparam|subreturn)\s+(\w+)\s+(\$\w+(?::\w+|->\w+)+)((?:\s+(?:optional|required|multiple))*)/', $docBlock, $matches);
+ for($i=0;$i<sizeof($matches[1]);$i++){
+ // var_dump(strpos("optional", $matches[4][$i]));
+ switch ($matches[1][$i]) {
+ case "subparam":
+ if (strpos($matches[4][$i], "optional")!==false) {
+ $descriptiontype = "optional";
+ } else {
+ $descriptiontype = "params" ;
+ }
+ break;
+ case "subreturn":
+ $descriptiontype = "return";
+ break;
+ }
- for($i=0;$i<sizeof($matches[1]);$i++){
- // var_dump(strpos("optional", $matches[4][$i]));
- switch ($matches[1][$i]) {
- case "subparam":
- if (strpos($matches[4][$i], "optional")!==false) {
- $descriptiontype = "optional";
- } else {
- $descriptiontype = "params" ;
- }
- break;
- case "subreturn":
- $descriptiontype = "return";
- break;
- }
+ if (empty($description[$method->getName()])) {
+ $description[$method->getName()] = array();
+ }
+
+ if (strpos($returnmatches[1][0] ,"object")===false && strpos($returnmatches[1][0],"array")===false) {
+ $description[$method->getName()]['return'] = array($returnmatches[2][0] => $returnmatches[1][0]);
+ }
+
+ if ($matches[1][$i] == "subparam" || $matches[1][$i] == "subreturn") {
- if (empty($description[$method->getName()])) {
- $description[$method->getName()] = array();
+
+ ///algorythm parts
+ ///1. We compare the string to the description array
+ /// When we find a attribut that is not in the description, we retrieve all the rest of the string
+ ///2. We create the missing part of the description array, starting from the end of the rest of the string
+ ///3. We add the missing part to the description array
+
+ ///Part 1.
+
+
+ ///construct the description array
+ if (strpos($matches[3][$i], "->")===false && strpos($matches[3][$i], ":")===false) {
+ //no separator
+ $otherparam = $matches[3][$i];
}
+ else if (strpos($matches[3][$i], "->")===false || (strpos($matches[3][$i], ":")!==false && (strpos($matches[3][$i], ":") < strpos($matches[3][$i], "->")))) {
+ $separator = ":";
+ $separatorsize=1;
- if (strpos($returnmatches[1][0] ,"object")===false && strpos($returnmatches[1][0],"array")===false) {
- $description[$method->getName()]['return'] = array($returnmatches[2][0] => $returnmatches[1][0]);
+ } else {
+ $separator = "->";
+ $separatorsize=2;
}
- if ($matches[1][$i] == "subparam" || $matches[1][$i] == "subreturn") {
+ $param = substr($matches[3][$i],1,strpos($matches[3][$i], $separator)-1);
+ $otherparam = substr($matches[3][$i],strpos($matches[3][$i], $separator)+$separatorsize);
+ $parsingdesc = $description[$method->getName()];
- ///algorythm parts
- ///1. We compare the string to the description array
- /// When we find a attribut that is not in the description, we retrieve all the rest of the string
- ///2. We create the missing part of the description array, starting from the end of the rest of the string
- ///3. We add the missing part to the description array
+ if (!empty($parsingdesc) && array_key_exists($descriptiontype, $parsingdesc)){
+ $parsingdesc = $parsingdesc[$descriptiontype];
+ }
+ $descriptionpath=array();
- ///Part 1.
+ $creationfinished = false;
+ unset($type);
+ while(!$creationfinished && (strpos($otherparam, ":") || strpos($otherparam, "->"))) {
+ if (strpos($otherparam, "->")===false || (strpos($otherparam, ":")!==false && (strpos($otherparam, ":") < strpos($otherparam, "->")))) {
+ $type = $separator;
- ///construct the description array
- if (strpos($matches[3][$i], "->")===false && strpos($matches[3][$i], ":")===false) {
- //no separator
- $otherparam = $matches[3][$i];
- }
- else if (strpos($matches[3][$i], "->")===false || (strpos($matches[3][$i], ":")!==false && (strpos($matches[3][$i], ":") < strpos($matches[3][$i], "->")))) {
$separator = ":";
$separatorsize=1;
-
} else {
+ $type = $separator;
$separator = "->";
$separatorsize=2;
}
- $param = substr($matches[3][$i],1,strpos($matches[3][$i], $separator)-1);
+ $param = substr($otherparam,0,strpos($otherparam, $separator));
- $otherparam = substr($matches[3][$i],strpos($matches[3][$i], $separator)+$separatorsize);
- $parsingdesc = $description[$method->getName()];
-
- if (!empty($parsingdesc) && array_key_exists($descriptiontype, $parsingdesc)){
- $parsingdesc = $parsingdesc[$descriptiontype];
- }
- $descriptionpath=array();
+ $otherparam = substr($otherparam,strpos($otherparam, $separator)+$separatorsize);
- $creationfinished = false;
- unset($type);
- while(!$creationfinished && (strpos($otherparam, ":") || strpos($otherparam, "->"))) {
- if (strpos($otherparam, "->")===false || (strpos($otherparam, ":")!==false && (strpos($otherparam, ":") < strpos($otherparam, "->")))) {
- $type = $separator;
-
- $separator = ":";
- $separatorsize=1;
- } else {
- $type = $separator;
- $separator = "->";
- $separatorsize=2;
- }
-
- $param = substr($otherparam,0,strpos($otherparam, $separator));
-
- $otherparam = substr($otherparam,strpos($otherparam, $separator)+$separatorsize);
+ if ($type==":") {
+ if (!array_key_exists('multiple:'.$param, $parsingdesc)){
+ $desctoadd = webservice_lib::create_end_of_description(":".$param.$separator.$otherparam, $matches[2][$i]);
- if ($type==":") {
- if (!array_key_exists('multiple:'.$param, $parsingdesc)){
-
- $desctoadd = webservice_lib::create_end_of_description(":".$param.$separator.$otherparam, $matches[2][$i]);
-
- if(empty($descriptionpath) ) {
- if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
- $desctoadd = array($descriptiontype => $desctoadd);
- }
- $paramtoadd = $descriptiontype;
- } else {
- $paramtoadd = 'multiple:'.$param;
+ if(empty($descriptionpath) ) {
+ if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
+ $desctoadd = array($descriptiontype => $desctoadd);
}
- webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
- $creationfinished = true;
+ $paramtoadd = $descriptiontype;
} else {
- if(empty($descriptionpath)) {
- $descriptionpath[] = $descriptiontype;
- }
- $descriptionpath[] = 'multiple:'.$param;
- $parsingdesc = $parsingdesc['multiple:'.$param];
+ $paramtoadd = 'multiple:'.$param;
}
+ webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
+ $creationfinished = true;
} else {
- if (!array_key_exists($param, $parsingdesc)){
-
- $desctoadd = webservice_lib::create_end_of_description("->".$param.$separator.$otherparam, $matches[2][$i]);
-
- if(empty($descriptionpath)) {
-
- if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
- $desctoadd = array($descriptiontype => $desctoadd);
- }
- $paramtoadd = $descriptiontype;
-
- } else {
- $paramtoadd = $param;
- }
- webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
-
- $creationfinished = true;
- } else {
- if(empty($descriptionpath)) {
- $descriptionpath[] = $descriptiontype;
- }
- $descriptionpath[] = $param;
- $parsingdesc = $parsingdesc[$param];
+ if(empty($descriptionpath)) {
+ $descriptionpath[] = $descriptiontype;
}
+ $descriptionpath[] = 'multiple:'.$param;
+ $parsingdesc = $parsingdesc['multiple:'.$param];
}
+ } else {
+ if (!array_key_exists($param, $parsingdesc)){
- }
-
- if (!$creationfinished) {
-
- if (!empty($type) && $type==":") {
-
- $desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]);
+ $desctoadd = webservice_lib::create_end_of_description("->".$param.$separator.$otherparam, $matches[2][$i]);
if(empty($descriptionpath)) {
+
if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
$desctoadd = array($descriptiontype => $desctoadd);
}
$paramtoadd = $descriptiontype;
+
} else {
- $paramtoadd = 'multiple:'.$param;
+ $paramtoadd = $param;
}
-
webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
+ $creationfinished = true;
} else {
- $desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]);
if(empty($descriptionpath)) {
+ $descriptionpath[] = $descriptiontype;
+ }
+ $descriptionpath[] = $param;
+ $parsingdesc = $parsingdesc[$param];
+ }
+ }
- if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
- $desctoadd = array($descriptiontype => $desctoadd);
- }
- $paramtoadd = $descriptiontype;
+ }
- } else {
- $paramtoadd = $param;
+ if (!$creationfinished) {
+
+ if (!empty($type) && $type==":") {
+ $desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]);
+
+ if(empty($descriptionpath)) {
+ if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
+ $desctoadd = array($descriptiontype => $desctoadd);
}
- webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
+ $paramtoadd = $descriptiontype;
+ } else {
+ $paramtoadd = 'multiple:'.$param;
+ }
+
+ webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
+
+ } else {
+ $desctoadd = webservice_lib::create_end_of_description($separator.$otherparam, $matches[2][$i]);
+ if(empty($descriptionpath)) {
+ if (empty($description[$method->getName()]) || !array_key_exists($descriptiontype, $description[$method->getName()])) {
+ $desctoadd = array($descriptiontype => $desctoadd);
+ }
+ $paramtoadd = $descriptiontype;
+
+ } else {
+ $paramtoadd = $param;
}
+ webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
}
}
}
}
}
-
return $description;
}
<?php
/**
* Created on 10/17/2008
- *
+ *
* Rest Test Client Suport Library
*
* @author David Castro Garcia
* starts the interface
*/
function start_interface ($title=" ") {
- $title2 = ($title)?$title:" ";
- echo '<html><head>';
- echo "<title>Moodle Webservice Rest Test Client</title>";
- echo '<link rel="stylesheet" href="style.css" type="text/css">';
- echo '</head><body>';
- echo '<div class="head"><h1>Moodle Webservice Rest Test Client</h1>';
- echo "<h3>$title2</h3>";
- echo '</div>';
- echo '<div class="content">';
- if ($title) echo '<p class="return"><a href="index.php"><img src="return.gif" border="0"/></a></p>';
+ $title2 = ($title)?$title:" ";
+ echo '<html><head>';
+ echo "<title>Moodle Webservice Rest Test Client</title>";
+ echo '<link rel="stylesheet" href="style.css" type="text/css">';
+ echo '</head><body>';
+ echo '<div class="head"><h1>Moodle Webservice Rest Test Client</h1>';
+ echo "<h3>$title2</h3>";
+ echo '</div>';
+ echo '<div class="content">';
+ if ($title) echo '<p class="return"><a href="index.php"><img src="return.gif" border="0"/></a></p>';
}
/**
* end interface
- *
+ *
* @param bool $ret=true: show return button
*/
function end_interface ($ret = true) {
- if ($ret) echo '<p class="return"><a href="index.php"><img src="return.gif" border="0"/></a></p>';
- echo '</div>';
- echo '<div class="footer">Created by David Castro i Ferran Recio for Moodle Webservices</div>';
- echo '</body></html>';
+ if ($ret) echo '<p class="return"><a href="index.php"><img src="return.gif" border="0"/></a></p>';
+ echo '</div>';
+ echo '<div class="footer">Created by David Castro i Ferran Recio for Moodle Webservices</div>';
+ echo '</body></html>';
}
/**
* print XML div area
- *
+ *
* @param string $xml
- *
+ *
*/
function show_xml ($xml) {
- echo '<div class="xmlshow">';
- echo '<a onClick="document.getElementById(\'toogleme\').style.display = ' .
- '(document.getElementById(\'toogleme\').style.display!=\'none\')?\'none\':\'\';">Hide/Show XML</a>';
- echo "<div style=\"display:none;\" id=\"toogleme\">";
- echo '<pre>';echo htmlentities($xml);echo '</pre>';
- echo "</div>";
- echo "</div>";
+ echo '<div class="xmlshow">';
+ echo '<a onClick="document.getElementById(\'toogleme\').style.display = ' .
+ '(document.getElementById(\'toogleme\').style.display!=\'none\')?\'none\':\'\';">Hide/Show XML</a>';
+ echo "<div style=\"display:none;\" id=\"toogleme\">";
+ echo '<pre>';echo htmlentities($xml);echo '</pre>';
+ echo "</div>";
+ echo "</div>";
}
/**
* format post data
*/
function format_postdata ($data) {
- $o="";
- foreach ($data as $k=>$v) {
- $o.= "$k=".rawurlencode($v)."&";
- }
- $post_data=substr($o,0,-1);
- return $post_data;
+ $o="";
+ foreach ($data as $k=>$v) {
+ $o.= "$k=".rawurlencode($v)."&";
+ }
+ $post_data=substr($o,0,-1);
+ return $post_data;
}
/**
* shows an object in list format
- *
+ *
* @param mixed $obj
* @param integer $cols: number of colums
* @ string string $check=false: if this attribute is not present, the $obj is ans error
- *
+ *
*/
function show_object ($obj,$cols=1,$check=false) {
- if (!is_array($obj)) $obj = array($obj);
- echo '<ul class="results">';
- foreach ($obj as $r) {
-
- if ($check && (!isset($r->$check) || $r->$check==-1)) {
- echo '<li class="error">';
- echo "EMPTY ROW!";
- } else {
- if (is_object($r)) {
- echo '<li class="element">';
- $text = array();
- $parts = get_object_vars($r);
- $num = 1;
- $currline = '';
- foreach ($parts as $key => $val) {
- $currline.= "<span class=\"resultval\"><b>$key:</b> <i>$val</i></span>, ";
- if ($num >= $cols) {
- $currline=substr($currline,0,-2);
- $text[] = $currline;
- $currline = '';
- $num = 0;
- }
- $num++;
- }
- echo implode('<br/>',$text);
- } else {
- if ($r==-1 || !$r) {
- echo '<li class="error">';
- echo "EMPTY ROW!";
- } else {
- echo '<li class="element">';
- echo "<span class=\"resultval\"><b>Returned Value:</b> <i>$r</i></span>";
- }
- }
- }
- echo '</li>';
- }
- echo '</ul>';
+ if (!is_array($obj)) $obj = array($obj);
+ echo '<ul class="results">';
+ foreach ($obj as $r) {
+
+ if ($check && (!isset($r->$check) || $r->$check==-1)) {
+ echo '<li class="error">';
+ echo "EMPTY ROW!";
+ } else {
+ if (is_object($r)) {
+ echo '<li class="element">';
+ $text = array();
+ $parts = get_object_vars($r);
+ $num = 1;
+ $currline = '';
+ foreach ($parts as $key => $val) {
+ $currline.= "<span class=\"resultval\"><b>$key:</b> <i>$val</i></span>, ";
+ if ($num >= $cols) {
+ $currline=substr($currline,0,-2);
+ $text[] = $currline;
+ $currline = '';
+ $num = 0;
+ }
+ $num++;
+ }
+ echo implode('<br/>',$text);
+ } else {
+ if ($r==-1 || !$r) {
+ echo '<li class="error">';
+ echo "EMPTY ROW!";
+ } else {
+ echo '<li class="element">';
+ echo "<span class=\"resultval\"><b>Returned Value:</b> <i>$r</i></span>";
+ }
+ }
+ }
+ echo '</li>';
+ }
+ echo '</ul>';
}
/**
* convert a simple xml into php object
- *
+ *
* @author ferran recio
- *
+ *
* @param String $xml
- *
+ *
* @return mixed
*/
function basicxml_xml_to_object ($xml) {
- $xml=utf8_encode($xml);
-
- //create the parser
- $parser = xml_parser_create ();
- xml_set_default_handler ($parser,'basicxml_xml_to_object_aux');
-
- $values = array();
- $index = array();
- xml_parse_into_struct($parser,$xml,$values,$index);
-
- //print_object($values);
- //print_object($index);
-
- //just simplexml tag (disabled)
- //if (strtolower($values[0]['tag']) != 'basicxml') return false;
- //if (strtolower($values[count($values)-1]['tag']) != 'basicxml') return false;
-
- $res = basicxml_xml_to_object_aux ($values);
- //omit the first tag
- $parts = array_keys(get_object_vars($res));
- $key = $parts[0];
- return $res->$key;
+ $xml=utf8_encode($xml);
+
+ //create the parser
+ $parser = xml_parser_create ();
+ xml_set_default_handler ($parser,'basicxml_xml_to_object_aux');
+
+ $values = array();
+ $index = array();
+ xml_parse_into_struct($parser,$xml,$values,$index);
+
+ //print_object($values);
+ //print_object($index);
+
+ //just simplexml tag (disabled)
+ //if (strtolower($values[0]['tag']) != 'basicxml') return false;
+ //if (strtolower($values[count($values)-1]['tag']) != 'basicxml') return false;
+
+ $res = basicxml_xml_to_object_aux ($values);
+ //omit the first tag
+ $parts = array_keys(get_object_vars($res));
+ $key = $parts[0];
+ return $res->$key;
}
/**
* auxiliar function to basicxml_xml_to_object
- *
+ *
* @author ferran recio
- *
+ *
* @param mixed $values
- *
+ *
* @return mixed
*/
function basicxml_xml_to_object_aux ($values) {
-
- if (!is_array($values)) return false;
- //print_object ($values);
- $currset = array();
- $search = false;
-
- foreach ($values as $value) {
- $tag = strtolower($value['tag']);
- //if we are acomulating, just acomulate it
- if ($search) {
- //if it closes a tag, we just stop searching
- if ($tag == $search && $value['type']=='close') {
- //recursivity
- $obj2 = basicxml_xml_to_object_aux ($currset);
- //search cleaning
- $search = false;
- //add to result
- if (isset($res->{$tag})){
- if (is_array($res->{$tag})){
- $res->{$tag}[] = $obj2;
- } else {
- $res->{$tag} = array($res->{$tag},$obj2);
- }
- } else {
- $res->{$tag} = $obj2;
- }
- } else {
- //we are searching. If it's cdada, pass it throw
- if ($value['type']=='cdata') continue;
- //if isn't cdata, put it in the set and continue searching
- //(because isn't the close we're searching)
- $currset[] = $value;
- }
- } else {
- //walking the xml
- if ($value['type']=='open'){
- //on open, let's search on it
- $currset = array();
- $search = $tag;
- } else {
- //if it's complete just save it
- if ($value['type']=='complete') {
- $val = html_entity_decode($value['value']);
- if (isset($res->{$tag})){
- if (is_array($res->{$tag})){
- $res->{$tag}[] = $val;
- } else {
- $res->{$tag} = array($res->{$tag},$val);
- }
- } else {
- $res->{$tag} = $val;
- }
- }
- }
- }
- }
- return $res;
+
+ if (!is_array($values)) return false;
+ //print_object ($values);
+ $currset = array();
+ $search = false;
+
+ foreach ($values as $value) {
+ $tag = strtolower($value['tag']);
+ //if we are acomulating, just acomulate it
+ if ($search) {
+ //if it closes a tag, we just stop searching
+ if ($tag == $search && $value['type']=='close') {
+ //recursivity
+ $obj2 = basicxml_xml_to_object_aux ($currset);
+ //search cleaning
+ $search = false;
+ //add to result
+ if (isset($res->{$tag})){
+ if (is_array($res->{$tag})){
+ $res->{$tag}[] = $obj2;
+ } else {
+ $res->{$tag} = array($res->{$tag},$obj2);
+ }
+ } else {
+ $res->{$tag} = $obj2;
+ }
+ } else {
+ //we are searching. If it's cdada, pass it throw
+ if ($value['type']=='cdata') continue;
+ //if isn't cdata, put it in the set and continue searching
+ //(because isn't the close we're searching)
+ $currset[] = $value;
+ }
+ } else {
+ //walking the xml
+ if ($value['type']=='open'){
+ //on open, let's search on it
+ $currset = array();
+ $search = $tag;
+ } else {
+ //if it's complete just save it
+ if ($value['type']=='complete') {
+ if (!empty($value['value'] )) {
+ $val = html_entity_decode($value['value']);
+ if (isset($res->{$tag})){
+ if (is_array($res->{$tag})){
+ $res->{$tag}[] = $val;
+ } else {
+ $res->{$tag} = array($res->{$tag},$val);
+ }
+ } else {
+ $res->{$tag} = $val;
+ }
+ }
+ }
+ }
+ }
+ }
+ return $res;
}
?>
\ No newline at end of file