/Zend/Text/Table/Row.php

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