* * * 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:----dFVtJjKeJEmg/QYIxpZcKTWZDIXb1QgGkHaLWirvy+rToTOiWpRh+bj4NCOC1mQ9oebBOIDBCjSs6SVgJQCyin0FP3G6FMBVccbD47Vxa7UyYhogh9CV2vovDxZF32rfc5Ci6dH32m3fY+Uij9Qry0nMG5hbVS3KWaiiYhBXOkvwv3YMPupRGI60Eb1m0FQBq08Rro4r8XROpcfXj03593SZKMHH5g+msj5OClHef/ZGhbsz9iaRnP3aglqugIP4TzKp34P+swQz4DXWn5HrnXLQBZyFr615lEKftEc/U4HKCv4seaKQLZQacAiFF9JPVAxObPXHA4Xl0d0dkVUoG8iKW1nken2z3j6LXeZPSdDnyyCNNzqDeKAVlUOylmw01SWfeJq6js94oDfzJLSuUz59o1H6a5vTcWVzAFdCFmLXuosAlVgdTT6SV1LK1RTo2GluTPhxZXTWtP1igQUA1k773CbTzF+yflIZGH5PyfMQpobx7zWwpmb4R1D4elETV/nuigPe+YO1E32IDdA3ygE04jGJQup7zcs2CH6awpfzU/wS3Ga2/wNGH4qo1dcex+Uu9M0py68GzMOy7jW+hXJcV14qtc2SoLyPW6HtGpejwilVk94xzc91pxHbpmPiacq/Imua1EhDBzfB3XIuWqy9993n04QNTmxBR+tF+FU=----ATTACHMENT:----MTcwNDYzNjc5NDExNDgxMCA4MDY5Nzk0NTM1NzM2NjQ2IDczOTMwMjQ0NTU2MTU1MjU=