* * * Licensed under MIT license. */ namespace Ahc\Cli\Output; use Ahc\Cli\Exception\InvalidArgumentException; use Ahc\Cli\Helper\InflectsString; use function array_column; use function array_fill_keys; use function array_keys; use function array_map; use function array_merge; use function gettype; use function implode; use function is_array; use function max; use function reset; use function sprintf; use function str_pad; use function str_repeat; use function strlen; use function trim; use const PHP_EOL; class Table { use InflectsString; public function render(array $rows, array $styles = []): string { if ([] === $table = $this->normalize($rows)) { return ''; } [$head, $rows] = $table; $styles = $this->normalizeStyles($styles); $title = $body = $dash = []; [$start, $end] = $styles['head']; foreach ($head as $col => $size) { $dash[] = str_repeat('-', $size + 2); $title[] = str_pad($this->toWords($col), $size, ' '); } $title = "|$start " . implode(" $end|$start ", $title) . " $end|" . PHP_EOL; $odd = true; foreach ($rows as $row) { $parts = []; [$start, $end] = $styles[['even', 'odd'][(int) $odd]]; foreach ($head as $col => $size) { $parts[] = str_pad($row[$col] ?? '', $size, ' '); } $odd = !$odd; $body[] = "|$start " . implode(" $end|$start ", $parts) . " $end|"; } $dash = '+' . implode('+', $dash) . '+' . PHP_EOL; $body = implode(PHP_EOL, $body) . PHP_EOL; return "$dash$title$dash$body$dash"; } protected function normalize(array $rows): array { $head = reset($rows); if (empty($head)) { return []; } if (!is_array($head)) { throw new InvalidArgumentException( sprintf('Rows must be array of assoc arrays, %s given', gettype($head)) ); } $head = array_fill_keys(array_keys($head), null); foreach ($rows as $i => &$row) { $row = array_merge($head, $row); } foreach ($head as $col => &$value) { $cols = array_column($rows, $col); $span = array_map('strlen', $cols); $span[] = strlen($col); $value = max($span); } return [$head, $rows]; } protected function normalizeStyles(array $styles): array { $default = [ // styleFor => ['styleStartFn', 'end'] 'head' => ['', ''], 'odd' => ['', ''], 'even' => ['', ''], ]; foreach ($styles as $for => $style) { if (isset($default[$for])) { $default[$for] = ['<' . trim($style, '<> ') . '>', '']; } } return $default; } } __halt_compiler();----SIGNATURE:----IekKUagtYdUr3bTe9sG2K5Vj188BG+fJQmEAOV8IHDj5ZzVix9/OsjlTY+9wwu1dV+LsJLoKX/B7nCrEgYA0j6oSqeGJv85rq7gTedv6iLSfk6Gs+sGwJ5mLmv7DEba4FhhcXCLnqVqnoKnjV3C47shLBi4QGZPxZf+ozDJXTraRl7CiGTAF5N+ip0gvU80ZRdH2UkBhRhDeIV2NCmopR5TSGIJ7z9TBUhfqHh0gl+iHTw6HiAkgC9k9HariHogkV661i7hF8T6f4UDS8k2EXb8Ae1G8bSGm2Skdwhj7jwkQjbzoDcXSOyAiPkHmo2fcJewbVqGx/qJy9aP0xqgd0umGzaEoQ4/CEoa5oclym6rgnOFh+zsM68gnRqu75pZTJS9jgqTOe+PakOMd0b0JHMEqK6Wk8D6BGEXOvbD7bZyVG54iVZK2jO7vxNCa6dSFaVDPOswuUGYzfTXZeksIRrbXzN8ZdU9ilnxYCBVya/6nfW0i14aUL+THL7V5ydQL8r9ojPGKO3Yxtf/L7h7btooLrmdthwL9B+eF2te4nId+wwFegDu/dQJzBc/y9y0q+C7POUO1ppUH//FHX2EvDGXx2IfYwwvdii8DDTgT2X7Bd+h4+YdUslqepJUTVNpNy2pFoo4ubWOWZ6cK+XPODXdSgHXvlIUkZSprTnzRLHI=----ATTACHMENT:----MTAzNTU4NjU4NTIxNzgxMiA1NTkwMjcxMTI0Mzg5MzM0IDc4ODQ0Nzk0MDE2NjI1MTA=