Webmozart\Console\Util\StringUtil::getLength PHP Method

getLength() public static method

public static getLength ( $string, Webmozart\Console\Api\Formatter\Formatter $formatter = null )
$formatter Webmozart\Console\Api\Formatter\Formatter
    public static function getLength($string, Formatter $formatter = null)
    {
        if ($formatter) {
            $string = $formatter->removeFormat($string);
        }
        if (!function_exists('mb_strwidth')) {
            return strlen($string);
        }
        if (false === ($encoding = mb_detect_encoding($string))) {
            return strlen($string);
        }
        return mb_strwidth($string, $encoding);
    }

Usage Example

Esempio n. 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");
     }
 }
All Usage Examples Of Webmozart\Console\Util\StringUtil::getLength