Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Table.php
Go to the documentation of this file.
1 <?php
31 {
35  const AUTO_SEPARATE_NONE = 0x0;
36  const AUTO_SEPARATE_HEADER = 0x1;
37  const AUTO_SEPARATE_FOOTER = 0x2;
38  const AUTO_SEPARATE_ALL = 0x4;
39 
45  protected $_decorator = null;
46 
52  protected $_columnWidths = null;
53 
59  protected $_rows = array();
60 
67 
73  protected $_padding = 0;
74 
80  protected $_defaultColumnAligns = array();
81 
87  protected $_pluginLoader = null;
88 
94  protected static $_inputCharset = 'utf-8';
95 
101  protected static $_outputCharset = 'utf-8';
102 
108  protected $_skipOptions = array(
109  'options',
110  'config',
111  'defaultColumnAlign',
112  );
113 
121  public function __construct($options = null)
122  {
123  // Set options
124  if (is_array($options)) {
125  $this->setOptions($options);
126  } else if ($options instanceof Zend_Config) {
127  $this->setConfig($options);
128  }
129 
130  // Check if column widths were set
131  // @todo When column widths were not set, assume auto-sizing
132  if ($this->_columnWidths === null) {
133  #require_once 'Zend/Text/Table/Exception.php';
134  throw new Zend_Text_Table_Exception('You must define the column widths');
135  }
136 
137  // If no decorator was given, use default unicode decorator
138  if ($this->_decorator === null) {
139  if (self::getOutputCharset() === 'utf-8') {
140  $this->setDecorator('unicode');
141  } else {
142  $this->setDecorator('ascii');
143  }
144  }
145  }
146 
153  public function setOptions(array $options)
154  {
155  foreach ($options as $key => $value) {
156  if (in_array(strtolower($key), $this->_skipOptions)) {
157  continue;
158  }
159 
160  $method = 'set' . ucfirst($key);
161  if (method_exists($this, $method)) {
162  $this->$method($value);
163  }
164  }
165 
166  return $this;
167  }
168 
175  public function setConfig(Zend_Config $config)
176  {
177  return $this->setOptions($config->toArray());
178  }
179 
188  public function setColumnWidths(array $columnWidths)
189  {
190  if (count($columnWidths) === 0) {
191  #require_once 'Zend/Text/Table/Exception.php';
192  throw new Zend_Text_Table_Exception('You must supply at least one column');
193  }
194 
195  foreach ($columnWidths as $columnNum => $columnWidth) {
196  if (is_int($columnWidth) === false or $columnWidth < 1) {
197  #require_once 'Zend/Text/Table/Exception.php';
198  throw new Zend_Text_Table_Exception('Column ' . $columnNum . ' has an invalid'
199  . ' column width');
200  }
201  }
202 
203  $this->_columnWidths = $columnWidths;
204 
205  return $this;
206  }
207 
214  public function setAutoSeparate($autoSeparate)
215  {
216  $this->_autoSeparate = (int) $autoSeparate;
217  return $this;
218  }
219 
226  public function setDecorator($decorator)
227  {
228  if ($decorator instanceof Zend_Text_Table_Decorator_Interface) {
229  $this->_decorator = $decorator;
230  } else {
231  $classname = $this->getPluginLoader()->load($decorator);
232  $this->_decorator = new $classname;
233  }
234 
235  return $this;
236  }
237 
244  public function setPadding($padding)
245  {
246  $this->_padding = max(0, (int) $padding);
247  return $this;
248  }
249 
255  public function getPluginLoader()
256  {
257  if ($this->_pluginLoader === null) {
258  $prefix = 'Zend_Text_Table_Decorator_';
259  $pathPrefix = 'Zend/Text/Table/Decorator/';
260 
261  #require_once 'Zend/Loader/PluginLoader.php';
262  $this->_pluginLoader = new Zend_Loader_PluginLoader(array($prefix => $pathPrefix));
263  }
264 
265  return $this->_pluginLoader;
266  }
267 
275  public function setDefaultColumnAlign($columnNum, $align)
276  {
277  $this->_defaultColumnAligns[$columnNum] = $align;
278 
279  return $this;
280  }
281 
287  public static function setInputCharset($charset)
288  {
289  self::$_inputCharset = strtolower($charset);
290  }
291 
297  public static function getInputCharset()
298  {
299  return self::$_inputCharset;
300  }
301 
307  public static function setOutputCharset($charset)
308  {
309  self::$_outputCharset = strtolower($charset);
310  }
311 
317  public static function getOutputCharset()
318  {
319  return self::$_outputCharset;
320  }
321 
330  public function appendRow($row)
331  {
332  if (!is_array($row) && !($row instanceof Zend_Text_Table_Row)) {
333  #require_once 'Zend/Text/Table/Exception.php';
334  throw new Zend_Text_Table_Exception('$row must be an array or instance of Zend_Text_Table_Row');
335  }
336 
337  if (is_array($row)) {
338  if (count($row) > count($this->_columnWidths)) {
339  #require_once 'Zend/Text/Table/Exception.php';
340  throw new Zend_Text_Table_Exception('Row contains too many columns');
341  }
342 
343  #require_once 'Zend/Text/Table/Row.php';
344  #require_once 'Zend/Text/Table/Column.php';
345 
346  $data = $row;
347  $row = new Zend_Text_Table_Row();
348  $colNum = 0;
349  foreach ($data as $columnData) {
350  if (isset($this->_defaultColumnAligns[$colNum])) {
351  $align = $this->_defaultColumnAligns[$colNum];
352  } else {
353  $align = null;
354  }
355 
356  $row->appendColumn(new Zend_Text_Table_Column($columnData, $align));
357  $colNum++;
358  }
359  }
360 
361  $this->_rows[] = $row;
362 
363  return $this;
364  }
365 
372  public function render()
373  {
374  // There should be at least one row
375  if (count($this->_rows) === 0) {
376  #require_once 'Zend/Text/Table/Exception.php';
377  throw new Zend_Text_Table_Exception('No rows were added to the table yet');
378  }
379 
380  // Initiate the result variable
381  $result = '';
382 
383  // Count total columns
384  $totalNumColumns = count($this->_columnWidths);
385 
386  // Now render all rows, starting from the first one
387  $numRows = count($this->_rows);
388  foreach ($this->_rows as $rowNum => $row) {
389  // Get all column widths
390  if (isset($columnWidths) === true) {
391  $lastColumnWidths = $columnWidths;
392  }
393 
394  $renderedRow = $row->render($this->_columnWidths, $this->_decorator, $this->_padding);
395  $columnWidths = $row->getColumnWidths();
396  $numColumns = count($columnWidths);
397 
398  // Check what we have to draw
399  if ($rowNum === 0) {
400  // If this is the first row, draw the table top
401  $result .= $this->_decorator->getTopLeft();
402 
403  foreach ($columnWidths as $columnNum => $columnWidth) {
404  $result .= str_repeat($this->_decorator->getHorizontal(),
405  $columnWidth);
406 
407  if (($columnNum + 1) === $numColumns) {
408  $result .= $this->_decorator->getTopRight();
409  } else {
410  $result .= $this->_decorator->getHorizontalDown();
411  }
412  }
413 
414  $result .= "\n";
415  } else {
416  // Else check if we have to draw the row separator
417  if ($this->_autoSeparate & self::AUTO_SEPARATE_ALL) {
418  $drawSeparator = true;
419  } else if ($rowNum === 1 && $this->_autoSeparate & self::AUTO_SEPARATE_HEADER) {
420  $drawSeparator = true;
421  } else if ($rowNum === ($numRows - 1) && $this->_autoSeparate & self::AUTO_SEPARATE_FOOTER) {
422  $drawSeparator = true;
423  } else {
424  $drawSeparator = false;
425  }
426 
427  if ($drawSeparator) {
428  $result .= $this->_decorator->getVerticalRight();
429 
430  $currentUpperColumn = 0;
431  $currentLowerColumn = 0;
432  $currentUpperWidth = 0;
433  $currentLowerWidth = 0;
434 
435  // Loop through all column widths
436  foreach ($this->_columnWidths as $columnNum => $columnWidth) {
437  // Add the horizontal line
438  $result .= str_repeat($this->_decorator->getHorizontal(),
439  $columnWidth);
440 
441  // If this is the last line, break out
442  if (($columnNum + 1) === $totalNumColumns) {
443  break;
444  }
445 
446  // Else check, which connector style has to be used
447  $connector = 0x0;
448  $currentUpperWidth += $columnWidth;
449  $currentLowerWidth += $columnWidth;
450 
451  if ($lastColumnWidths[$currentUpperColumn] === $currentUpperWidth) {
452  $connector |= 0x1;
453  $currentUpperColumn += 1;
454  $currentUpperWidth = 0;
455  } else {
456  $currentUpperWidth += 1;
457  }
458 
459  if ($columnWidths[$currentLowerColumn] === $currentLowerWidth) {
460  $connector |= 0x2;
461  $currentLowerColumn += 1;
462  $currentLowerWidth = 0;
463  } else {
464  $currentLowerWidth += 1;
465  }
466 
467  switch ($connector) {
468  case 0x0:
469  $result .= $this->_decorator->getHorizontal();
470  break;
471 
472  case 0x1:
473  $result .= $this->_decorator->getHorizontalUp();
474  break;
475 
476  case 0x2:
477  $result .= $this->_decorator->getHorizontalDown();
478  break;
479 
480  case 0x3:
481  $result .= $this->_decorator->getCross();
482  break;
483 
484  default:
485  // This can never happen, but the CS tells I have to have it ...
486  break;
487  }
488  }
489 
490  $result .= $this->_decorator->getVerticalLeft() . "\n";
491  }
492  }
493 
494  // Add the rendered row to the result
495  $result .= $renderedRow;
496 
497  // If this is the last row, draw the table bottom
498  if (($rowNum + 1) === $numRows) {
499  $result .= $this->_decorator->getBottomLeft();
500 
501  foreach ($columnWidths as $columnNum => $columnWidth) {
502  $result .= str_repeat($this->_decorator->getHorizontal(),
503  $columnWidth);
504 
505  if (($columnNum + 1) === $numColumns) {
506  $result .= $this->_decorator->getBottomRight();
507  } else {
508  $result .= $this->_decorator->getHorizontalUp();
509  }
510  }
511 
512  $result .= "\n";
513  }
514  }
515 
516  return $result;
517  }
518 
524  public function __toString()
525  {
526  try {
527  return $this->render();
528  } catch (Exception $e) {
529  trigger_error($e->getMessage(), E_USER_ERROR);
530  }
531 
532  }
533 }
setConfig(Zend_Config $config)
Definition: Table.php:175
$_defaultColumnAligns
Definition: Table.php:80
setColumnWidths(array $columnWidths)
Definition: Table.php:188
$config
Definition: fraud_order.php:17
setDecorator($decorator)
Definition: Table.php:226
static getInputCharset()
Definition: Table.php:297
static $_outputCharset
Definition: Table.php:101
const AUTO_SEPARATE_HEADER
Definition: Table.php:36
setOptions(array $options)
Definition: Table.php:153
setAutoSeparate($autoSeparate)
Definition: Table.php:214
$prefix
Definition: name.phtml:25
const AUTO_SEPARATE_NONE
Definition: Table.php:35
static $_inputCharset
Definition: Table.php:94
$value
Definition: gender.phtml:16
$numColumns
Definition: grid.phtml:12
static setInputCharset($charset)
Definition: Table.php:287
static getOutputCharset()
Definition: Table.php:317
$method
Definition: info.phtml:13
static setOutputCharset($charset)
Definition: Table.php:307
const AUTO_SEPARATE_ALL
Definition: Table.php:38
setDefaultColumnAlign($columnNum, $align)
Definition: Table.php:275
__construct($options=null)
Definition: Table.php:121
setPadding($padding)
Definition: Table.php:244
const AUTO_SEPARATE_FOOTER
Definition: Table.php:37
appendRow($row)
Definition: Table.php:330