/pimcore/lib/Zend/Text/Table/Row.php

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