Skip to content

Commit 14caaec

Browse files
committed
[Console] added the possibility to insert a table separator anywhere in a table output
1 parent 39c495f commit 14caaec

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/Symfony/Component/Console/Helper/Table.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,18 @@ public function addRows(array $rows)
158158
return $this;
159159
}
160160

161-
public function addRow(array $row)
161+
public function addRow($row)
162162
{
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+
163173
$this->rows[] = array_values($row);
164174

165175
$keys = array_keys($this->rows);
@@ -217,7 +227,11 @@ public function render()
217227
$this->renderRowSeparator();
218228
}
219229
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+
}
221235
}
222236
if (!empty($this->rows)) {
223237
$this->renderRowSeparator();
@@ -337,6 +351,10 @@ private function getColumnWidth($column)
337351

338352
$lengths = array($this->getCellWidth($this->headers, $column));
339353
foreach ($this->rows as $row) {
354+
if ($row instanceof TableSeparator) {
355+
continue;
356+
}
357+
340358
$lengths[] = $this->getCellWidth($row, $column);
341359
}
342360

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

src/Symfony/Component/Console/Tests/Helper/TableTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Helper\Table;
1515
use Symfony\Component\Console\Helper\TableStyle;
16+
use Symfony\Component\Console\Helper\TableSeparator;
1617
use Symfony\Component\Console\Output\StreamOutput;
1718

1819
class TableTest extends \PHPUnit_Framework_TestCase
@@ -306,6 +307,37 @@ public function testStyle()
306307
. Bar .
307308
.......
308309
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+
309341
TABLE;
310342

311343
$this->assertEquals($expected, $this->getOutputContent($output));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy