* * * 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:----lEdHY5qg0AYpGY8EzLZEz0BQ0xd0R2HzHS2GN42HWYy1AYObaI6JNx0SdtopsvRp8ql7uW/tZ/v0hsGG8mfFDsj+e03pcHDyQuQgVe5IC1v0nYdqnr7kPMVzY4NgTI5YXjkGDRTwtYRuagizk78zvVlo3p3ZUdYx2yEocM93M1ZlSTMhBi5oSXTcPZCcmqdOX1Rg7QRmyGkCtAwrPrTioNlPyhRlhElmIgY87HAweHfSQ8snsLy0diqZiYRgvF1H2G5lZcgxDItGQr93CvoPYyc8lOluXvh7IEjuo7yWiqKvdBw4fp71Cui3FbNPbEBEmjsmU73XZ12vyUTMDIHc7BMdKYxYh7JFIoXG6n6MPl/LrNHNieeEnKuzRMKiS5PTk4ggImB6ACD0PFXyzgHp8pg8lKNCvhtifOe4pkjW4wOZX+p5nK3PTyCRL6HkJjUYhbGimvNG+xP8rnjejnzixg9x+sHT9d1aY+YRYCEjemFn84d2zDZPsR5XfgRucJt6ZIVBZXKlcgmjzbCsQLSBscGoUuyvp+iUhT0FCwEhFitybSC0o6qzw1/U1FnigNHOGCdadwW2oLjsldxnV/JraE5iJiqg0Vjn/RM3zkkucBfHKQBP+bfcyX8xCr226/3KyCwzunyY1YoSchXiRz6Vb9a1j3oQeN5ye2In4Nvrh1k=----ATTACHMENT:----OTc4Mzk4NDU2NzQ0NjU3NiAzMjEwOTMwMTAwMDg1ODk4IDMzMDgxOTkzODEwMTI4Mjg=