$attributes
* @return string Value nicely formatted/colored
*/
public function ok($content, $ok = false, array $attributes = []) {
if ($ok) {
$type = 'yes';
$color = 'green';
} else {
$type = 'no';
$color = 'red';
}
$options = [
'type' => $type,
'color' => $color,
];
$options['content'] = $content;
$options['attributes'] = $this->template->formatAttributes($attributes);
return $this->template->format('ok', $options);
}
/**
* Prepared string for output inside `...
`.
*
* @param string $text
* @param array $options
*
* @return string
*/
public function pre(string $text, array $options = []): string {
$options += [
'escape' => true,
'space' => 4,
];
if ($options['escape']) {
$text = h($text);
}
$text = str_replace("\t", str_repeat(' ', $options['space']), $text);
return $text;
}
/**
* Useful for displaying tabbed (code) content when the default of 8 spaces
* inside is too much. This converts it to spaces for better output.
*
* Inspired by the tab2space function found at:
*
* @see http://aidan.dotgeek.org/lib/?file=function.tab2space.php
* @param string $text
* @param int $spaces
* @return string
*/
public function tab2space($text, $spaces = 4) {
$spacesString = str_repeat(' ', $spaces);
$splitText = preg_split("/\r\n|\r|\n/", trim($text));
if ($splitText === false) {
return $text;
}
$wordLengths = [];
$wArray = [];
// Store word lengths
foreach ($splitText as $line) {
$words = preg_split("/(\t+)/", $line, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach (array_keys($words) as $i) {
$strlen = strlen($words[$i]);
$add = isset($wordLengths[$i]) && ($wordLengths[$i] < $strlen);
if ($add || !isset($wordLengths[$i])) {
$wordLengths[$i] = $strlen;
}
}
$wArray[] = $words;
}
$text = '';
// Apply padding when appropriate and rebuild the string
foreach (array_keys($wArray) as $i) {
foreach (array_keys($wArray[$i]) as $ii) {
if (preg_match("/^\t+$/", $wArray[$i][$ii])) {
$wArray[$i][$ii] = str_pad($wArray[$i][$ii], $wordLengths[$ii], "\t");
} else {
$wArray[$i][$ii] = str_pad($wArray[$i][$ii], $wordLengths[$ii]);
}
}
$text .= str_replace("\t", $spacesString, implode('', $wArray[$i])) . "\n";
}
return $text;
}
/**
* Translate a result array into a HTML table
*
* @todo Move to Text Helper etc.
*
* Options:
* - recursive: Recursively generate tables for multi-dimensional arrays
* - heading: Display the first as heading row (th)
* - escape: Defaults to true
* - null: Null value
*
* @author Aidan Lister
* @version 1.3.2
* @link http://aidanlister.com/2004/04/converting-arrays-to-human-readable-tables/
* @param array $array The result (numericaly keyed, associative inner) array.
* @param array $options
* @param array $attributes For the table
* @return string
*/
public function array2table(array $array, array $options = [], array $attributes = []) {
$defaults = [
'null' => ' ',
'recursive' => false,
'heading' => true,
'escape' => true,
];
$options += $defaults;
// Sanity check
if (!$array) {
return '';
}
if (!isset($array[0]) || !is_array($array[0])) {
$array = [$array];
}
$attributes += [
'class' => 'table',
];
$attributes = $this->template->formatAttributes($attributes);
// Start the table
$table = "\n";
if ($options['heading']) {
// The header
$table .= "\t";
// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
$table .= '| ' . ($options['escape'] ? h($heading) : $heading) . ' | ';
}
$table .= "
\n";
}
// The body
foreach ($array as $row) {
$table .= "\t";
foreach ($row as $cell) {
$table .= '| ';
// Cast objects
if (is_object($cell)) {
$cell = (array)$cell;
}
if ($options['recursive'] && is_array($cell) && !empty($cell)) {
// Recursive mode
$table .= "\n" . static::array2table($cell, $options) . "\n";
} else {
$table .= (!is_array($cell) && strlen($cell) > 0) ? ($options['escape'] ? h(
$cell,
) : $cell) : $options['null'];
}
$table .= ' | ';
}
$table .= "
\n";
}
$table .= '
';
return $table;
}
/**
* @param string $string
*
* @throws \RuntimeException
* @return string
*/
public function slug($string) {
if ($this->_config['slugger']) {
$callable = $this->_config['slugger'];
if (!is_callable($callable)) {
throw new RuntimeException('Invalid callable passed as slugger.');
}
return $callable($string);
}
return ShimInflector::slug($string);
}
}