/library/Zend/Text/Table/Row.php

https://github.com/snippet/zf2 · PHP · 217 lines · 89 code · 31 blank · 97 comment · 9 complexity · e1b82ee2879bd6d730b36dd39f38eeba 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Text\Table;
  25. /**
  26. * Row class for Zend_Text_Table
  27. *
  28. * @uses \Zend\Text\Table\Column
  29. * @uses \Zend\Text\Table\Exception
  30. * @category Zend
  31. * @package Zend_Text_Table
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Row
  36. {
  37. /**
  38. * List of all columns
  39. *
  40. * @var array
  41. */
  42. protected $_columns = array();
  43. /**
  44. * Temporary stored column widths
  45. *
  46. * @var array
  47. */
  48. protected $_columnWidths = null;
  49. /**
  50. * Create a new column and append it to the row
  51. *
  52. * @param string $content
  53. * @param array $options
  54. * @return \Zend\Text\Table\Row
  55. */
  56. public function createColumn($content, array $options = null)
  57. {
  58. $align = null;
  59. $colSpan = null;
  60. $encoding = null;
  61. if ($options !== null) {
  62. extract($options, EXTR_IF_EXISTS);
  63. }
  64. $column = new Column($content, $align, $colSpan, $encoding);
  65. $this->appendColumn($column);
  66. return $this;
  67. }
  68. /**
  69. * Append a column to the row
  70. *
  71. * @param \Zend\Text\Table\Column $column The column to append to the row
  72. * @return \Zend\Text\Table\Row
  73. */
  74. public function appendColumn(Column $column)
  75. {
  76. $this->_columns[] = $column;
  77. return $this;
  78. }
  79. /**
  80. * Get a column by it's index
  81. *
  82. * Returns null, when the index is out of range
  83. *
  84. * @param integer $index
  85. * @return \Zend\Text\Table\Column|null
  86. */
  87. public function getColumn($index)
  88. {
  89. if (!isset($this->_columns[$index])) {
  90. return null;
  91. }
  92. return $this->_columns[$index];
  93. }
  94. /**
  95. * Get all columns of the row
  96. *
  97. * @return array
  98. */
  99. public function getColumns()
  100. {
  101. return $this->_columns;
  102. }
  103. /**
  104. * Get the widths of all columns, which were rendered last
  105. *
  106. * @throws \Zend\Text\Table\Exception When no columns were rendered yet
  107. * @return integer
  108. */
  109. public function getColumnWidths()
  110. {
  111. if ($this->_columnWidths === null) {
  112. throw new Exception('No columns were rendered yet');
  113. }
  114. return $this->_columnWidths;
  115. }
  116. /**
  117. * Render the row
  118. *
  119. * @param array $columnWidths Width of all columns
  120. * @param \Zend\Text\Table\Decorator\DecoratorInterface $decorator Decorator for the row borders
  121. * @param integer $padding Padding for the columns
  122. * @throws \Zend\Text\Table\Exception When there are too many columns
  123. * @return string
  124. */
  125. public function render(array $columnWidths,
  126. Decorator\DecoratorInterface $decorator,
  127. $padding = 0)
  128. {
  129. // Prepare an array to store all column widths
  130. $this->_columnWidths = array();
  131. // If there is no single column, create a column which spans over the
  132. // entire row
  133. if (count($this->_columns) === 0) {
  134. $this->appendColumn(new Column(null, null, count($columnWidths)));
  135. }
  136. // First we have to render all columns, to get the maximum height
  137. $renderedColumns = array();
  138. $maxHeight = 0;
  139. $colNum = 0;
  140. foreach ($this->_columns as $column) {
  141. // Get the colspan of the column
  142. $colSpan = $column->getColSpan();
  143. // Verify if there are enough column widths defined
  144. if (($colNum + $colSpan) > count($columnWidths)) {
  145. throw new Exception('Too many columns');
  146. }
  147. // Calculate the column width
  148. $columnWidth = ($colSpan - 1 + array_sum(array_slice($columnWidths,
  149. $colNum,
  150. $colSpan)));
  151. // Render the column and split it's lines into an array
  152. $result = explode("\n", $column->render($columnWidth, $padding));
  153. // Store the width of the rendered column
  154. $this->_columnWidths[] = $columnWidth;
  155. // Store the rendered column and calculate the new max height
  156. $renderedColumns[] = $result;
  157. $maxHeight = max($maxHeight, count($result));
  158. // Set up the internal column number
  159. $colNum += $colSpan;
  160. }
  161. // If the row doesnt contain enough columns to fill the entire row, fill
  162. // it with an empty column
  163. if ($colNum < count($columnWidths)) {
  164. $remainingWidth = (count($columnWidths) - $colNum - 1) +
  165. array_sum(array_slice($columnWidths,
  166. $colNum));
  167. $renderedColumns[] = array(str_repeat(' ', $remainingWidth));
  168. $this->_columnWidths[] = $remainingWidth;
  169. }
  170. // Add each single column line to the result
  171. $result = '';
  172. for ($line = 0; $line < $maxHeight; $line++) {
  173. $result .= $decorator->getVertical();
  174. foreach ($renderedColumns as $renderedColumn) {
  175. if (isset($renderedColumn[$line]) === true) {
  176. $result .= $renderedColumn[$line];
  177. } else {
  178. $result .= str_repeat(' ', strlen($renderedColumn[0]));
  179. }
  180. $result .= $decorator->getVertical();
  181. }
  182. $result .= "\n";
  183. }
  184. return $result;
  185. }
  186. }