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 * @see Zend_Text_Table 0024 */ 0025 // require_once 'Zend/Text/Table.php'; 0026 0027 /** 0028 * @see Zend_Text_MultiByte 0029 */ 0030 // require_once 'Zend/Text/MultiByte.php'; 0031 0032 /** 0033 * Column class for Zend_Text_Table_Row 0034 * 0035 * @category Zend 0036 * @package Zend_Text_Table 0037 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0038 * @license http://framework.zend.com/license/new-bsd New BSD License 0039 */ 0040 class Zend_Text_Table_Column 0041 { 0042 /** 0043 * Aligns for columns 0044 */ 0045 const ALIGN_LEFT = 'left'; 0046 const ALIGN_CENTER = 'center'; 0047 const ALIGN_RIGHT = 'right'; 0048 0049 /** 0050 * Content of the column 0051 * 0052 * @var string 0053 */ 0054 protected $_content = ''; 0055 0056 /** 0057 * Align of the column 0058 * 0059 * @var string 0060 */ 0061 protected $_align = self::ALIGN_LEFT; 0062 0063 /** 0064 * Colspan of the column 0065 * 0066 * @var integer 0067 */ 0068 protected $_colSpan = 1; 0069 0070 /** 0071 * Allowed align parameters 0072 * 0073 * @var array 0074 */ 0075 protected $_allowedAligns = array(self::ALIGN_LEFT, self::ALIGN_CENTER, self::ALIGN_RIGHT); 0076 0077 /** 0078 * Create a column for a Zend_Text_Table_Row object. 0079 * 0080 * @param string $content The content of the column 0081 * @param string $align The align of the content 0082 * @param integer $colSpan The colspan of the column 0083 * @param string $charset The encoding of the content 0084 */ 0085 public function __construct($content = null, $align = null, $colSpan = null, $charset = null) 0086 { 0087 if ($content !== null) { 0088 $this->setContent($content, $charset); 0089 } 0090 0091 if ($align !== null) { 0092 $this->setAlign($align); 0093 } 0094 0095 if ($colSpan !== null) { 0096 $this->setColSpan($colSpan); 0097 } 0098 } 0099 0100 /** 0101 * Set the content. 0102 * 0103 * If $charset is not defined, it is assumed that $content is encoded in 0104 * the charset defined via Zend_Text_Table::setInputCharset() (defaults 0105 * to utf-8). 0106 * 0107 * @param string $content Content of the column 0108 * @param string $charset The charset of the content 0109 * @throws Zend_Text_Table_Exception When $content is not a string 0110 * @return Zend_Text_Table_Column 0111 */ 0112 public function setContent($content, $charset = null) 0113 { 0114 if (is_string($content) === false) { 0115 // require_once 'Zend/Text/Table/Exception.php'; 0116 throw new Zend_Text_Table_Exception('$content must be a string'); 0117 } 0118 0119 if ($charset === null) { 0120 $inputCharset = Zend_Text_Table::getInputCharset(); 0121 } else { 0122 $inputCharset = strtolower($charset); 0123 } 0124 0125 $outputCharset = Zend_Text_Table::getOutputCharset(); 0126 0127 if ($inputCharset !== $outputCharset) { 0128 if (PHP_OS !== 'AIX') { 0129 // AIX does not understand these character sets 0130 $content = iconv($inputCharset, $outputCharset, $content); 0131 } 0132 0133 } 0134 0135 $this->_content = $content; 0136 0137 return $this; 0138 } 0139 0140 /** 0141 * Set the align 0142 * 0143 * @param string $align Align of the column 0144 * @throws Zend_Text_Table_Exception When supplied align is invalid 0145 * @return Zend_Text_Table_Column 0146 */ 0147 public function setAlign($align) 0148 { 0149 if (in_array($align, $this->_allowedAligns) === false) { 0150 // require_once 'Zend/Text/Table/Exception.php'; 0151 throw new Zend_Text_Table_Exception('Invalid align supplied'); 0152 } 0153 0154 $this->_align = $align; 0155 0156 return $this; 0157 } 0158 0159 /** 0160 * Set the colspan 0161 * 0162 * @param int $colSpan 0163 * @throws Zend_Text_Table_Exception When $colSpan is smaller than 1 0164 * @return Zend_Text_Table_Column 0165 */ 0166 public function setColSpan($colSpan) 0167 { 0168 if (is_int($colSpan) === false or $colSpan < 1) { 0169 // require_once 'Zend/Text/Table/Exception.php'; 0170 throw new Zend_Text_Table_Exception('$colSpan must be an integer and greater than 0'); 0171 } 0172 0173 $this->_colSpan = $colSpan; 0174 0175 return $this; 0176 } 0177 0178 /** 0179 * Get the colspan 0180 * 0181 * @return integer 0182 */ 0183 public function getColSpan() 0184 { 0185 return $this->_colSpan; 0186 } 0187 0188 /** 0189 * Render the column width the given column width 0190 * 0191 * @param integer $columnWidth The width of the column 0192 * @param integer $padding The padding for the column 0193 * @throws Zend_Text_Table_Exception When $columnWidth is lower than 1 0194 * @throws Zend_Text_Table_Exception When padding is greater than columnWidth 0195 * @return string 0196 */ 0197 public function render($columnWidth, $padding = 0) 0198 { 0199 if (is_int($columnWidth) === false or $columnWidth < 1) { 0200 // require_once 'Zend/Text/Table/Exception.php'; 0201 throw new Zend_Text_Table_Exception('$columnWidth must be an integer and greater than 0'); 0202 } 0203 0204 $columnWidth -= ($padding * 2); 0205 0206 if ($columnWidth < 1) { 0207 // require_once 'Zend/Text/Table/Exception.php'; 0208 throw new Zend_Text_Table_Exception('Padding (' . $padding . ') is greater than column width'); 0209 } 0210 0211 switch ($this->_align) { 0212 case self::ALIGN_LEFT: 0213 $padMode = STR_PAD_RIGHT; 0214 break; 0215 0216 case self::ALIGN_CENTER: 0217 $padMode = STR_PAD_BOTH; 0218 break; 0219 0220 case self::ALIGN_RIGHT: 0221 $padMode = STR_PAD_LEFT; 0222 break; 0223 0224 default: 0225 // This can never happen, but the CS tells I have to have it ... 0226 break; 0227 } 0228 0229 $outputCharset = Zend_Text_Table::getOutputCharset(); 0230 $lines = explode("\n", Zend_Text_MultiByte::wordWrap($this->_content, $columnWidth, "\n", true, $outputCharset)); 0231 $paddedLines = array(); 0232 0233 foreach ($lines AS $line) { 0234 $paddedLines[] = str_repeat(' ', $padding) 0235 . Zend_Text_MultiByte::strPad($line, $columnWidth, ' ', $padMode, $outputCharset) 0236 . str_repeat(' ', $padding); 0237 } 0238 0239 $result = implode("\n", $paddedLines); 0240 0241 return $result; 0242 } 0243 }