Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1.     /**
  2.      * @param $left
  3.      * @param $right
  4.      * @param int $charSizeMultiply
  5.      * @param bool $nowrap
  6.      * @return FormattedRow[]
  7.      */
  8.     public function justifyRow($left, $right, $charSizeMultiply = null, $nowrap = false) {
  9.         $maxLength = floor($this->_receiptWidth / ($charSizeMultiply ?? 1));
  10.         $minLength = floor($maxLength / 2);
  11.         $leftLen = mb_strlen($left);
  12.         $rightLen = mb_strlen($right);
  13.  
  14.         $result = [];
  15.         $sectionLength = $maxLength - $this->_alignmentJustifyOffset;
  16.         if($leftLen + $rightLen > $sectionLength) {
  17.  
  18.             if($nowrap) {
  19.  
  20.                 $leftRows = $this->divideTextByRow($left, $sectionLength);
  21.                 foreach ($leftRows as $n => $leftRow) {
  22.                     $leftRowLen = mb_strlen($leftRow);
  23.                     if($leftRowLen + $rightLen > $sectionLength && $n === 0) {
  24.                         $r = $this->formatRow($leftRow, self::ALIGNMENT_LEFT, $charSizeMultiply);
  25.                     } else {
  26.                         $r = $this->justifyRow($leftRow, $right, $charSizeMultiply, $nowrap);
  27.                     }
  28.                     $result = array_merge($result, $r);
  29.                 }
  30.             } else {
  31.                 if($leftLen > $rightLen || $leftLen > $minLength) {
  32.                     $leftRows = $this->divideTextByRow($left, $sectionLength - $rightLen >= $minLength ? $sectionLength - $rightLen : $sectionLength);
  33.                     $leftRowsCount = sizeof($leftRows);
  34.                     foreach ($leftRows as $n => $leftRow) {
  35.                         if ($n === $leftRowsCount - 1) {
  36.                             $r = $this->justifyRow($leftRow, $right, $charSizeMultiply, $nowrap);
  37.                         } else {
  38.                             $r = $this->formatRow($leftRow, self::ALIGNMENT_LEFT, $charSizeMultiply);
  39.                         }
  40.                         $result = array_merge($result, $r);
  41.                     }
  42.                 } else {
  43.                     Yii::error(print_r(["'{$left}'", "'{$right}'", $minLength, $maxLength, $sectionLength], true));
  44.                     $rightRows = $this->divideTextByRow($right, $sectionLength - $leftLen >= $minLength ? $sectionLength - $leftLen : $sectionLength);
  45.                     foreach ($rightRows as $n => $rightRow) {
  46.                         if ($n === 0) {
  47.                             $r = $this->justifyRow($left, $rightRow, $charSizeMultiply, $nowrap);
  48.                         } else {
  49.                             $r = $this->formatRow($rightRow, self::ALIGNMENT_RIGHT, $charSizeMultiply);
  50.                         }
  51.                         $result = array_merge($result, $r);
  52.                     }
  53.                 }
  54.             }
  55.  
  56.         } else {
  57.             $result = $this->formatRow($left.str_repeat(' ', $maxLength - $leftLen - $rightLen).$right, self::ALIGNMENT_LEFT, $charSizeMultiply);
  58.         }
  59.  
  60.         return $result;
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement