PrettyTable::setHeaders()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace kalanis\kw_clipr\Output;
4
5
6
use kalanis\kw_clipr\Clipr\Useful;
7
8
9
/**
10
 * Class PrettyTable
11
 *
12
 * Lib which makes pretty tables (Markdown rulezz!)
13
 *
14
 * Usage (basically twice, once set, then output):
15
 * <pre>
16
 *   # set
17
 *   $libPrettyTable = new PrettyTable();
18
 *   $libPrettyTable->setHeaders(['tb1','tb2','tb3']);
19
 *   $libPrettyTable->setColors(['yellow','','blue']);
20
 *   $libPrettyTable->setDataLine(['abc','def','ghi']);
21
 *   $libPrettyTable->setDataLine(['rst','uvw','xyz']);
22
 *   # print
23
 *   cliprATask->outputLn($libPrettyTable->getHeader());
24
 *   cliprATask->outputLn($libPrettyTable->getSeparator());
25
 *   foreach ($libPrettyTable as $row) {
26
 *       cliprATask->outputLn($row);
27
 *   }
28
 * </pre>
29
 * @package kalanis\kw_clipr\Output
30
 */
31
class PrettyTable implements \Iterator
32
{
33
    /** @var array<int, string> */
34
    protected array $header = [];
35
    /** @var array<int, string> */
36
    protected array $colors = [];
37
    /** @var array<int, string> */
38
    protected array $colorsHeader = [];
39
    /** @var array<int, array<int, string>> */
40
    protected array $table = [];
41
    protected int $position = 0;
42
    /** @var array<int, int> */
43
    protected array $lengths = [];
44
45
    /**
46
     * @param array<int, string> $values
47
     */
48 9
    public function setColors($values): void
49
    {
50 9
        $this->colors = $values;
51 9
    }
52
53 1
    public function setColor(int $index, string $value): void
54
    {
55 1
        $this->colors[$index] = $value;
56 1
    }
57
58
    /**
59
     * @param array<int, string> $values
60
     */
61 2
    public function setColorsHeader($values): void
62
    {
63 2
        $this->colorsHeader = $values;
64 2
    }
65
66 1
    public function setColorHeader(int $index, string $value): void
67
    {
68 1
        $this->colorsHeader[$index] = $value;
69 1
    }
70
71
    /**
72
     * @param array<int, string> $values
73
     */
74 13
    public function setHeaders($values): void
75
    {
76 13
        $this->header = $values;
77 13
    }
78
79 1
    public function setHeader(int $index, string $value): void
80
    {
81 1
        $this->header[$index] = $value;
82 1
    }
83
84
    /**
85
     * @param array<int, string> $values
86
     */
87 8
    public function setDataLine($values): void
88
    {
89 8
        $this->table[$this->position] = $values;
90 8
        $this->next();
91 8
    }
92
93
    /**
94
     * @param int $index
95
     * @param string $value
96
     */
97 1
    public function setData(int $index, $value): void
98
    {
99 1
        $this->table[$this->position][$index] = $value;
100 1
    }
101
102 10
    public function setLengths(bool $force = false): void
103
    {
104 10
        if (empty($this->lengths) || $force) {
105
            // for correct padding it's necessary to set max lengths for each column
106 10
            $outputArray = array_merge([$this->header], $this->table);
107 10
            foreach ($outputArray as $row) {
108 10
                foreach ($row as $index => $item) {
109 10
                    $len = mb_strlen($item);
110 10
                    if (!isset($this->lengths[$index]) || ($this->lengths[$index] < $len)) {
111 10
                        $this->lengths[$index] = $len;
112
                    }
113
                }
114
            }
115
        }
116 10
    }
117
118
    #[\ReturnTypeWillChange]
119 8
    public function current()
120
    {
121 8
        $this->setLengths();
122 8
        return $this->dumpLine($this->table[$this->position], $this->colors);
123
    }
124
125 1
    public function prev(): void
126
    {
127 1
        $this->position--;
128 1
    }
129
130 8
    public function next(): void
131
    {
132 8
        $this->position++;
133 8
    }
134
135
    /**
136
     * @return int|mixed
137
     * @codeCoverageIgnore no access inside iterator
138
     */
139
    #[\ReturnTypeWillChange]
140
    public function key()
141
    {
142
        // @codeCoverageIgnoreStart
143
        return $this->position;
144
    }
145
    // @codeCoverageIgnoreEnd
146
147 10
    public function valid(): bool
148
    {
149 10
        return isset($this->table[$this->position]);
150
    }
151
152 10
    public function rewind(): void
153
    {
154 10
        $this->position = 0;
155 10
    }
156
157 10
    public function getHeader(): string
158
    {
159 10
        $this->setLengths();
160 10
        if (!empty($this->header)) {
161 10
            return $this->dumpLine($this->header, $this->colorsHeader + $this->colors);
162
        }
163 1
        return '';
164
    }
165
166 10
    public function getSeparator(): string
167
    {
168 10
        $this->setLengths();
169 10
        if (!empty($this->lengths)) {
170 10
            $line = array();
171 10
            foreach ($this->lengths as $index => $length) {
172 10
                $line[$index] = str_repeat('-', $length);
173
            }
174 10
            return $this->dumpLine($line, $this->colorsHeader + $this->colors);
175
        }
176 1
        return '';
177
    }
178
179
    /**
180
     * @param iterable<int, string> $content
181
     * @param array<int, string> $colors
182
     * @return string
183
     */
184 10
    protected function dumpLine(iterable $content, array $colors = []): string
185
    {
186 10
        $line = array();
187 10
        foreach ($content as $index => $item) {
188 10
            $padded = Useful::mb_str_pad($item, $this->lengths[$index]);
189 10
            if (empty($colors[$index])) {
190 8
                $line[] = $padded;
191
            } else {
192 6
                $color = $colors[$index];
193 6
                $line[] = '<' . $color . '>' . $padded . '</' . $color . '>';
194
            }
195
        }
196 10
        return '| ' . implode(' | ', $line) . ' |';
197
    }
198
}
199