Since: 1.0
Author: Bernhard Schussek (bschussek@gmail.com)
Beispiel #1
0
 /**
  * Draws a bordered row of cells.
  *
  * @param IO          $io            The I/O.
  * @param BorderStyle $style         The border style.
  * @param string[]    $row           The row cells.
  * @param int[]       $columnLengths The lengths of the cells.
  * @param int[]       $alignments    The alignments of the cells.
  * @param string      $cellFormat    The cell format.
  * @param Style       $cellStyle     The cell style.
  * @param string      $paddingChar   The character used to pad cells.
  * @param int         $indentation   The number of spaces to indent.
  */
 public static function drawRow(IO $io, BorderStyle $style, array $row, array $columnLengths, array $alignments, $cellFormat, Style $cellStyle = null, $paddingChar, $indentation = 0)
 {
     $totalLines = 0;
     // Split all cells into lines
     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;
         }
         // Remove trailing space
         $io->write(rtrim($line) . "\n");
     }
 }
Beispiel #2
0
 private function wrapColumn($col, $columnLength, Formatter $formatter)
 {
     foreach ($this->wrappedRows as $i => $row) {
         $cell = $row[$col];
         $cellLength = $this->cellLengths[$i][$col];
         if ($cellLength > $columnLength) {
             $this->wordWraps = true;
             if (!$this->wordCuts) {
                 $minLengthWithoutCut = StringUtil::getMaxWordLength($cell, $formatter);
                 if ($minLengthWithoutCut > $columnLength) {
                     $this->wordCuts = true;
                 }
             }
             // TODO use format aware wrapper
             // true: Words may be cut in two
             $wrappedCell = wordwrap($cell, $columnLength, "\n", true);
             $this->wrappedRows[$i][$col] = $wrappedCell;
             // Refresh cell length
             $this->cellLengths[$i][$col] = StringUtil::getMaxLineLength($wrappedCell, $formatter);
         }
     }
 }
Beispiel #3
0
 /**
  * @dataProvider getInvalidParseFloatTests
  * @expectedException \Webmozart\Console\Api\Args\Format\InvalidValueException
  */
 public function testParseFloatFailsIfInvalid($input, $nullable = true)
 {
     StringUtil::parseFloat($input, $nullable);
 }
Beispiel #4
0
 /**
  * Parses an argument value.
  *
  * Pass one of the flags {@link STRING}, {@link BOOLEAN}, {@link INTEGER}
  * and {@link FLOAT} to the constructor to configure the result of this
  * method. You can optionally combine the flags with {@link NULLABLE} to
  * support the conversion of "null" to `null`.
  *
  * @param mixed $value The value to parse.
  *
  * @return mixed The parsed value.
  *
  * @throws InvalidValueException
  */
 public function parseValue($value)
 {
     $nullable = $this->flags & self::NULLABLE;
     if ($this->flags & self::BOOLEAN) {
         return StringUtil::parseBoolean($value, $nullable);
     }
     if ($this->flags & self::INTEGER) {
         return StringUtil::parseInteger($value, $nullable);
     }
     if ($this->flags & self::FLOAT) {
         return StringUtil::parseFloat($value, $nullable);
     }
     return StringUtil::parseString($value, $nullable);
 }
Beispiel #5
0
 private function getCellWrapper(Formatter $formatter, $screenWidth, $excessColumnWidth, $indentation)
 {
     $borderStyle = $this->style->getBorderStyle();
     $borderWidth = StringUtil::getLength($borderStyle->getLineVLChar()) + ($this->nbColumns - 1) * StringUtil::getLength($borderStyle->getLineVCChar()) + StringUtil::getLength($borderStyle->getLineVRChar());
     $availableWidth = $screenWidth - $indentation - $borderWidth - $this->nbColumns * $excessColumnWidth;
     $wrapper = new CellWrapper();
     foreach ($this->headerRow as $headerCell) {
         $wrapper->addCell($headerCell);
     }
     foreach ($this->rows as $row) {
         foreach ($row as $cell) {
             $wrapper->addCell($cell);
         }
     }
     $wrapper->fit($availableWidth, $this->nbColumns, $formatter);
     return $wrapper;
 }
Beispiel #6
0
 private function getCellWrapper(Formatter $formatter, $screenWidth, $excessColumnWidth, $indentation)
 {
     $borderStyle = $this->style->getBorderStyle();
     $wrapper = new CellWrapper();
     foreach ($this->cells as $cell) {
         $wrapper->addCell($cell);
     }
     $nbColumns = min($this->maxNbColumns, $wrapper->getEstimatedNbColumns($screenWidth));
     do {
         $borderWidth = StringUtil::getLength($borderStyle->getLineVLChar()) + ($nbColumns - 1) * StringUtil::getLength($borderStyle->getLineVCChar()) + StringUtil::getLength($borderStyle->getLineVRChar());
         $availableWidth = $screenWidth - $indentation - $borderWidth - $nbColumns * $excessColumnWidth;
         $wrapper->fit($availableWidth, $nbColumns, $formatter);
         --$nbColumns;
     } while ($wrapper->hasWordCuts() && $nbColumns >= $this->minNbColumns);
     return $wrapper;
 }