Completed
Push — master ( bd5ff6...90c2c7 )
by Bernhard
07:04
created

BorderUtil::drawMiddleBorder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4286
cc 1
eloc 10
nc 1
nop 4
crap 1
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
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 Webmozart\Console\UI\Component;
13
14
use Webmozart\Console\Api\Formatter\Style;
15
use Webmozart\Console\Api\IO\IO;
16
use Webmozart\Console\UI\Style\Alignment;
17
use Webmozart\Console\UI\Style\BorderStyle;
18
use Webmozart\Console\Util\StringUtil;
19
20
/**
21
 * Contains utility methods to draw borders and bordered cells.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class BorderUtil
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
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.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cellStyle not be null|Style?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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
                        case Alignment::RIGHT:
150 2
                            $paddingLeft = str_repeat($paddingChar, $totalPadLength);
151 2
                            break;
152
                        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()
189
    {
190
    }
191
}
192