1 | <?php |
||
49 | class Table extends Widget |
||
50 | { |
||
51 | const DEFAULT_CONSOLE_SCREEN_WIDTH = 120; |
||
52 | const CONSOLE_SCROLLBAR_OFFSET = 3; |
||
53 | |||
54 | const CHAR_TOP = 'top'; |
||
55 | const CHAR_TOP_MID = 'top-mid'; |
||
56 | const CHAR_TOP_LEFT = 'top-left'; |
||
57 | const CHAR_TOP_RIGHT = 'top-right'; |
||
58 | const CHAR_BOTTOM = 'bottom'; |
||
59 | const CHAR_BOTTOM_MID = 'bottom-mid'; |
||
60 | const CHAR_BOTTOM_LEFT = 'bottom-left'; |
||
61 | const CHAR_BOTTOM_RIGHT = 'bottom-right'; |
||
62 | const CHAR_LEFT = 'left'; |
||
63 | const CHAR_LEFT_MID = 'left-mid'; |
||
64 | const CHAR_MID = 'mid'; |
||
65 | const CHAR_MID_MID = 'mid-mid'; |
||
66 | const CHAR_RIGHT = 'right'; |
||
67 | const CHAR_RIGHT_MID = 'right-mid'; |
||
68 | const CHAR_MIDDLE = 'middle'; |
||
69 | |||
70 | /** |
||
71 | * @var array table headers |
||
72 | */ |
||
73 | private $_headers = []; |
||
74 | /** |
||
75 | * @var array table rows |
||
76 | */ |
||
77 | private $_rows = []; |
||
78 | /** |
||
79 | * @var array table chars |
||
80 | */ |
||
81 | private $_chars = [ |
||
82 | self::CHAR_TOP => '═', |
||
83 | self::CHAR_TOP_MID => '╤', |
||
84 | self::CHAR_TOP_LEFT => '╔', |
||
85 | self::CHAR_TOP_RIGHT => '╗', |
||
86 | self::CHAR_BOTTOM => '═', |
||
87 | self::CHAR_BOTTOM_MID => '╧', |
||
88 | self::CHAR_BOTTOM_LEFT => '╚', |
||
89 | self::CHAR_BOTTOM_RIGHT => '╝', |
||
90 | self::CHAR_LEFT => '║', |
||
91 | self::CHAR_LEFT_MID => '╟', |
||
92 | self::CHAR_MID => '─', |
||
93 | self::CHAR_MID_MID => '┼', |
||
94 | self::CHAR_RIGHT => '║', |
||
95 | self::CHAR_RIGHT_MID => '╢', |
||
96 | self::CHAR_MIDDLE => '│', |
||
97 | ]; |
||
98 | /** |
||
99 | * @var array table column widths |
||
100 | */ |
||
101 | private $_columnWidths = []; |
||
102 | /** |
||
103 | * @var int screen width |
||
104 | */ |
||
105 | private $_screenWidth; |
||
106 | /** |
||
107 | * @var string list prefix |
||
108 | */ |
||
109 | private $_listPrefix = '• '; |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Set table headers. |
||
114 | * |
||
115 | * @param array $headers table headers |
||
116 | * @return $this |
||
117 | */ |
||
118 | 8 | public function setHeaders(array $headers) |
|
123 | |||
124 | /** |
||
125 | * Set table rows. |
||
126 | * |
||
127 | * @param array $rows table rows |
||
128 | * @return $this |
||
129 | */ |
||
130 | 8 | public function setRows(array $rows) |
|
135 | |||
136 | /** |
||
137 | * Set table chars. |
||
138 | * |
||
139 | * @param array $chars table chars |
||
140 | * @return $this |
||
141 | */ |
||
142 | 1 | public function setChars(array $chars) |
|
147 | |||
148 | /** |
||
149 | * Set screen width. |
||
150 | * |
||
151 | * @param int $width screen width |
||
152 | * @return $this |
||
153 | */ |
||
154 | 8 | public function setScreenWidth($width) |
|
159 | |||
160 | /** |
||
161 | * Set list prefix. |
||
162 | * |
||
163 | * @param string $listPrefix list prefix |
||
164 | * @return $this |
||
165 | */ |
||
166 | 1 | public function setListPrefix($listPrefix) |
|
171 | |||
172 | /** |
||
173 | * @return string the rendered table |
||
174 | */ |
||
175 | 8 | public function run() |
|
214 | |||
215 | /** |
||
216 | * Renders a row of data into a string. |
||
217 | * |
||
218 | * @param array $row row of data |
||
219 | * @param string $spanLeft character for left border |
||
220 | * @param string $spanMiddle character for middle border |
||
221 | * @param string $spanRight character for right border |
||
222 | * @return string |
||
223 | * @see \yii\console\widgets\Table::render() |
||
224 | */ |
||
225 | 8 | protected function renderRow(array $row, $spanLeft, $spanMiddle, $spanRight) |
|
272 | |||
273 | /** |
||
274 | * Renders separator. |
||
275 | * |
||
276 | * @param string $spanLeft character for left border |
||
277 | * @param string $spanMid character for middle border |
||
278 | * @param string $spanMidMid character for middle-middle border |
||
279 | * @param string $spanRight character for right border |
||
280 | * @return string the generated separator row |
||
281 | * @see \yii\console\widgets\Table::render() |
||
282 | */ |
||
283 | 8 | protected function renderSeparator($spanLeft, $spanMid, $spanMidMid, $spanRight) |
|
295 | |||
296 | /** |
||
297 | * Calculate the size of rows to draw anchor of columns in console. |
||
298 | * |
||
299 | * @see \yii\console\widgets\Table::render() |
||
300 | */ |
||
301 | 8 | protected function calculateRowsSize() |
|
302 | { |
||
303 | 8 | $this->_columnWidths = $columns = []; |
|
304 | 8 | $totalWidth = 0; |
|
305 | 8 | $screenWidth = $this->getScreenWidth() - self::CONSOLE_SCROLLBAR_OFFSET; |
|
306 | |||
307 | 8 | for ($i = 0, $count = count($this->_headers); $i < $count; $i++) { |
|
308 | 8 | $columns[] = ArrayHelper::getColumn($this->_rows, $i); |
|
309 | 8 | $columns[$i][] = $this->_headers[$i]; |
|
310 | } |
||
311 | |||
312 | 8 | foreach ($columns as $column) { |
|
313 | 8 | $columnWidth = max(array_map(function ($val) { |
|
314 | 8 | if (is_array($val)) { |
|
315 | 2 | $encodings = array_fill(0, count($val), Yii::$app->charset); |
|
316 | 2 | return max(array_map('mb_strwidth', $val, $encodings)) + mb_strwidth($this->_listPrefix, Yii::$app->charset); |
|
317 | } |
||
318 | |||
319 | 8 | return mb_strwidth($val, Yii::$app->charset); |
|
320 | 8 | }, $column)) + 2; |
|
321 | 8 | $this->_columnWidths[] = $columnWidth; |
|
322 | 8 | $totalWidth += $columnWidth; |
|
323 | } |
||
324 | |||
325 | 8 | $relativeWidth = $screenWidth / $totalWidth; |
|
326 | |||
327 | 8 | if ($totalWidth > $screenWidth) { |
|
328 | foreach ($this->_columnWidths as $j => $width) { |
||
|
|||
329 | $this->_columnWidths[$j] = (int) ($width * $relativeWidth); |
||
330 | if ($j === count($this->_columnWidths)) { |
||
331 | $this->_columnWidths = $totalWidth; |
||
332 | } |
||
333 | $totalWidth -= $this->_columnWidths[$j]; |
||
334 | } |
||
335 | } |
||
336 | 8 | } |
|
337 | |||
338 | /** |
||
339 | * Calculate the height of a row. |
||
340 | * |
||
341 | * @param array $row |
||
342 | * @return int maximum row per cell |
||
343 | * @see \yii\console\widgets\Table::render() |
||
344 | */ |
||
345 | protected function calculateRowHeight($row) |
||
346 | { |
||
347 | 8 | $rowsPerCell = array_map(function ($size, $columnWidth) { |
|
348 | 8 | if (is_array($columnWidth)) { |
|
349 | 2 | $rows = 0; |
|
350 | 2 | foreach ($columnWidth as $width) { |
|
351 | 2 | $rows += ceil($width / ($size - 2)); |
|
352 | } |
||
353 | |||
354 | 2 | return $rows; |
|
355 | } |
||
356 | |||
357 | 8 | return ceil($columnWidth / ($size - 2)); |
|
358 | }, $this->_columnWidths, array_map(function ($val) { |
||
359 | 8 | if (is_array($val)) { |
|
360 | 2 | $encodings = array_fill(0, count($val), Yii::$app->charset); |
|
361 | 2 | return array_map('mb_strwidth', $val, $encodings); |
|
362 | } |
||
363 | |||
364 | 8 | return mb_strwidth($val, Yii::$app->charset); |
|
365 | 8 | }, $row) |
|
366 | ); |
||
367 | |||
368 | 8 | return max($rowsPerCell); |
|
369 | } |
||
370 | |||
371 | /** |
||
372 | * Getting screen width. |
||
373 | * If it is not able to determine screen width, default value `123` will be set. |
||
374 | * |
||
375 | * @return int screen width |
||
376 | */ |
||
377 | 8 | protected function getScreenWidth() |
|
387 | } |
||
388 |
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.