1 | <?php |
||
27 | class BorderUtil |
||
|
|||
28 | { |
||
29 | /** |
||
30 | * Draws a top border. |
||
31 | * |
||
32 | * Crossings are drawn between each pair of columns if more than one column |
||
33 | * length is passed. |
||
34 | * |
||
35 | * @param IO $io The I/O. |
||
36 | * @param BorderStyle $style The border style. |
||
37 | * @param int[] $columnLengths An array of column lengths. |
||
38 | * @param int $indentation The number of spaces to indent. |
||
39 | */ |
||
40 | 21 | public static function drawTopBorder(IO $io, BorderStyle $style, array $columnLengths, $indentation = 0) |
|
41 | { |
||
42 | 21 | self::drawBorder( |
|
43 | $io, |
||
44 | $columnLengths, |
||
45 | $indentation, |
||
46 | 21 | $style->getLineHTChar(), |
|
47 | 21 | $style->getCornerTLChar(), |
|
48 | 21 | $style->getCrossingTChar(), |
|
49 | 21 | $style->getCornerTRChar(), |
|
50 | 21 | $style->getStyle() |
|
51 | ); |
||
52 | 21 | } |
|
53 | |||
54 | /** |
||
55 | * Draws a middle border. |
||
56 | * |
||
57 | * Crossings are drawn between each pair of columns if more than one column |
||
58 | * length is passed. |
||
59 | * |
||
60 | * @param IO $io The I/O. |
||
61 | * @param BorderStyle $style The border style. |
||
62 | * @param int[] $columnLengths An array of column lengths. |
||
63 | * @param int $indentation The number of spaces to indent. |
||
64 | */ |
||
65 | 20 | public static function drawMiddleBorder(IO $io, BorderStyle $style, array $columnLengths, $indentation = 0) |
|
66 | { |
||
67 | 20 | self::drawBorder( |
|
68 | $io, |
||
69 | $columnLengths, |
||
70 | $indentation, |
||
71 | 20 | $style->getLineHCChar(), |
|
72 | 20 | $style->getCrossingLChar(), |
|
73 | 20 | $style->getCrossingCChar(), |
|
74 | 20 | $style->getCrossingRChar(), |
|
75 | 20 | $style->getStyle() |
|
76 | ); |
||
77 | 20 | } |
|
78 | |||
79 | /** |
||
80 | * Draws a bottom border. |
||
81 | * |
||
82 | * Crossings are drawn between each pair of columns if more than one column |
||
83 | * length is passed. |
||
84 | * |
||
85 | * @param IO $io The I/O. |
||
86 | * @param BorderStyle $style The border style. |
||
87 | * @param int[] $columnLengths An array of column lengths. |
||
88 | * @param int $indentation The number of spaces to indent. |
||
89 | */ |
||
90 | 21 | public static function drawBottomBorder(IO $io, BorderStyle $style, array $columnLengths, $indentation = 0) |
|
91 | { |
||
92 | 21 | self::drawBorder( |
|
93 | $io, |
||
94 | $columnLengths, |
||
95 | $indentation, |
||
96 | 21 | $style->getLineHBChar(), |
|
97 | 21 | $style->getCornerBLChar(), |
|
98 | 21 | $style->getCrossingBChar(), |
|
99 | 21 | $style->getCornerBRChar(), |
|
100 | 21 | $style->getStyle() |
|
101 | ); |
||
102 | 21 | } |
|
103 | |||
104 | /** |
||
105 | * Draws a bordered row of cells. |
||
106 | * |
||
107 | * @param IO $io The I/O. |
||
108 | * @param BorderStyle $style The border style. |
||
109 | * @param string[] $row The row cells. |
||
110 | * @param int[] $columnLengths The lengths of the cells. |
||
111 | * @param int[] $alignments The alignments of the cells. |
||
112 | * @param string $cellFormat The cell format. |
||
113 | * @param Style $cellStyle The cell style. |
||
114 | * @param string $paddingChar The character used to pad cells. |
||
115 | * @param int $indentation The number of spaces to indent. |
||
116 | */ |
||
117 | 21 | public static function drawRow(IO $io, BorderStyle $style, array $row, array $columnLengths, array $alignments, $cellFormat, Style $cellStyle = null, $paddingChar, $indentation = 0) |
|
118 | { |
||
119 | 21 | $totalLines = 0; |
|
120 | |||
121 | // Split all cells into lines |
||
122 | 21 | foreach ($row as $col => $cell) { |
|
123 | 21 | $row[$col] = explode("\n", $cell); |
|
124 | 21 | $totalLines = max($totalLines, count($row[$col])); |
|
125 | } |
||
126 | |||
127 | 21 | $nbColumns = count($row); |
|
128 | 21 | $borderVLChar = $io->format($style->getLineVLChar(), $style->getStyle()); |
|
129 | 21 | $borderVCChar = $io->format($style->getLineVCChar(), $style->getStyle()); |
|
130 | 21 | $borderVRChar = $io->format($style->getLineVRChar(), $style->getStyle()); |
|
131 | |||
132 | 21 | for ($i = 0; $i < $totalLines; ++$i) { |
|
133 | 21 | $line = str_repeat(' ', $indentation); |
|
134 | 21 | $line .= $borderVLChar; |
|
135 | |||
136 | 21 | foreach ($row as $col => &$remainingLines) { |
|
137 | 21 | $cellLine = $remainingLines ? array_shift($remainingLines) : ''; |
|
138 | 21 | $totalPadLength = $columnLengths[$col] - StringUtil::getLength($cellLine, $io); |
|
139 | 21 | $paddingLeft = ''; |
|
140 | 21 | $paddingRight = ''; |
|
141 | |||
142 | 21 | if ($totalPadLength > 0) { |
|
143 | 20 | $alignment = isset($alignments[$col]) ? $alignments[$col] : Alignment::LEFT; |
|
144 | |||
145 | switch ($alignment) { |
||
146 | 20 | case Alignment::LEFT: |
|
147 | 18 | $paddingRight = str_repeat($paddingChar, $totalPadLength); |
|
148 | 18 | break; |
|
149 | 3 | case Alignment::RIGHT: |
|
150 | 2 | $paddingLeft = str_repeat($paddingChar, $totalPadLength); |
|
151 | 2 | break; |
|
152 | 2 | case Alignment::CENTER: |
|
153 | 2 | $leftPadLength = floor($totalPadLength / 2); |
|
154 | 2 | $paddingLeft = str_repeat($paddingChar, $leftPadLength); |
|
155 | 2 | $paddingRight = str_repeat($paddingChar, $totalPadLength - $leftPadLength); |
|
156 | 2 | break; |
|
157 | } |
||
158 | } |
||
159 | |||
160 | 21 | $line .= $io->format(sprintf($cellFormat, $paddingLeft.$cellLine.$paddingRight), $cellStyle); |
|
161 | 21 | $line .= $col < $nbColumns - 1 ? $borderVCChar : $borderVRChar; |
|
162 | } |
||
163 | |||
164 | // Remove trailing space |
||
165 | 21 | $io->write(rtrim($line)."\n"); |
|
166 | } |
||
167 | 21 | } |
|
168 | |||
169 | 21 | private static function drawBorder(IO $io, array $columnLengths, $indentation, $lineChar, $crossingLChar, $crossingCChar, $crossingRChar, Style $style = null) |
|
170 | { |
||
171 | 21 | $line = str_repeat(' ', $indentation); |
|
172 | 21 | $line .= $crossingLChar; |
|
173 | |||
174 | 21 | for ($i = 0, $l = count($columnLengths); $i < $l; ++$i) { |
|
175 | 21 | $line .= str_repeat($lineChar, $columnLengths[$i]); |
|
176 | 21 | $line .= $i < $l - 1 ? $crossingCChar : $crossingRChar; |
|
177 | } |
||
178 | |||
179 | // Remove trailing space |
||
180 | 21 | $line = rtrim($line); |
|
181 | |||
182 | // Render only non-empty separators |
||
183 | 21 | if ($line) { |
|
184 | 20 | $io->write($io->format($line, $style)."\n"); |
|
185 | } |
||
186 | 21 | } |
|
187 | |||
188 | private function __construct() |
||
191 | } |
||
192 |