Exemplo n.º 1
0
 /**
  * Render the table
  *
  * @throws Exception\UnexpectedValueException When no rows were added to the table
  * @return string
  */
 public function render()
 {
     // There should be at least one row
     if (count($this->_rows) === 0) {
         throw new Exception\UnexpectedValueException('No rows were added to the table yet');
     }
     // Initiate the result variable
     $result = '';
     // Count total columns
     $totalNumColumns = count($this->_columnWidths);
     // Now render all rows, starting from the first one
     $numRows = count($this->_rows);
     foreach ($this->_rows as $rowNum => $row) {
         // Get all column widths
         if (isset($columnWidths) === true) {
             $lastColumnWidths = $columnWidths;
         }
         $renderedRow = $row->render($this->_columnWidths, $this->_decorator, $this->_padding);
         $columnWidths = $row->getColumnWidths();
         $numColumns = count($columnWidths);
         // Check what we have to draw
         if ($rowNum === 0) {
             // If this is the first row, draw the table top
             $result .= $this->_decorator->getTopLeft();
             foreach ($columnWidths as $columnNum => $columnWidth) {
                 $result .= str_repeat($this->_decorator->getHorizontal(), $columnWidth);
                 if ($columnNum + 1 === $numColumns) {
                     $result .= $this->_decorator->getTopRight();
                 } else {
                     $result .= $this->_decorator->getHorizontalDown();
                 }
             }
             $result .= "\n";
         } else {
             // Else check if we have to draw the row separator
             if ($this->_autoSeparate & self::AUTO_SEPARATE_ALL) {
                 $drawSeparator = true;
             } elseif ($rowNum === 1 && $this->_autoSeparate & self::AUTO_SEPARATE_HEADER) {
                 $drawSeparator = true;
             } elseif ($rowNum === $numRows - 1 && $this->_autoSeparate & self::AUTO_SEPARATE_FOOTER) {
                 $drawSeparator = true;
             } else {
                 $drawSeparator = false;
             }
             if ($drawSeparator) {
                 $result .= $this->_decorator->getVerticalRight();
                 $currentUpperColumn = 0;
                 $currentLowerColumn = 0;
                 $currentUpperWidth = 0;
                 $currentLowerWidth = 0;
                 // Loop through all column widths
                 foreach ($this->_columnWidths as $columnNum => $columnWidth) {
                     // Add the horizontal line
                     $result .= str_repeat($this->_decorator->getHorizontal(), $columnWidth);
                     // If this is the last line, break out
                     if ($columnNum + 1 === $totalNumColumns) {
                         break;
                     }
                     // Else check, which connector style has to be used
                     $connector = 0x0;
                     $currentUpperWidth += $columnWidth;
                     $currentLowerWidth += $columnWidth;
                     if ($lastColumnWidths[$currentUpperColumn] === $currentUpperWidth) {
                         $connector |= 0x1;
                         $currentUpperColumn += 1;
                         $currentUpperWidth = 0;
                     } else {
                         $currentUpperWidth += 1;
                     }
                     if ($columnWidths[$currentLowerColumn] === $currentLowerWidth) {
                         $connector |= 0x2;
                         $currentLowerColumn += 1;
                         $currentLowerWidth = 0;
                     } else {
                         $currentLowerWidth += 1;
                     }
                     switch ($connector) {
                         case 0x0:
                             $result .= $this->_decorator->getHorizontal();
                             break;
                         case 0x1:
                             $result .= $this->_decorator->getHorizontalUp();
                             break;
                         case 0x2:
                             $result .= $this->_decorator->getHorizontalDown();
                             break;
                         case 0x3:
                             $result .= $this->_decorator->getCross();
                             break;
                         default:
                             // This can never happen, but the CS tells I have to have it ...
                             break;
                     }
                 }
                 $result .= $this->_decorator->getVerticalLeft() . "\n";
             }
         }
         // Add the rendered row to the result
         $result .= $renderedRow;
         // If this is the last row, draw the table bottom
         if ($rowNum + 1 === $numRows) {
             $result .= $this->_decorator->getBottomLeft();
             foreach ($columnWidths as $columnNum => $columnWidth) {
                 $result .= str_repeat($this->_decorator->getHorizontal(), $columnWidth);
                 if ($columnNum + 1 === $numColumns) {
                     $result .= $this->_decorator->getBottomRight();
                 } else {
                     $result .= $this->_decorator->getHorizontalUp();
                 }
             }
             $result .= "\n";
         }
     }
     return $result;
 }