Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e6d1e3

Browse files
committed
Make pretty the box style table
1 parent 0032368 commit 3e6d1e3

File tree

5 files changed

+131
-15
lines changed

5 files changed

+131
-15
lines changed

UPGRADE-4.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 4.0 to 4.1
22
=======================
33

4+
Console
5+
-------
6+
7+
* Deprecated the `setCrossingChar()` method in favor of the `setCrossingChars()` method in `TableStyle`.
8+
49
HttpFoundation
510
--------------
611

UPGRADE-5.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 4.x to 5.0
22
=======================
33

4+
Console
5+
-------
6+
7+
* Removed the `setCrossingChar()` method in favor of the `setCrossingChars()` method in `TableStyle`.
8+
49
HttpFoundation
510
--------------
611

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
* @author Саша Стаменковић <[email protected]>
2222
* @author Abdellatif Ait boudad <[email protected]>
2323
* @author Max Grigorian <[email protected]>
24+
* @author Dany Maillard <[email protected]>
2425
*/
2526
class Table
2627
{
28+
private const SEPARATOR_TOP = 0;
29+
private const SEPARATOR_MID = 1;
30+
private const SEPARATOR_BOTTOM = 2;
31+
2732
/**
2833
* Table headers.
2934
*/
@@ -281,7 +286,7 @@ public function render()
281286

282287
$this->calculateColumnsWidth(array_merge($headers, $rows));
283288

284-
$this->renderRowSeparator();
289+
$this->renderRowSeparator(self::SEPARATOR_TOP);
285290
if (!empty($headers)) {
286291
foreach ($headers as $header) {
287292
$this->renderRow($header, $this->style->getCellHeaderFormat());
@@ -296,7 +301,7 @@ public function render()
296301
}
297302
}
298303
if (!empty($rows)) {
299-
$this->renderRowSeparator();
304+
$this->renderRowSeparator(self::SEPARATOR_BOTTOM);
300305
}
301306

302307
$this->cleanup();
@@ -307,7 +312,7 @@ public function render()
307312
*
308313
* Example: <code>+-----+-----------+-------+</code>
309314
*/
310-
private function renderRowSeparator()
315+
private function renderRowSeparator(int $type = self::SEPARATOR_MID)
311316
{
312317
if (0 === $count = $this->numberOfColumns) {
313318
return;
@@ -317,9 +322,18 @@ private function renderRowSeparator()
317322
return;
318323
}
319324

320-
$markup = $this->style->getCrossingChar();
325+
if (self::SEPARATOR_MID === $type) {
326+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingMidLeftChar(), $this->style->getCrossingChar(), $this->style->getCrossingMidRightChar());
327+
} elseif (self::SEPARATOR_TOP === $type) {
328+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingTopLeftChar(), $this->style->getCrossingTopMidChar(), $this->style->getCrossingTopRightChar());
329+
} else {
330+
list($leftChar, $midChar, $rightChar) = array($this->style->getCrossingBottomLeftChar(), $this->style->getCrossingBottomMidChar(), $this->style->getCrossingBottomRightChar());
331+
}
332+
333+
$markup = $leftChar;
321334
for ($column = 0; $column < $count; ++$column) {
322-
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar();
335+
$markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]);
336+
$markup .= $column === $count - 1 ? $rightChar : $midChar;
323337
}
324338

325339
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
@@ -618,29 +632,29 @@ private static function initStyles()
618632
$borderless
619633
->setHorizontalBorderChar('=')
620634
->setVerticalBorderChar(' ')
621-
->setCrossingChar(' ')
635+
->setCrossingChars(' ')
622636
;
623637

624638
$compact = new TableStyle();
625639
$compact
626640
->setHorizontalBorderChar('')
627641
->setVerticalBorderChar(' ')
628-
->setCrossingChar('')
642+
->setCrossingChars('')
629643
->setCellRowContentFormat('%s')
630644
;
631645

632646
$styleGuide = new TableStyle();
633647
$styleGuide
634648
->setHorizontalBorderChar('-')
635649
->setVerticalBorderChar(' ')
636-
->setCrossingChar(' ')
650+
->setCrossingChars(' ')
637651
->setCellHeaderFormat('%s')
638652
;
639653

640654
$box = (new TableStyle())
641655
->setHorizontalBorderChar('')
642656
->setVerticalBorderChar('')
643-
->setCrossingChar('')
657+
->setCrossingChars('', '', '', '', '', '', '', '', '')
644658
;
645659

646660
return array(

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

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@
1919
*
2020
* @author Fabien Potencier <[email protected]>
2121
* @author Саша Стаменковић <[email protected]>
22+
* @author Dany Maillard <[email protected]>
2223
*/
2324
class TableStyle
2425
{
2526
private $paddingChar = ' ';
2627
private $horizontalBorderChar = '-';
2728
private $verticalBorderChar = '|';
2829
private $crossingChar = '+';
30+
private $crossingTopRightChar = '+';
31+
private $crossingTopMidChar = '+';
32+
private $crossingTopLeftChar = '+';
33+
private $crossingMidRightChar = '+';
34+
private $crossingBottomRightChar = '+';
35+
private $crossingBottomMidChar = '+';
36+
private $crossingBottomLeftChar = '+';
37+
private $crossingMidLeftChar = '+';
2938
private $cellHeaderFormat = '<info>%s</info>';
3039
private $cellRowFormat = '%s';
3140
private $cellRowContentFormat = ' %s ';
@@ -108,18 +117,61 @@ public function getVerticalBorderChar()
108117
return $this->verticalBorderChar;
109118
}
110119

120+
/**
121+
* Sets crossing characters.
122+
*
123+
* Example:
124+
* <code>
125+
* 1---------------2-----------------------2------------------3
126+
* | ISBN | Title | Author |
127+
* 8---------------0-----------------------0------------------4
128+
* | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
129+
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
130+
* | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
131+
* 7---------------6-----------------------6------------------5
132+
* </code>
133+
*
134+
* @param string $cross Crossing char (see #0 of example)
135+
* @param string|null $topLeft Top left char (see #1 of example), equal to $cross if null
136+
* @param string|null $topMid Top mid char (see #2 of example), equal to $cross if null
137+
* @param string|null $topRight Top right char (see #3 of example), equal to $cross if null
138+
* @param string|null $midRight Mid right char (see #4 of example), equal to $cross if null
139+
* @param string|null $bottomRight Bottom right char (see #5 of example), equal to $cross if null
140+
* @param string|null $bottomMid Bottom mid char (see #6 of example), equal to $cross if null
141+
* @param string|null $bottomLeft Bottom left char (see #7 of example), equal to $cross if null
142+
* @param string|null $midLeft Mid left char (see #8 of example), equal to $cross if null
143+
*
144+
* @return $this
145+
*/
146+
public function setCrossingChars(string $cross, string $topLeft = null, string $topMid = null, string $topRight = null, string $midRight = null, string $bottomRight = null, string $bottomMid = null, string $bottomLeft = null, string $midLeft = null): self
147+
{
148+
$this->crossingChar = $cross;
149+
$this->crossingTopLeftChar = $topLeft ?? $cross;
150+
$this->crossingTopMidChar = $topMid ?? $cross;
151+
$this->crossingTopRightChar = $topRight ?? $cross;
152+
$this->crossingMidRightChar = $midRight ?? $cross;
153+
$this->crossingBottomRightChar = $bottomRight ?? $cross;
154+
$this->crossingBottomMidChar = $bottomMid ?? $cross;
155+
$this->crossingBottomLeftChar = $bottomLeft ?? $cross;
156+
$this->crossingMidLeftChar = $midLeft ?? $cross;
157+
158+
return $this;
159+
}
160+
111161
/**
112162
* Sets crossing character.
113163
*
114164
* @param string $crossingChar
115165
*
116166
* @return $this
167+
*
168+
* @deprecated since version 4.1, to be removed in 5.0. Use {@link setCrossingChars()} instead.
117169
*/
118170
public function setCrossingChar($crossingChar)
119171
{
120-
$this->crossingChar = $crossingChar;
172+
@trigger_error(sprintf('%s() is deprecated since version 4.1 and will be removed in 5.0. Use setCrossingChars() instead.', __METHOD__), E_USER_DEPRECATED);
121173

122-
return $this;
174+
return $this->setCrossingChars($crossingChar);
123175
}
124176

125177
/**
@@ -132,6 +184,46 @@ public function getCrossingChar()
132184
return $this->crossingChar;
133185
}
134186

187+
public function getCrossingTopRightChar(): string
188+
{
189+
return $this->crossingTopRightChar;
190+
}
191+
192+
public function getCrossingTopMidChar(): string
193+
{
194+
return $this->crossingTopMidChar;
195+
}
196+
197+
public function getCrossingTopLeftChar(): string
198+
{
199+
return $this->crossingTopLeftChar;
200+
}
201+
202+
public function getCrossingMidRightChar(): string
203+
{
204+
return $this->crossingMidRightChar;
205+
}
206+
207+
public function getCrossingBottomRightChar(): string
208+
{
209+
return $this->crossingBottomRightChar;
210+
}
211+
212+
public function getCrossingBottomMidChar(): string
213+
{
214+
return $this->crossingBottomMidChar;
215+
}
216+
217+
public function getCrossingBottomLeftChar(): string
218+
{
219+
return $this->crossingBottomLeftChar;
220+
}
221+
222+
public function getCrossingMidLeftChar(): string
223+
{
224+
return $this->crossingMidLeftChar;
225+
}
226+
135227
/**
136228
* Sets header cell format.
137229
*

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ public function renderProvider()
143143
$books,
144144
'box',
145145
<<<'TABLE'
146-
───────────────────────────────────────────────────────────
146+
───────────────────────────────────────────────────────────
147147
│ ISBN │ Title │ Author │
148-
───────────────┼──────────────────────────┼──────────────────
148+
───────────────┼──────────────────────────┼──────────────────
149149
│ 99921-58-10-7 │ Divine Comedy │ Dante Alighieri │
150150
│ 9971-5-0210-0 │ A Tale of Two Cities │ Charles Dickens │
151151
│ 960-425-059-0 │ The Lord of the Rings │ J. R. R. Tolkien │
152152
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
153-
───────────────────────────────────────────────────────────
153+
───────────────────────────────────────────────────────────
154154

155155
TABLE
156156
),
@@ -628,7 +628,7 @@ public function testStyle()
628628
$style
629629
->setHorizontalBorderChar('.')
630630
->setVerticalBorderChar('.')
631-
->setCrossingChar('.')
631+
->setCrossingChars('.')
632632
;
633633

634634
Table::setStyleDefinition('dotfull', $style);

0 commit comments

Comments
 (0)