File tree Expand file tree Collapse file tree 3 files changed +73
-2
lines changed Expand file tree Collapse file tree 3 files changed +73
-2
lines changed Original file line number Diff line number Diff line change @@ -158,8 +158,18 @@ public function addRows(array $rows)
158
158
return $ this ;
159
159
}
160
160
161
- public function addRow (array $ row )
161
+ public function addRow ($ row )
162
162
{
163
+ if ($ row instanceof TableSeparator) {
164
+ $ this ->rows [] = $ row ;
165
+
166
+ return ;
167
+ }
168
+
169
+ if (!is_array ($ row )) {
170
+ throw new InvalidArgumentException ('A row must be an array or a TableSeparator instance. ' );
171
+ }
172
+
163
173
$ this ->rows [] = array_values ($ row );
164
174
165
175
$ keys = array_keys ($ this ->rows );
@@ -217,7 +227,11 @@ public function render()
217
227
$ this ->renderRowSeparator ();
218
228
}
219
229
foreach ($ this ->rows as $ row ) {
220
- $ this ->renderRow ($ row , $ this ->style ->getCellRowFormat ());
230
+ if ($ row instanceof TableSeparator) {
231
+ $ this ->renderRowSeparator ();
232
+ } else {
233
+ $ this ->renderRow ($ row , $ this ->style ->getCellRowFormat ());
234
+ }
221
235
}
222
236
if (!empty ($ this ->rows )) {
223
237
$ this ->renderRowSeparator ();
@@ -337,6 +351,10 @@ private function getColumnWidth($column)
337
351
338
352
$ lengths = array ($ this ->getCellWidth ($ this ->headers , $ column ));
339
353
foreach ($ this ->rows as $ row ) {
354
+ if ($ row instanceof TableSeparator) {
355
+ continue ;
356
+ }
357
+
340
358
$ lengths [] = $ this ->getCellWidth ($ row , $ column );
341
359
}
342
360
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \Component \Console \Helper ;
13
+
14
+ /**
15
+ * Marks a row as being a separator.
16
+ *
17
+ * @author Fabien Potencier <fabien@symfony.com>
18
+ */
19
+ class TableSeparator
20
+ {
21
+ }
Original file line number Diff line number Diff line change 13
13
14
14
use Symfony \Component \Console \Helper \Table ;
15
15
use Symfony \Component \Console \Helper \TableStyle ;
16
+ use Symfony \Component \Console \Helper \TableSeparator ;
16
17
use Symfony \Component \Console \Output \StreamOutput ;
17
18
18
19
class TableTest extends \PHPUnit_Framework_TestCase
@@ -306,6 +307,37 @@ public function testStyle()
306
307
. Bar .
307
308
.......
308
309
310
+ TABLE ;
311
+
312
+ $ this ->assertEquals ($ expected , $ this ->getOutputContent ($ output ));
313
+ }
314
+
315
+ public function testRowSeparator ()
316
+ {
317
+ $ table = new Table ($ output = $ this ->getOutputStream ());
318
+ $ table
319
+ ->setHeaders (array ('Foo ' ))
320
+ ->setRows (array (
321
+ array ('Bar1 ' ),
322
+ new TableSeparator (),
323
+ array ('Bar2 ' ),
324
+ new TableSeparator (),
325
+ array ('Bar3 ' ),
326
+ ));
327
+ $ table ->render ();
328
+
329
+ $ expected =
330
+ <<<TABLE
331
+ +------+
332
+ | Foo |
333
+ +------+
334
+ | Bar1 |
335
+ +------+
336
+ | Bar2 |
337
+ +------+
338
+ | Bar3 |
339
+ +------+
340
+
309
341
TABLE ;
310
342
311
343
$ this ->assertEquals ($ expected , $ this ->getOutputContent ($ output ));
You can’t perform that action at this time.
0 commit comments