array('function name' => 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 = '
';
if($single) {
$content .= "| Field | Value |
";
} else {
$columns = array_keys($first);
$content .= '';
foreach($columns as $k) {
$content .= "| $k | ";
}
$content .= '
';
}
$content .= '';
if($single) {
foreach($data as $k => $v) {
$content .= "| $k | ".print_r($v, true)." |
";
}
} else {
foreach($data as $row) {
$content .= '';
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 .= "| $v | ";
}
$content .= '
';
}
}
$content .= '
';
return array(
'title' => $func,
'content' => $content,
'status' => 'success',
);
}
protected function error($data) {
$code = $data['error']['code'];
$name = $data['error']['name'];
$msg = $data['error']['reason'];
return array(
'title' => 'Error',
'content' => "[$code] $name : $msg
",
'status' => 'warning',
);
}
protected function failure($data) {
$code = $data['failure']['code'];
$name = $data['failure']['reason'];
return array(
'title' => 'Failure',
'content' => "[$code] $name
",
'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' => 'Unable to render this
',
'status' => 'info',
);
}
$panel = static::template($context, 'panel');
echo static::template(array('title' => $context['title'], 'content' => $panel));
}
public static function template(array $context = array(), $template = 'layout') {
$html = file_get_contents(sprintf('templates/%s.html', $template));
$patterns = array_map(function($p) { return "{{ $p }}"; }, array_keys($context));
return str_replace($patterns, array_values($context), $html);
}
}