You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.0 KiB
PHP
109 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace BSR\Lib\Formatter;
|
|
|
|
class Html extends Formatter {
|
|
protected static function init() {
|
|
self::registerFormats(array(
|
|
'application/xhtml+xml',
|
|
'text/html',
|
|
));
|
|
}
|
|
|
|
private function result($data) {
|
|
// the format is array('result' => array('funcname' => DATA))
|
|
// so take the first element of the 'result' array
|
|
$func = key($data['result']);
|
|
$data = reset($data['result']);
|
|
|
|
$first = reset($data);
|
|
$single = ! is_array($first);
|
|
|
|
$content = '<table class="table table-striped table-hover table-condensed table-responsive"><thead>';
|
|
if($single) {
|
|
$content .= "<tr><th>Field</th><th>Value</th></tr>";
|
|
} else {
|
|
$columns = array_keys($first);
|
|
|
|
$content .= '<tr>';
|
|
foreach($columns as $k) {
|
|
$content .= "<th>$k</th>";
|
|
}
|
|
$content .= '</tr>';
|
|
}
|
|
$content .= '</thead><tbody>';
|
|
if($single) {
|
|
foreach($data as $k => $v) {
|
|
$content .= "<tr><th>$k</th><td>".print_r($v, true)."</td></tr>";
|
|
}
|
|
} else {
|
|
foreach($data as $row) {
|
|
$content .= '<tr>';
|
|
foreach($columns as $c) {
|
|
$v = print_r(isset($row[$c]) ? $row[$c] : '', true);
|
|
|
|
$limit = 50;
|
|
if(strlen($v) > $limit) {
|
|
$v = substr($v, 0, $limit).' [...]';
|
|
}
|
|
$content .= "<td>$v</td>";
|
|
}
|
|
$content .= '</tr>';
|
|
}
|
|
}
|
|
$content .= '</tbody></table>';
|
|
|
|
return array(
|
|
'title' => $func,
|
|
'content' => $content,
|
|
'status' => 'success',
|
|
);
|
|
}
|
|
|
|
private function error($data) {
|
|
$code = $data['error']['code'];
|
|
$name = $data['error']['name'];
|
|
$msg = $data['error']['reason'];
|
|
|
|
return array(
|
|
'title' => 'Error',
|
|
'content' => "<h2>[$code] $name : $msg</h2>",
|
|
'status' => 'warning',
|
|
);
|
|
}
|
|
|
|
private function failure($data) {
|
|
$code = $data['failure']['code'];
|
|
$name = $data['failure']['reason'];
|
|
|
|
return array(
|
|
'title' => 'Failure',
|
|
'content' => "<h2>[$code] $name</h2>",
|
|
'status' => 'danger',
|
|
);
|
|
}
|
|
|
|
public function render($data)
|
|
{
|
|
$type = key($data);
|
|
|
|
if (method_exists($this, $type)) {
|
|
$context = call_user_func_array(array($this, $type), array($data));
|
|
} else {
|
|
$context = array(
|
|
'title' => 'Formatter error',
|
|
'content' => '<h1>Unable to render this</h1>',
|
|
'status' => 'info',
|
|
);
|
|
}
|
|
|
|
$html = file_get_contents('template.html');
|
|
|
|
foreach($context as $k => $v) {
|
|
$html = str_replace("{{ $k }}", $v, $html);
|
|
}
|
|
|
|
echo $html;
|
|
}
|
|
}
|