Passed
Pull Request — master (#50)
by Jitendra
01:44
created

Table::normalizeStyles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 16
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Ahc\Cli\Output;
4
5
use Ahc\Cli\Exception\InvalidArgumentException;
6
use Ahc\Cli\Helper\InflectsString;
7
8
class Table
9
{
10
    use InflectsString;
11
12
    public function render(array $rows, array $styles = []): string
13
    {
14
        if ([] === $table = $this->normalize($rows)) {
15
            return '';
16
        }
17
18
        list($head, $rows) = $table;
19
20
        $styles = $this->normalizeStyles($styles);
21
        $title  = $body = $dash = [];
22
23
        list($start, $end) = $styles['head'];
24
        foreach ($head as $col => $size) {
25
            $dash[]  = \str_repeat('-', $size + 2);
26
            $title[] = \str_pad($this->toWords($col), $size, ' ');
27
        }
28
29
        $title = "|$start " . \implode(" $end|$start ", $title) . " $end|" . \PHP_EOL;
30
31
        $odd = true;
32
        foreach ($rows as $row) {
33
            $parts = [];
34
35
            list($start, $end) = $styles[['even', 'odd'][(int) $odd]];
36
            foreach ($head as $col => $size) {
37
                $parts[] = \str_pad($row[$col] ?? '', $size, ' ');
38
            }
39
40
            $odd    = !$odd;
0 ignored issues
show
introduced by
The condition $odd is always true.
Loading history...
41
            $body[] = "|$start " . \implode(" $end|$start ", $parts) . " $end|";
42
        }
43
44
        $dash  = '+' . \implode('+', $dash) . '+' . \PHP_EOL;
45
        $body  = \implode(\PHP_EOL, $body) . \PHP_EOL;
46
47
        return "$dash$title$dash$body$dash";
48
    }
49
50
    protected function normalize(array $rows): array
51
    {
52
        $head = \reset($rows);
53
        if (empty($head)) {
54
            return [];
55
        }
56
57
        if (!\is_array($head)) {
58
            throw new InvalidArgumentException(
59
                \sprintf('Rows must be array of assoc arrays, %s given', \gettype($head))
60
            );
61
        }
62
63
        $head = \array_fill_keys(\array_keys($head), null);
64
        foreach ($rows as $i => &$row) {
65
            $row = \array_merge($head, $row);
66
        }
67
68
        foreach ($head as $col => &$value) {
69
            $cols   = \array_column($rows, $col);
70
            $span   = \array_map('strlen', $cols);
71
            $span[] = \strlen($col);
72
            $value  = \max($span);
73
        }
74
75
        return [$head, $rows];
76
    }
77
78
    protected function normalizeStyles(array $styles)
79
    {
80
        $default = [
81
            // styleFor => ['styleStartFn', 'end']
82
            'head' => ['', ''],
83
            'odd'  => ['', ''],
84
            'even' => ['', ''],
85
        ];
86
87
        foreach ($styles as $for => $style) {
88
            if (isset($default[$for])) {
89
                $default[$for] = ['<' . \trim($style, '<> ') . '>', '</end>'];
90
            }
91
        }
92
93
        return $default;
94
    }
95
}
96