1 | <?php |
||
49 | class Table extends Widget |
||
50 | { |
||
51 | const DEFAULT_CONSOLE_SCREEN_WIDTH = 120; |
||
52 | const CONSOLE_SCROLLBAR_OFFSET = 3; |
||
53 | const CHAR_TOP = 'top'; |
||
54 | const CHAR_TOP_MID = 'top-mid'; |
||
55 | const CHAR_TOP_LEFT = 'top-left'; |
||
56 | const CHAR_TOP_RIGHT = 'top-right'; |
||
57 | const CHAR_BOTTOM = 'bottom'; |
||
58 | const CHAR_BOTTOM_MID = 'bottom-mid'; |
||
59 | const CHAR_BOTTOM_LEFT = 'bottom-left'; |
||
60 | const CHAR_BOTTOM_RIGHT = 'bottom-right'; |
||
61 | const CHAR_LEFT = 'left'; |
||
62 | const CHAR_LEFT_MID = 'left-mid'; |
||
63 | const CHAR_MID = 'mid'; |
||
64 | const CHAR_MID_MID = 'mid-mid'; |
||
65 | const CHAR_RIGHT = 'right'; |
||
66 | const CHAR_RIGHT_MID = 'right-mid'; |
||
67 | const CHAR_MIDDLE = 'middle'; |
||
68 | |||
69 | /** |
||
70 | * @var array table headers |
||
71 | */ |
||
72 | private $_headers = []; |
||
73 | /** |
||
74 | * @var array table rows |
||
75 | */ |
||
76 | private $_rows = []; |
||
77 | /** |
||
78 | * @var array table chars |
||
79 | */ |
||
80 | private $_chars = [ |
||
81 | self::CHAR_TOP => '═', |
||
82 | self::CHAR_TOP_MID => '╤', |
||
83 | self::CHAR_TOP_LEFT => '╔', |
||
84 | self::CHAR_TOP_RIGHT => '╗', |
||
85 | self::CHAR_BOTTOM => '═', |
||
86 | self::CHAR_BOTTOM_MID => '╧', |
||
87 | self::CHAR_BOTTOM_LEFT => '╚', |
||
88 | self::CHAR_BOTTOM_RIGHT => '╝', |
||
89 | self::CHAR_LEFT => '║', |
||
90 | self::CHAR_LEFT_MID => '╟', |
||
91 | self::CHAR_MID => '─', |
||
92 | self::CHAR_MID_MID => '┼', |
||
93 | self::CHAR_RIGHT => '║', |
||
94 | self::CHAR_RIGHT_MID => '╢', |
||
95 | self::CHAR_MIDDLE => '│', |
||
96 | ]; |
||
97 | /** |
||
98 | * @var array table column widths |
||
99 | */ |
||
100 | private $_columnWidths = []; |
||
101 | /** |
||
102 | * @var int screen width |
||
103 | */ |
||
104 | private $_screenWidth; |
||
105 | /** |
||
106 | * @var string list prefix |
||
107 | */ |
||
108 | private $_listPrefix = '• '; |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Set table headers. |
||
113 | * |
||
114 | * @param array $headers table headers |
||
115 | * @return $this |
||
116 | */ |
||
117 | 9 | public function setHeaders(array $headers) |
|
118 | { |
||
119 | 9 | $this->_headers = array_values($headers); |
|
120 | 9 | return $this; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Set table rows. |
||
125 | * |
||
126 | * @param array $rows table rows |
||
127 | * @return $this |
||
128 | */ |
||
129 | 9 | public function setRows(array $rows) |
|
130 | { |
||
131 | 9 | $this->_rows = array_map('array_values', $rows); |
|
132 | 9 | return $this; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Set table chars. |
||
137 | * |
||
138 | * @param array $chars table chars |
||
139 | * @return $this |
||
140 | */ |
||
141 | 1 | public function setChars(array $chars) |
|
142 | { |
||
143 | 1 | $this->_chars = $chars; |
|
144 | 1 | return $this; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Set screen width. |
||
149 | * |
||
150 | * @param int $width screen width |
||
151 | * @return $this |
||
152 | */ |
||
153 | 9 | public function setScreenWidth($width) |
|
154 | { |
||
155 | 9 | $this->_screenWidth = $width; |
|
156 | 9 | return $this; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Set list prefix. |
||
161 | * |
||
162 | * @param string $listPrefix list prefix |
||
163 | * @return $this |
||
164 | */ |
||
165 | 1 | public function setListPrefix($listPrefix) |
|
166 | { |
||
167 | 1 | $this->_listPrefix = $listPrefix; |
|
168 | 1 | return $this; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return string the rendered table |
||
173 | */ |
||
174 | 9 | public function run() |
|
175 | { |
||
176 | 9 | $this->calculateRowsSize(); |
|
177 | 9 | $buffer = $this->renderSeparator( |
|
178 | 9 | $this->_chars[self::CHAR_TOP_LEFT], |
|
179 | 9 | $this->_chars[self::CHAR_TOP_MID], |
|
180 | 9 | $this->_chars[self::CHAR_TOP], |
|
181 | 9 | $this->_chars[self::CHAR_TOP_RIGHT] |
|
182 | ); |
||
183 | // Header |
||
184 | 9 | $buffer .= $this->renderRow($this->_headers, |
|
185 | 9 | $this->_chars[self::CHAR_LEFT], |
|
186 | 9 | $this->_chars[self::CHAR_MIDDLE], |
|
187 | 9 | $this->_chars[self::CHAR_RIGHT] |
|
188 | ); |
||
189 | |||
190 | // Content |
||
191 | 9 | foreach ($this->_rows as $row) { |
|
192 | 9 | $buffer .= $this->renderSeparator( |
|
193 | 9 | $this->_chars[self::CHAR_LEFT_MID], |
|
194 | 9 | $this->_chars[self::CHAR_MID_MID], |
|
195 | 9 | $this->_chars[self::CHAR_MID], |
|
196 | 9 | $this->_chars[self::CHAR_RIGHT_MID] |
|
197 | ); |
||
198 | 9 | $buffer .= $this->renderRow($row, |
|
199 | 9 | $this->_chars[self::CHAR_LEFT], |
|
200 | 9 | $this->_chars[self::CHAR_MIDDLE], |
|
201 | 9 | $this->_chars[self::CHAR_RIGHT]); |
|
202 | } |
||
203 | |||
204 | 9 | $buffer .= $this->renderSeparator( |
|
205 | 9 | $this->_chars[self::CHAR_BOTTOM_LEFT], |
|
206 | 9 | $this->_chars[self::CHAR_BOTTOM_MID], |
|
207 | 9 | $this->_chars[self::CHAR_BOTTOM], |
|
208 | 9 | $this->_chars[self::CHAR_BOTTOM_RIGHT] |
|
209 | ); |
||
210 | |||
211 | 9 | return $buffer; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * Renders a row of data into a string. |
||
216 | * |
||
217 | * @param array $row row of data |
||
218 | * @param string $spanLeft character for left border |
||
219 | * @param string $spanMiddle character for middle border |
||
220 | * @param string $spanRight character for right border |
||
221 | * @return string |
||
222 | * @see \yii\console\widgets\Table::render() |
||
223 | */ |
||
224 | 9 | protected function renderRow(array $row, $spanLeft, $spanMiddle, $spanRight) |
|
225 | { |
||
226 | 9 | $size = $this->_columnWidths; |
|
227 | |||
228 | 9 | $buffer = ''; |
|
229 | 9 | $arrayPointer = []; |
|
230 | 9 | $finalChunk = []; |
|
231 | 9 | for ($i = 0, ($max = $this->calculateRowHeight($row)) ?: $max = 1; $i < $max; $i++) { |
|
232 | 9 | $buffer .= $spanLeft . ' '; |
|
233 | 9 | foreach ($size as $index => $cellSize) { |
|
234 | 9 | $cell = $row[$index] ?? null; |
|
235 | 9 | $prefix = ''; |
|
236 | 9 | if ($index !== 0) { |
|
237 | 9 | $buffer .= $spanMiddle . ' '; |
|
238 | } |
||
239 | 9 | if (is_array($cell)) { |
|
240 | 2 | if (empty($finalChunk[$index])) { |
|
241 | 2 | $finalChunk[$index] = ''; |
|
242 | 2 | $start = 0; |
|
243 | 2 | $prefix = $this->_listPrefix; |
|
244 | 2 | if (!isset($arrayPointer[$index])) { |
|
245 | 2 | $arrayPointer[$index] = 0; |
|
246 | } |
||
247 | } else { |
||
248 | $start = mb_strwidth($finalChunk[$index], Yii::$app->charset); |
||
249 | } |
||
250 | 2 | $chunk = mb_substr($cell[$arrayPointer[$index]], $start, $cellSize - 4, Yii::$app->charset); |
|
251 | 2 | $finalChunk[$index] .= $chunk; |
|
252 | 2 | if (isset($cell[$arrayPointer[$index] + 1]) && $finalChunk[$index] === $cell[$arrayPointer[$index]]) { |
|
253 | 2 | $arrayPointer[$index]++; |
|
254 | 2 | $finalChunk[$index] = ''; |
|
255 | } |
||
256 | } else { |
||
257 | 9 | $chunk = mb_substr($cell, ($cellSize * $i) - ($i * 2), $cellSize - 2, Yii::$app->charset); |
|
258 | } |
||
259 | 9 | $chunk = $prefix . $chunk; |
|
260 | 9 | $repeat = $cellSize - mb_strwidth($chunk, Yii::$app->charset) - 1; |
|
261 | 9 | $buffer .= $chunk; |
|
262 | 9 | if ($repeat >= 0) { |
|
263 | 9 | $buffer .= str_repeat(' ', $repeat); |
|
264 | } |
||
265 | } |
||
266 | 9 | $buffer .= "$spanRight\n"; |
|
267 | } |
||
268 | |||
269 | 9 | return $buffer; |
|
270 | } |
||
271 | |||
272 | /** |
||
273 | * Renders separator. |
||
274 | * |
||
275 | * @param string $spanLeft character for left border |
||
276 | * @param string $spanMid character for middle border |
||
277 | * @param string $spanMidMid character for middle-middle border |
||
278 | * @param string $spanRight character for right border |
||
279 | * @return string the generated separator row |
||
280 | * @see \yii\console\widgets\Table::render() |
||
281 | */ |
||
282 | 9 | protected function renderSeparator($spanLeft, $spanMid, $spanMidMid, $spanRight) |
|
294 | |||
295 | /** |
||
296 | * Calculate the size of rows to draw anchor of columns in console. |
||
297 | * |
||
298 | * @see \yii\console\widgets\Table::render() |
||
299 | */ |
||
300 | 9 | protected function calculateRowsSize() |
|
336 | |||
337 | /** |
||
338 | * Calculate the height of a row. |
||
339 | * |
||
340 | * @param array $row |
||
341 | * @return int maximum row per cell |
||
342 | * @see \yii\console\widgets\Table::render() |
||
343 | */ |
||
344 | protected function calculateRowHeight($row) |
||
369 | |||
370 | /** |
||
371 | * Getting screen width. |
||
372 | * If it is not able to determine screen width, default value `123` will be set. |
||
373 | * |
||
374 | * @return int screen width |
||
375 | */ |
||
376 | 9 | protected function getScreenWidth() |
|
385 | } |
||
386 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.