/** * Generates a table. * * @param array $table * @return string */ public static function generateTable(array $table) { $columnSize = array(); foreach ($table as $row) { $i = 0; foreach ($row as $column) { if (!isset($columnSize[$i])) { $columnSize[$i] = 0; } $columnSize[$i] = max($columnSize[$i], mb_strlen(AnsiUtil::stripAnsi($column))); $i++; } } $result = ''; $result .= '+'; foreach ($columnSize as $column) { $result .= str_repeat('-', $column + 2) . '+'; } $result .= PHP_EOL; foreach ($table as $row) { $result .= "|"; $i = 0; foreach ($row as $column) { $paddedString = StringUtil::pad(AnsiUtil::stripAnsi($column), $columnSize[$i], ' ', is_numeric($column) ? STR_PAD_LEFT : STR_PAD_RIGHT); $result .= ' ' . str_replace(AnsiUtil::stripAnsi($column), $column, $paddedString) . ' |'; $i++; } $result .= PHP_EOL . "+"; foreach ($columnSize as $column) { $result .= str_repeat('-', $column + 2) . '+'; } $result .= PHP_EOL; } return $result; }