1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192:
<?php
namespace Webmozart\Console\UI\Component;
use Webmozart\Console\Api\Formatter\Style;
use Webmozart\Console\Api\IO\IO;
use Webmozart\Console\UI\Style\Alignment;
use Webmozart\Console\UI\Style\BorderStyle;
use Webmozart\Console\Util\StringUtil;
class BorderUtil
{
public static function drawTopBorder(IO $io, BorderStyle $style, array $columnLengths, $indentation = 0)
{
self::drawBorder(
$io,
$columnLengths,
$indentation,
$style->getLineHTChar(),
$style->getCornerTLChar(),
$style->getCrossingTChar(),
$style->getCornerTRChar(),
$style->getStyle()
);
}
public static function drawMiddleBorder(IO $io, BorderStyle $style, array $columnLengths, $indentation = 0)
{
self::drawBorder(
$io,
$columnLengths,
$indentation,
$style->getLineHCChar(),
$style->getCrossingLChar(),
$style->getCrossingCChar(),
$style->getCrossingRChar(),
$style->getStyle()
);
}
public static function drawBottomBorder(IO $io, BorderStyle $style, array $columnLengths, $indentation = 0)
{
self::drawBorder(
$io,
$columnLengths,
$indentation,
$style->getLineHBChar(),
$style->getCornerBLChar(),
$style->getCrossingBChar(),
$style->getCornerBRChar(),
$style->getStyle()
);
}
public static function drawRow(IO $io, BorderStyle $style, array $row, array $columnLengths, array $alignments, $cellFormat, Style $cellStyle = null, $paddingChar, $indentation = 0)
{
$totalLines = 0;
foreach ($row as $col => $cell) {
$row[$col] = explode("\n", $cell);
$totalLines = max($totalLines, count($row[$col]));
}
$nbColumns = count($row);
$borderVLChar = $io->format($style->getLineVLChar(), $style->getStyle());
$borderVCChar = $io->format($style->getLineVCChar(), $style->getStyle());
$borderVRChar = $io->format($style->getLineVRChar(), $style->getStyle());
for ($i = 0; $i < $totalLines; ++$i) {
$line = str_repeat(' ', $indentation);
$line .= $borderVLChar;
foreach ($row as $col => &$remainingLines) {
$cellLine = $remainingLines ? array_shift($remainingLines) : '';
$totalPadLength = $columnLengths[$col] - StringUtil::getLength($cellLine, $io);
$paddingLeft = '';
$paddingRight = '';
if ($totalPadLength > 0) {
$alignment = isset($alignments[$col]) ? $alignments[$col] : Alignment::LEFT;
switch ($alignment) {
case Alignment::LEFT:
$paddingRight = str_repeat($paddingChar, $totalPadLength);
break;
case Alignment::RIGHT:
$paddingLeft = str_repeat($paddingChar, $totalPadLength);
break;
case Alignment::CENTER:
$leftPadLength = floor($totalPadLength / 2);
$paddingLeft = str_repeat($paddingChar, $leftPadLength);
$paddingRight = str_repeat($paddingChar, $totalPadLength - $leftPadLength);
break;
}
}
$line .= $io->format(sprintf($cellFormat, $paddingLeft.$cellLine.$paddingRight), $cellStyle);
$line .= $col < $nbColumns - 1 ? $borderVCChar : $borderVRChar;
}
$io->write(rtrim($line)."\n");
}
}
private static function drawBorder(IO $io, array $columnLengths, $indentation, $lineChar, $crossingLChar, $crossingCChar, $crossingRChar, Style $style = null)
{
$line = str_repeat(' ', $indentation);
$line .= $crossingLChar;
for ($i = 0, $l = count($columnLengths); $i < $l; ++$i) {
$line .= str_repeat($lineChar, $columnLengths[$i]);
$line .= $i < $l - 1 ? $crossingCChar : $crossingRChar;
}
$line = rtrim($line);
if ($line) {
$io->write($io->format($line, $style)."\n");
}
}
private function __construct()
{
}
}