/** * Render the row * * @param array $columnWidths Width of all columns * @param Decorator $decorator Decorator for the row borders * @param integer $padding Padding for the columns * @throws Exception\OverflowException When there are too many columns * @return string */ public function render(array $columnWidths, Decorator $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) { $this->appendColumn(new 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)) { throw new Exception\OverflowException('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 $index => $renderedColumn) { if (isset($renderedColumn[$line]) === true) { $result .= $renderedColumn[$line]; } else { $result .= str_repeat(' ', $this->columnWidths[$index]); } $result .= $decorator->getVertical(); } $result .= "\n"; } return $result; }
/** * 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; }