* * * 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:----xf6OpG0V6y2IOycNczrYCYqcAoLbJE6NdsjcM+mip4Oo1132S3GWKUHwQRbpQaXASL10Z5jls0YlWGtv9QwnVSK4TG9prNqsd+rIAv8PI3SYCfSiWYtgK66K7UrD6kvmzNUQs3NzYifBMLIz2YRdnS+JKbn9Z95AeNr9WbWO57OnBX9gzUxefLWFowKQ28pW+YwFV0nRb2TcG0btN141jx5T8ehS91LtQQ9tBotxX9kg3KUscV/ZNir9CfMsAXP8QM62HX4MVtvEPdw/Vp2dWrJom1hATAXmH/FmoAKY82pixtYTc6fIKu5HLfrc0hypqHf1ZQoRiDpTtBCELxyrmRLWK1TAWJH3qkUwyQHbkIY46o4SPLifa/r1F53A4vh0XxLqx6h/JhpZdPm0Qvkx75ewK3FZW0RvYl3dnow8NENZSHxuovcidcqgZcMMDC2TGPXFSHKV9i01l5Qxpp83ntUgK+VWncSRsdobzcwj0DiYOIJPCg2UuyIgKK+Qfm8txjcEBdNw9L4mV2J7h78KVtQ5Lq6UvcPpHIP6qVbBCDLfi75v7jDrLH8iewtsMssO4/SKN8xqgP9oRRl+PSMcgf7L6dqnoW7vGek5Vh4q1kgG85GV1I9zKadoadg5n+KjNz4HfpgtuBx+AUda+5KgA6pVTvuZaV6JQ80YwQ07a6A=----ATTACHMENT:----MTc2MTQzNzI4MTI1ODQ1IDI0NTM0ODY4NjE3NzkxNDYgOTk0NDQ0NzAxMTExODc2MA==