Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Row.php
Go to the documentation of this file.
1 <?php
31 {
37  protected $_columns = array();
38 
44  protected $_columnWidths = null;
45 
53  public function createColumn($content, array $options = null)
54  {
55  $align = null;
56  $colSpan = null;
57  $encoding = null;
58 
59  if ($options !== null) {
60  extract($options, EXTR_IF_EXISTS);
61  }
62 
63  #require_once 'Zend/Text/Table/Column.php';
64 
65  $column = new Zend_Text_Table_Column($content, $align, $colSpan, $encoding);
66 
67  $this->appendColumn($column);
68 
69  return $this;
70  }
71 
78  public function appendColumn(Zend_Text_Table_Column $column)
79  {
80  $this->_columns[] = $column;
81 
82  return $this;
83  }
84 
93  public function getColumn($index)
94  {
95  if (!isset($this->_columns[$index])) {
96  return null;
97  }
98 
99  return $this->_columns[$index];
100  }
101 
107  public function getColumns()
108  {
109  return $this->_columns;
110  }
111 
118  public function getColumnWidths()
119  {
120  if ($this->_columnWidths === null) {
121  #require_once 'Zend/Text/Table/Exception.php';
122  throw new Zend_Text_Table_Exception('No columns were rendered yet');
123  }
124 
125  return $this->_columnWidths;
126  }
127 
137  public function render(array $columnWidths,
139  $padding = 0)
140  {
141  // Prepare an array to store all column widths
142  $this->_columnWidths = array();
143 
144  // If there is no single column, create a column which spans over the
145  // entire row
146  if (count($this->_columns) === 0) {
147  #require_once 'Zend/Text/Table/Column.php';
148  $this->appendColumn(new Zend_Text_Table_Column(null, null, count($columnWidths)));
149  }
150 
151  // First we have to render all columns, to get the maximum height
152  $renderedColumns = array();
153  $maxHeight = 0;
154  $colNum = 0;
155  foreach ($this->_columns as $column) {
156  // Get the colspan of the column
157  $colSpan = $column->getColSpan();
158 
159  // Verify if there are enough column widths defined
160  if (($colNum + $colSpan) > count($columnWidths)) {
161  #require_once 'Zend/Text/Table/Exception.php';
162  throw new Zend_Text_Table_Exception('Too many columns');
163  }
164 
165  // Calculate the column width
166  $columnWidth = ($colSpan - 1 + array_sum(array_slice($columnWidths,
167  $colNum,
168  $colSpan)));
169 
170  // Render the column and split it's lines into an array
171  $result = explode("\n", $column->render($columnWidth, $padding));
172 
173  // Store the width of the rendered column
174  $this->_columnWidths[] = $columnWidth;
175 
176  // Store the rendered column and calculate the new max height
177  $renderedColumns[] = $result;
178  $maxHeight = max($maxHeight, count($result));
179 
180  // Set up the internal column number
181  $colNum += $colSpan;
182  }
183 
184  // If the row doesnt contain enough columns to fill the entire row, fill
185  // it with an empty column
186  if ($colNum < count($columnWidths)) {
187  $remainingWidth = (count($columnWidths) - $colNum - 1) +
188  array_sum(array_slice($columnWidths,
189  $colNum));
190  $renderedColumns[] = array(str_repeat(' ', $remainingWidth));
191 
192  $this->_columnWidths[] = $remainingWidth;
193  }
194 
195  // Add each single column line to the result
196  $result = '';
197  for ($line = 0; $line < $maxHeight; $line++) {
198  $result .= $decorator->getVertical();
199 
200  foreach ($renderedColumns as $index => $renderedColumn) {
201  if (isset($renderedColumn[$line]) === true) {
202  $result .= $renderedColumn[$line];
203  } else {
204  $result .= str_repeat(' ', $this->_columnWidths[$index]);
205  }
206 
207  $result .= $decorator->getVertical();
208  }
209 
210  $result .= "\n";
211  }
212 
213  return $result;
214  }
215 }
render(array $columnWidths, Zend_Text_Table_Decorator_Interface $decorator, $padding=0)
Definition: Row.php:137
getColumn($index)
Definition: Row.php:93
createColumn($content, array $options=null)
Definition: Row.php:53
appendColumn(Zend_Text_Table_Column $column)
Definition: Row.php:78
$index
Definition: list.phtml:44