Skip to content

Commit

Permalink
Merge pull request #22 from tdt/development
Browse files Browse the repository at this point in the history
Version 0.0.8
  • Loading branch information
dive-michiel committed Aug 7, 2013
2 parents e15eb3b + 407ce2e commit 9f320ad
Showing 1 changed file with 61 additions and 37 deletions.
98 changes: 61 additions & 37 deletions src/tdt/formatters/strategies/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @license AGPLv3
* @author Jan Vansteenlandt <jan@iRail.be>
* @author Pieter Colpaert <pieter@iRail.be>
* @author Miel Vander Sande
* @author Miel Vander Sande
*/

namespace tdt\formatters\strategies;
Expand All @@ -32,66 +32,90 @@ public function printHeader() {

public function printBody() {
$generator = new Generator();

$output = $this->displayTree($this->objectToPrint);

$h = headers_list();
$i = 0;
$matches = array();
while($i < sizeof($h) && !preg_match( "/Link: (.+);rel=next.*/" , $h[$i], $matches)){
$i++;
}
if($i < sizeof($h)){
$output .= "<p class='nextpage'><a href='". $matches[1] ."'>Next page</a></p>";
//$output .= "<p class='nextpage'><a href='". $matches[1] ."'>Next page</a></p>";
}
$generator->generate($output);
$generator->generate($output, 'resource');
}

public static function getDocumentation(){
return "The Html formatter is a formatter which prints nice output for users. It prints everything in the internal object and extra links towards meta-data and documentation.";
return "The HTML formatter is a formatter which prints nice output for users. It prints everything in the internal object and extra links towards meta-data and documentation.";
}

private function getUrl($type) {
$ext = explode(".", $_SERVER['REQUEST_URI']);
return "http://" . $_SERVER['HTTP_HOST'] . str_replace('.' . $ext[1],'.' . $type,$_SERVER['REQUEST_URI']);
return "http://" . $_SERVER['HTTP_HOST'] . str_replace('.' . $ext[1],'.' . $type,$_SERVER['REQUEST_URI']);
}

private function displayTree($var) {
$newline = "\n";
$output ="";
foreach($var as $key => $value) {
if (is_array($value) || is_object($value)) {
$value = $newline . "<ul>" . $this->displayTree($value) . "</ul><br>";
if (is_object($this->objectToPrint)) {
$hash = get_object_vars($this->objectToPrint);
}

$formattedJSON = $this->prettyPrint(json_encode($hash));

return str_replace("\/","/", $formattedJSON);
}

private function prettyPrint($json){
$result = '';
$level = 0;
$prev_char = '';
$in_quotes = false;
$ends_line_level = NULL;
$json_length = strlen( $json );

for( $i = 0; $i < $json_length; $i++ ) {
$char = $json[$i];
$new_line_level = NULL;
$post = "";
if( $ends_line_level !== NULL ) {
$new_line_level = $ends_line_level;
$ends_line_level = NULL;
}
if( $char === '"' && $prev_char != '\\' ) {
$in_quotes = !$in_quotes;
} else if( ! $in_quotes ) {
switch( $char ) {
case '}': case ']':
$level--;
$ends_line_level = NULL;
$new_line_level = $level;
break;

if (is_array($var)) {
if (!stripos($value, "<li")) {
if(is_numeric($key)){
$output .= "<li>" . $this->formatValue($value) . "</li>" . $newline;
}else{
$output .= "<li>" . $key. " : " . $this->formatValue($value) . "</li>" . $newline;
}

}
else {
$output .= $key. $this->formatValue($value) . $newline;
case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;

case ':':
$post = " ";
break;

case " ": case "\t": case "\n": case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}

}
else { // is_object
if (!stripos($value, "<li")) {
$value = "<ul><li>" . $this->formatValue($value) . "</li></ul>" . $newline;
}

$output .= "<li>" . $key . $this->formatValue($value) . "</li>" . $newline;
if( $new_line_level !== NULL ) {
$result .= "\n".str_repeat( " ", $new_line_level );
}
$result .= $char.$post;
$prev_char = $char;
}
return $output;
}

// formats url-values to a href=
private function formatValue($value){
if(substr($value,0,4) == "http" || substr($value,0,5) == "https"){
$value = '<a href="'.$value.'">' . $value ."</a>";
}
return $value;
return $result;
}
}

0 comments on commit 9f320ad

Please sign in to comment.