$limit) { $v = substr($v, 0, $limit).$ellipsis; } return $v; } protected function formatValue($v) { if(is_numeric($v)) { return $v; } if(is_bool($v)) { return ''; } if(is_string($v) && strpos($v, 'http') !== false) { $url = $v; $v = $this->truncate($v, '...'); return "$v"; } return $this->truncate(print_r($v, true)); } protected function result($data) { // the format is array('result' => array('function name' => DATA)) // so take the first element of the 'result' array $func = key($data['result']); $data = reset($data['result']); $title = $func; $content = ''; if($func == 'NewSearch') { $content .= '

Count : '.$data['count'].'

'; $content .= '

Facets : '.print_r($data['facets'], true).'

'; unset($data['count']); unset($data['facets']); } $first = reset($data); $single = ! is_array($first); $content .= ''; if($single) { $content .= ""; } else { $columns = array_keys($first); $title .= ' ('.count($data).' results)'; $content .= ''; foreach($columns as $k) { $content .= ""; } $content .= ''; } $content .= ''; if($single) { foreach($data as $k => $v) { $content .= ""; } } else { foreach($data as $row) { $content .= ''; foreach($columns as $c) { $content .= ''; } $content .= ''; } } $content .= '
FieldValue
$k
$k".$this->formatValue($v)."
'.$this->formatValue(isset($row[$c]) ? $row[$c] : '').'
'; return array( 'title' => $title, '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); } }