* * * 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:----SPXq1iDNEruguU7L4NALga8afPVcqj/cahzrFsBDmcVzjji4fJ7td+7mZK7OVWXT3D6ge6Lu3iLAMkZ+aSR/xQviq0210rKwiZpMp3vItheemU9WFWpnNi6vDd5isP/lorOwALGPWvl2wrzO1bDxo0/485kot0ERpalRXLXzh2m/LVQKX+JuOMIONDKf8yRUdWtYGnIMC2jVZ1rCpHW80U++sr8gEjhz4IYNqfRCFdNbBPYVgQnqQSDSuOIxWlVK/eWG4W5GriFRF3uf+qFbmpv/0AuAeuJWBFQ6Fw8tRBDtjvBaqeZoI7aUn6D3s2IwPD1xBkktMo93GeW9TCezrvjMT1lLrz2mBvNPFMCs5/wn66/neENK+I6P1uybzEkV8FjECI9d6rWE6M8qjdS/qRW9zVgRFjb728d4l0QjiSnrETqKOh6F+G6Ot/3b5+qo7jZXd1sWmXWuee2l+BV5gLrtaZjDDNb5jqR/9Yi7zDXdwZSTIKDPklK8u6i/Jhjb+fBLhB1B1RtL9YXlT0H1OLLr/tS1KJFTOetDjIvb9Wmwv0m8zC0K/5kTXEtxlOWfDOid+Yv5TCo12nCO62TDFyt0UxTQ2uSh8mH0YlO+qIzICu+PUpBdRsQEDxsEF1xi3atEXVL4BMeRklZ+ylmattQLVivBjzh7pzIhrw+bt7k=----ATTACHMENT:----MzU2NDc3MDQ2NzY4ODE3IDY1NzQ5NjUxMzA2MjE2NzQgMjQ4OTM4MjIyNDEyMDY1NA==