File indexing completed on 2024-12-22 05:37:08
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Text_Table 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * Row class for Zend_Text_Table 0024 * 0025 * @category Zend 0026 * @package Zend_Text_Table 0027 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0028 * @license http://framework.zend.com/license/new-bsd New BSD License 0029 */ 0030 class Zend_Text_Table_Row 0031 { 0032 /** 0033 * List of all columns 0034 * 0035 * @var array 0036 */ 0037 protected $_columns = array(); 0038 0039 /** 0040 * Temporary stored column widths 0041 * 0042 * @var array 0043 */ 0044 protected $_columnWidths = null; 0045 0046 /** 0047 * Create a new column and append it to the row 0048 * 0049 * @param string $content 0050 * @param array $options 0051 * @return Zend_Text_Table_Row 0052 */ 0053 public function createColumn($content, array $options = null) 0054 { 0055 $align = null; 0056 $colSpan = null; 0057 $encoding = null; 0058 0059 if ($options !== null) { 0060 extract($options, EXTR_IF_EXISTS); 0061 } 0062 0063 // require_once 'Zend/Text/Table/Column.php'; 0064 0065 $column = new Zend_Text_Table_Column($content, $align, $colSpan, $encoding); 0066 0067 $this->appendColumn($column); 0068 0069 return $this; 0070 } 0071 0072 /** 0073 * Append a column to the row 0074 * 0075 * @param Zend_Text_Table_Column $column The column to append to the row 0076 * @return Zend_Text_Table_Row 0077 */ 0078 public function appendColumn(Zend_Text_Table_Column $column) 0079 { 0080 $this->_columns[] = $column; 0081 0082 return $this; 0083 } 0084 0085 /** 0086 * Get a column by it's index 0087 * 0088 * Returns null, when the index is out of range 0089 * 0090 * @param integer $index 0091 * @return Zend_Text_Table_Column|null 0092 */ 0093 public function getColumn($index) 0094 { 0095 if (!isset($this->_columns[$index])) { 0096 return null; 0097 } 0098 0099 return $this->_columns[$index]; 0100 } 0101 0102 /** 0103 * Get all columns of the row 0104 * 0105 * @return array 0106 */ 0107 public function getColumns() 0108 { 0109 return $this->_columns; 0110 } 0111 0112 /** 0113 * Get the widths of all columns, which were rendered last 0114 * 0115 * @throws Zend_Text_Table_Exception When no columns were rendered yet 0116 * @return integer 0117 */ 0118 public function getColumnWidths() 0119 { 0120 if ($this->_columnWidths === null) { 0121 // require_once 'Zend/Text/Table/Exception.php'; 0122 throw new Zend_Text_Table_Exception('No columns were rendered yet'); 0123 } 0124 0125 return $this->_columnWidths; 0126 } 0127 0128 /** 0129 * Render the row 0130 * 0131 * @param array $columnWidths Width of all columns 0132 * @param Zend_Text_Table_Decorator_Interface $decorator Decorator for the row borders 0133 * @param integer $padding Padding for the columns 0134 * @throws Zend_Text_Table_Exception When there are too many columns 0135 * @return string 0136 */ 0137 public function render(array $columnWidths, 0138 Zend_Text_Table_Decorator_Interface $decorator, 0139 $padding = 0) 0140 { 0141 // Prepare an array to store all column widths 0142 $this->_columnWidths = array(); 0143 0144 // If there is no single column, create a column which spans over the 0145 // entire row 0146 if (count($this->_columns) === 0) { 0147 // require_once 'Zend/Text/Table/Column.php'; 0148 $this->appendColumn(new Zend_Text_Table_Column(null, null, count($columnWidths))); 0149 } 0150 0151 // First we have to render all columns, to get the maximum height 0152 $renderedColumns = array(); 0153 $maxHeight = 0; 0154 $colNum = 0; 0155 foreach ($this->_columns as $column) { 0156 // Get the colspan of the column 0157 $colSpan = $column->getColSpan(); 0158 0159 // Verify if there are enough column widths defined 0160 if (($colNum + $colSpan) > count($columnWidths)) { 0161 // require_once 'Zend/Text/Table/Exception.php'; 0162 throw new Zend_Text_Table_Exception('Too many columns'); 0163 } 0164 0165 // Calculate the column width 0166 $columnWidth = ($colSpan - 1 + array_sum(array_slice($columnWidths, 0167 $colNum, 0168 $colSpan))); 0169 0170 // Render the column and split it's lines into an array 0171 $result = explode("\n", $column->render($columnWidth, $padding)); 0172 0173 // Store the width of the rendered column 0174 $this->_columnWidths[] = $columnWidth; 0175 0176 // Store the rendered column and calculate the new max height 0177 $renderedColumns[] = $result; 0178 $maxHeight = max($maxHeight, count($result)); 0179 0180 // Set up the internal column number 0181 $colNum += $colSpan; 0182 } 0183 0184 // If the row doesnt contain enough columns to fill the entire row, fill 0185 // it with an empty column 0186 if ($colNum < count($columnWidths)) { 0187 $remainingWidth = (count($columnWidths) - $colNum - 1) + 0188 array_sum(array_slice($columnWidths, 0189 $colNum)); 0190 $renderedColumns[] = array(str_repeat(' ', $remainingWidth)); 0191 0192 $this->_columnWidths[] = $remainingWidth; 0193 } 0194 0195 // Add each single column line to the result 0196 $result = ''; 0197 for ($line = 0; $line < $maxHeight; $line++) { 0198 $result .= $decorator->getVertical(); 0199 0200 foreach ($renderedColumns as $index => $renderedColumn) { 0201 if (isset($renderedColumn[$line]) === true) { 0202 $result .= $renderedColumn[$line]; 0203 } else { 0204 $result .= str_repeat(' ', $this->_columnWidths[$index]); 0205 } 0206 0207 $result .= $decorator->getVertical(); 0208 } 0209 0210 $result .= "\n"; 0211 } 0212 0213 return $result; 0214 } 0215 }