Skip to content

Commit 9f320ad

Browse files
committed
Merge pull request #22 from tdt/development
Version 0.0.8
2 parents e15eb3b + 407ce2e commit 9f320ad

File tree

1 file changed

+61
-37
lines changed

1 file changed

+61
-37
lines changed

src/tdt/formatters/strategies/HTML.php

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @license AGPLv3
88
* @author Jan Vansteenlandt <[email protected]>
99
* @author Pieter Colpaert <[email protected]>
10-
* @author Miel Vander Sande
10+
* @author Miel Vander Sande
1111
*/
1212

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

3333
public function printBody() {
3434
$generator = new Generator();
35+
3536
$output = $this->displayTree($this->objectToPrint);
37+
3638
$h = headers_list();
3739
$i = 0;
3840
$matches = array();
3941
while($i < sizeof($h) && !preg_match( "/Link: (.+);rel=next.*/" , $h[$i], $matches)){
4042
$i++;
4143
}
4244
if($i < sizeof($h)){
43-
$output .= "<p class='nextpage'><a href='". $matches[1] ."'>Next page</a></p>";
45+
//$output .= "<p class='nextpage'><a href='". $matches[1] ."'>Next page</a></p>";
4446
}
45-
$generator->generate($output);
47+
$generator->generate($output, 'resource');
4648
}
47-
49+
4850
public static function getDocumentation(){
49-
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.";
51+
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.";
5052
}
5153

5254
private function getUrl($type) {
5355
$ext = explode(".", $_SERVER['REQUEST_URI']);
54-
return "http://" . $_SERVER['HTTP_HOST'] . str_replace('.' . $ext[1],'.' . $type,$_SERVER['REQUEST_URI']);
56+
return "http://" . $_SERVER['HTTP_HOST'] . str_replace('.' . $ext[1],'.' . $type,$_SERVER['REQUEST_URI']);
5557
}
5658

5759
private function displayTree($var) {
58-
$newline = "\n";
59-
$output ="";
60-
foreach($var as $key => $value) {
61-
if (is_array($value) || is_object($value)) {
62-
$value = $newline . "<ul>" . $this->displayTree($value) . "</ul><br>";
60+
if (is_object($this->objectToPrint)) {
61+
$hash = get_object_vars($this->objectToPrint);
62+
}
63+
64+
$formattedJSON = $this->prettyPrint(json_encode($hash));
65+
66+
return str_replace("\/","/", $formattedJSON);
67+
}
68+
69+
private function prettyPrint($json){
70+
$result = '';
71+
$level = 0;
72+
$prev_char = '';
73+
$in_quotes = false;
74+
$ends_line_level = NULL;
75+
$json_length = strlen( $json );
76+
77+
for( $i = 0; $i < $json_length; $i++ ) {
78+
$char = $json[$i];
79+
$new_line_level = NULL;
80+
$post = "";
81+
if( $ends_line_level !== NULL ) {
82+
$new_line_level = $ends_line_level;
83+
$ends_line_level = NULL;
6384
}
85+
if( $char === '"' && $prev_char != '\\' ) {
86+
$in_quotes = !$in_quotes;
87+
} else if( ! $in_quotes ) {
88+
switch( $char ) {
89+
case '}': case ']':
90+
$level--;
91+
$ends_line_level = NULL;
92+
$new_line_level = $level;
93+
break;
6494

65-
if (is_array($var)) {
66-
if (!stripos($value, "<li")) {
67-
if(is_numeric($key)){
68-
$output .= "<li>" . $this->formatValue($value) . "</li>" . $newline;
69-
}else{
70-
$output .= "<li>" . $key. " : " . $this->formatValue($value) . "</li>" . $newline;
71-
}
72-
73-
}
74-
else {
75-
$output .= $key. $this->formatValue($value) . $newline;
95+
case '{': case '[':
96+
$level++;
97+
case ',':
98+
$ends_line_level = $level;
99+
break;
100+
101+
case ':':
102+
$post = " ";
103+
break;
104+
105+
case " ": case "\t": case "\n": case "\r":
106+
$char = "";
107+
$ends_line_level = $new_line_level;
108+
$new_line_level = NULL;
109+
break;
76110
}
77-
78111
}
79-
else { // is_object
80-
if (!stripos($value, "<li")) {
81-
$value = "<ul><li>" . $this->formatValue($value) . "</li></ul>" . $newline;
82-
}
83-
84-
$output .= "<li>" . $key . $this->formatValue($value) . "</li>" . $newline;
112+
if( $new_line_level !== NULL ) {
113+
$result .= "\n".str_repeat( " ", $new_line_level );
85114
}
115+
$result .= $char.$post;
116+
$prev_char = $char;
86117
}
87-
return $output;
88-
}
89118

90-
// formats url-values to a href=
91-
private function formatValue($value){
92-
if(substr($value,0,4) == "http" || substr($value,0,5) == "https"){
93-
$value = '<a href="'.$value.'">' . $value ."</a>";
94-
}
95-
return $value;
119+
return $result;
96120
}
97121
}

0 commit comments

Comments
 (0)