PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Text/Table/Column.php

https://gitlab.com/devtoannh/cafe
PHP | 243 lines | 102 code | 35 blank | 106 comment | 13 complexity | 1b4f8d9a2b3cdea28b5e04e90fd8c987 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Text_Table
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Column.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Text_Table
  23. */
  24. require_once 'Zend/Text/Table.php';
  25. /**
  26. * @see Zend_Text_MultiByte
  27. */
  28. require_once 'Zend/Text/MultiByte.php';
  29. /**
  30. * Column class for Zend_Text_Table_Row
  31. *
  32. * @category Zend
  33. * @package Zend_Text_Table
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Text_Table_Column
  38. {
  39. /**
  40. * Aligns for columns
  41. */
  42. const ALIGN_LEFT = 'left';
  43. const ALIGN_CENTER = 'center';
  44. const ALIGN_RIGHT = 'right';
  45. /**
  46. * Content of the column
  47. *
  48. * @var string
  49. */
  50. protected $_content = '';
  51. /**
  52. * Align of the column
  53. *
  54. * @var string
  55. */
  56. protected $_align = self::ALIGN_LEFT;
  57. /**
  58. * Colspan of the column
  59. *
  60. * @var integer
  61. */
  62. protected $_colSpan = 1;
  63. /**
  64. * Allowed align parameters
  65. *
  66. * @var array
  67. */
  68. protected $_allowedAligns = array(self::ALIGN_LEFT, self::ALIGN_CENTER, self::ALIGN_RIGHT);
  69. /**
  70. * Create a column for a Zend_Text_Table_Row object.
  71. *
  72. * @param string $content The content of the column
  73. * @param string $align The align of the content
  74. * @param integer $colSpan The colspan of the column
  75. * @param string $charset The encoding of the content
  76. */
  77. public function __construct($content = null, $align = null, $colSpan = null, $charset = null)
  78. {
  79. if ($content !== null) {
  80. $this->setContent($content, $charset);
  81. }
  82. if ($align !== null) {
  83. $this->setAlign($align);
  84. }
  85. if ($colSpan !== null) {
  86. $this->setColSpan($colSpan);
  87. }
  88. }
  89. /**
  90. * Set the content.
  91. *
  92. * If $charset is not defined, it is assumed that $content is encoded in
  93. * the charset defined via Zend_Text_Table::setInputCharset() (defaults
  94. * to utf-8).
  95. *
  96. * @param string $content Content of the column
  97. * @param string $charset The charset of the content
  98. * @throws Zend_Text_Table_Exception When $content is not a string
  99. * @return Zend_Text_Table_Column
  100. */
  101. public function setContent($content, $charset = null)
  102. {
  103. if (is_string($content) === false) {
  104. require_once 'Zend/Text/Table/Exception.php';
  105. throw new Zend_Text_Table_Exception('$content must be a string');
  106. }
  107. if ($charset === null) {
  108. $inputCharset = Zend_Text_Table::getInputCharset();
  109. } else {
  110. $inputCharset = strtolower($charset);
  111. }
  112. $outputCharset = Zend_Text_Table::getOutputCharset();
  113. if ($inputCharset !== $outputCharset) {
  114. if (PHP_OS !== 'AIX') {
  115. // AIX does not understand these character sets
  116. $content = iconv($inputCharset, $outputCharset, $content);
  117. }
  118. }
  119. $this->_content = $content;
  120. return $this;
  121. }
  122. /**
  123. * Set the align
  124. *
  125. * @param string $align Align of the column
  126. * @throws Zend_Text_Table_Exception When supplied align is invalid
  127. * @return Zend_Text_Table_Column
  128. */
  129. public function setAlign($align)
  130. {
  131. if (in_array($align, $this->_allowedAligns) === false) {
  132. require_once 'Zend/Text/Table/Exception.php';
  133. throw new Zend_Text_Table_Exception('Invalid align supplied');
  134. }
  135. $this->_align = $align;
  136. return $this;
  137. }
  138. /**
  139. * Set the colspan
  140. *
  141. * @param int $colSpan
  142. * @throws Zend_Text_Table_Exception When $colSpan is smaller than 1
  143. * @return Zend_Text_Table_Column
  144. */
  145. public function setColSpan($colSpan)
  146. {
  147. if (is_int($colSpan) === false or $colSpan < 1) {
  148. require_once 'Zend/Text/Table/Exception.php';
  149. throw new Zend_Text_Table_Exception('$colSpan must be an integer and greater than 0');
  150. }
  151. $this->_colSpan = $colSpan;
  152. return $this;
  153. }
  154. /**
  155. * Get the colspan
  156. *
  157. * @return integer
  158. */
  159. public function getColSpan()
  160. {
  161. return $this->_colSpan;
  162. }
  163. /**
  164. * Render the column width the given column width
  165. *
  166. * @param integer $columnWidth The width of the column
  167. * @param integer $padding The padding for the column
  168. * @throws Zend_Text_Table_Exception When $columnWidth is lower than 1
  169. * @throws Zend_Text_Table_Exception When padding is greater than columnWidth
  170. * @return string
  171. */
  172. public function render($columnWidth, $padding = 0)
  173. {
  174. if (is_int($columnWidth) === false or $columnWidth < 1) {
  175. require_once 'Zend/Text/Table/Exception.php';
  176. throw new Zend_Text_Table_Exception('$columnWidth must be an integer and greater than 0');
  177. }
  178. $columnWidth -= ($padding * 2);
  179. if ($columnWidth < 1) {
  180. require_once 'Zend/Text/Table/Exception.php';
  181. throw new Zend_Text_Table_Exception('Padding (' . $padding . ') is greater than column width');
  182. }
  183. switch ($this->_align) {
  184. case self::ALIGN_LEFT:
  185. $padMode = STR_PAD_RIGHT;
  186. break;
  187. case self::ALIGN_CENTER:
  188. $padMode = STR_PAD_BOTH;
  189. break;
  190. case self::ALIGN_RIGHT:
  191. $padMode = STR_PAD_LEFT;
  192. break;
  193. default:
  194. // This can never happen, but the CS tells I have to have it ...
  195. break;
  196. }
  197. $outputCharset = Zend_Text_Table::getOutputCharset();
  198. $lines = explode("\n", Zend_Text_MultiByte::wordWrap($this->_content, $columnWidth, "\n", true, $outputCharset));
  199. $paddedLines = array();
  200. foreach ($lines AS $line) {
  201. $paddedLines[] = str_repeat(' ', $padding)
  202. . Zend_Text_MultiByte::strPad($line, $columnWidth, ' ', $padMode, $outputCharset)
  203. . str_repeat(' ', $padding);
  204. }
  205. $result = implode("\n", $paddedLines);
  206. return $result;
  207. }
  208. }