Example #1
0
 /**
  * Render the row
  *
  * @param  array                               $columnWidths Width of all columns
  * @param  Zend_Text_Table_Decorator_Interface $decorator    Decorator for the row borders
  * @return string
  */
 public function render(array $columnWidths, Zend_Text_Table_Decorator_Interface $decorator)
 {
     // First we have to render all columns, to get the maximum height
     $renderedColumns = array();
     $maxHeight = 0;
     $colNum = 0;
     foreach ($this->_columns as $column) {
         // Get the colspan of the column
         $colSpan = $column->getColSpan();
         // Verify if there are enough column widths defined
         if ($colNum + $colSpan > count($columnWidths)) {
             throw new Zend_Text_Table_Exception('Too many columns');
         }
         // Calculate the column width
         $columnWidth = $colSpan - 1 + array_sum(array_slice($columnWidths, $colNum, $colSpan));
         // Render the column and split it's lines into an array
         $result = explode("\n", $column->render($columnWidth));
         // Store the rendered column and calculate the new max height
         $renderedColumns[] = $result;
         $maxHeight = max($maxHeight, count($result) + 1);
         // Set up the internal column number
         $colNum += $colSpan;
     }
     // If the row doesnt contain enough columns to fill the entire row, fill
     // it with an empty column
     if ($colNum < count($columnWidths)) {
         $remainingWidth = count($columnWidths) - $colNum - 1 + array_sum(array_slice($columnWidths, $colNum));
         $renderedColumns[] = str_repeat(' ', $remainingWidth);
     }
     // Add each single column line to the resul
     $result = '';
     for ($line = 0; $line < $maxHeight; $line++) {
         $result .= $decorator->getHorizontal();
         foreach ($renderedColumns as $renderedColumn) {
             if (isset($renderedColumn[$line]) === true) {
                 $result .= $renderedColumn[$line];
             } else {
                 $result .= str_repeat(' ', strlen($renderedColumn[0]));
             }
             $result .= $decorator->getHorizontal();
         }
     }
     return $result;
 }
Example #2
0
 /**
  * Render the row
  *
  * @param  array                               $columnWidths Width of all columns
  * @param  Zend_Text_Table_Decorator_Interface $decorator    Decorator for the row borders
  * @param  integer                             $padding      Padding for the columns
  * @throws Zend_Text_Table_Exception When there are too many columns
  * @return string
  */
 public function render(array $columnWidths, Zend_Text_Table_Decorator_Interface $decorator, $padding = 0)
 {
     // Prepare an array to store all column widths
     $this->_columnWidths = array();
     // If there is no single column, create a column which spans over the
     // entire row
     if (count($this->_columns) === 0) {
         require_once 'Zend/Text/Table/Column.php';
         $this->appendColumn(new Zend_Text_Table_Column(null, null, count($columnWidths)));
     }
     // First we have to render all columns, to get the maximum height
     $renderedColumns = array();
     $maxHeight = 0;
     $colNum = 0;
     foreach ($this->_columns as $column) {
         // Get the colspan of the column
         $colSpan = $column->getColSpan();
         // Verify if there are enough column widths defined
         if ($colNum + $colSpan > count($columnWidths)) {
             require_once 'Zend/Text/Table/Exception.php';
             throw new Zend_Text_Table_Exception('Too many columns');
         }
         // Calculate the column width
         $columnWidth = $colSpan - 1 + array_sum(array_slice($columnWidths, $colNum, $colSpan));
         // Render the column and split it's lines into an array
         $result = explode("\n", $column->render($columnWidth, $padding));
         // Store the width of the rendered column
         $this->_columnWidths[] = $columnWidth;
         // Store the rendered column and calculate the new max height
         $renderedColumns[] = $result;
         $maxHeight = max($maxHeight, count($result));
         // Set up the internal column number
         $colNum += $colSpan;
     }
     // If the row doesnt contain enough columns to fill the entire row, fill
     // it with an empty column
     if ($colNum < count($columnWidths)) {
         $remainingWidth = count($columnWidths) - $colNum - 1 + array_sum(array_slice($columnWidths, $colNum));
         $renderedColumns[] = array(str_repeat(' ', $remainingWidth));
         $this->_columnWidths[] = $remainingWidth;
     }
     // Add each single column line to the result
     $result = '';
     for ($line = 0; $line < $maxHeight; $line++) {
         $result .= $decorator->getVertical();
         foreach ($renderedColumns as $renderedColumn) {
             if (isset($renderedColumn[$line]) === true) {
                 $result .= $renderedColumn[$line];
             } else {
                 $result .= str_repeat(' ', strlen($renderedColumn[0]));
             }
             $result .= $decorator->getVertical();
         }
         $result .= "\n";
     }
     return $result;
 }
Example #3
0
 /**
  * Render the table
  *
  * @throws Zend_Text_Table_Exception 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) {
         require_once 'Zend/Text/Table/Exception.php';
         throw new Zend_Text_Table_Exception('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;
             } else {
                 if ($rowNum === 1 && $this->_autoSeparate & self::AUTO_SEPARATE_HEADER) {
                     $drawSeparator = true;
                 } else {
                     if ($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;
 }
Example #4
0
 /**
  * Render the table
  *
  * @throws Zend_Text_Table_Exception 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 Zend_Text_Table_Exception('No rows were added to the table yet');
     }
     // Initiate the result variable
     $result = '';
     // 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;
         }
         $columnWidths = $row->getColumnWidths($this->_columnWidths);
         $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 === $numColumns) {
                     $result .= $this->_decorator->getTopRight();
                 } else {
                     $result .= $this->_decorator->getHorizontalDown();
                 }
             }
             $result .= "\n";
         } else {
             // Else draw the row seperator
             $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 === $numColumns) {
                     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 should *never* happen!
                         break;
                 }
             }
             $result .= $this->_decorator->getVerticalLeft() . "\n";
         }
         // Add the rendered row to the result
         $result .= $row->render($this->_columnWidths, $this->_decorator) . "\n";
         // If this is the last row, draw the table bottom
         if ($rowNum === $numRows) {
             $result .= $this->_decorator->getBottomLeft();
             foreach ($columnWidths as $columnNum => $columnWidth) {
                 $result .= str_repeat($this->_decorator->getHorizontal(), $columnWidth);
                 if ($columnNum === $numColumns) {
                     $result .= $this->_decorator->getBottomRight();
                 } else {
                     $result .= $this->_decorator->getHorizontalUp();
                 }
             }
             $result .= "\n";
         }
     }
     return $result;
 }