Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Data Fields | Protected Attributes | Static Protected Attributes
Zend_Text_Table Class Reference

Public Member Functions

 __construct ($options=null)
 
 setOptions (array $options)
 
 setConfig (Zend_Config $config)
 
 setColumnWidths (array $columnWidths)
 
 setAutoSeparate ($autoSeparate)
 
 setDecorator ($decorator)
 
 setPadding ($padding)
 
 getPluginLoader ()
 
 setDefaultColumnAlign ($columnNum, $align)
 
 appendRow ($row)
 
 render ()
 
 __toString ()
 

Static Public Member Functions

static setInputCharset ($charset)
 
static getInputCharset ()
 
static setOutputCharset ($charset)
 
static getOutputCharset ()
 

Data Fields

const AUTO_SEPARATE_NONE = 0x0
 
const AUTO_SEPARATE_HEADER = 0x1
 
const AUTO_SEPARATE_FOOTER = 0x2
 
const AUTO_SEPARATE_ALL = 0x4
 

Protected Attributes

 $_decorator = null
 
 $_columnWidths = null
 
 $_rows = array()
 
 $_autoSeparate = self::AUTO_SEPARATE_ALL
 
 $_padding = 0
 
 $_defaultColumnAligns = array()
 
 $_pluginLoader = null
 
 $_skipOptions
 

Static Protected Attributes

static $_inputCharset = 'utf-8'
 
static $_outputCharset = 'utf-8'
 

Detailed Description

Definition at line 30 of file Table.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options = null)

Create a basic table object

Parameters
array$columnsWidthsList of all column widths
Zend_Config | array$optionsConfiguration options
Exceptions
Zend_Text_Table_ExceptionWhen no columns widths were set

Definition at line 121 of file Table.php.

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  }
setConfig(Zend_Config $config)
Definition: Table.php:175
setDecorator($decorator)
Definition: Table.php:226
setOptions(array $options)
Definition: Table.php:153

Member Function Documentation

◆ __toString()

__toString ( )

Magic method which returns the rendered table

Returns
string

Definition at line 524 of file Table.php.

525  {
526  try {
527  return $this->render();
528  } catch (Exception $e) {
529  trigger_error($e->getMessage(), E_USER_ERROR);
530  }
531 
532  }

◆ appendRow()

appendRow (   $row)

Append a row to the table

Parameters
array | Zend_Text_Table_Row$rowThe row to append to the table
Exceptions
Zend_Text_Table_ExceptionWhen $row is neither an array nor Zend_Zext_Table_Row
Zend_Text_Table_ExceptionWhen a row contains too many columns
Returns
Zend_Text_Table

Definition at line 330 of file Table.php.

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  }

◆ getInputCharset()

static getInputCharset ( )
static

Get the input charset for column contents

Parameters
string$charset

Definition at line 297 of file Table.php.

298  {
299  return self::$_inputCharset;
300  }
static $_inputCharset
Definition: Table.php:94

◆ getOutputCharset()

static getOutputCharset ( )
static

Get the output charset for column contents

Parameters
string$charset

Definition at line 317 of file Table.php.

318  {
319  return self::$_outputCharset;
320  }
static $_outputCharset
Definition: Table.php:101

◆ getPluginLoader()

getPluginLoader ( )

Get the plugin loader for decorators

Returns
Zend_Loader_PluginLoader

Definition at line 255 of file Table.php.

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  }
$prefix
Definition: name.phtml:25

◆ render()

render ( )

Render the table

Exceptions
Zend_Text_Table_ExceptionWhen no rows were added to the table
Returns
string

Definition at line 372 of file Table.php.

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  }
$numColumns
Definition: grid.phtml:12

◆ setAutoSeparate()

setAutoSeparate (   $autoSeparate)

Set auto separation mode

Parameters
integer$autoSeparateAuto separation mode
Returns
Zend_Text_Table

Definition at line 214 of file Table.php.

215  {
216  $this->_autoSeparate = (int) $autoSeparate;
217  return $this;
218  }

◆ setColumnWidths()

setColumnWidths ( array  $columnWidths)

Set column widths

Parameters
array$columnWidthsWidths of all columns
Exceptions
Zend_Text_Table_ExceptionWhen no columns were supplied
Zend_Text_Table_ExceptionWhen a column has an invalid width
Returns
Zend_Text_Table

Definition at line 188 of file Table.php.

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  }

◆ setConfig()

setConfig ( Zend_Config  $config)

Set options from config object

Parameters
Zend_Config$configConfiguration for Zend_Text_Table
Returns
Zend_Text_Table

Definition at line 175 of file Table.php.

176  {
177  return $this->setOptions($config->toArray());
178  }
setOptions(array $options)
Definition: Table.php:153

◆ setDecorator()

setDecorator (   $decorator)

Set decorator

Parameters
Zend_Text_Table_Decorator_Interface | string$decoratorDecorator to use
Returns
Zend_Text_Table

Definition at line 226 of file Table.php.

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  }

◆ setDefaultColumnAlign()

setDefaultColumnAlign (   $columnNum,
  $align 
)

Set default column align for rows created by appendRow(array $data)

Parameters
integer$columnNum
string$align
Returns
Zend_Text_Table

Definition at line 275 of file Table.php.

276  {
277  $this->_defaultColumnAligns[$columnNum] = $align;
278 
279  return $this;
280  }

◆ setInputCharset()

static setInputCharset (   $charset)
static

Set the input charset for column contents

Parameters
string$charset

Definition at line 287 of file Table.php.

288  {
289  self::$_inputCharset = strtolower($charset);
290  }

◆ setOptions()

setOptions ( array  $options)

Set options from array

Parameters
array$optionsConfiguration for Zend_Text_Table
Returns
Zend_Text_Table

Definition at line 153 of file Table.php.

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  }
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13

◆ setOutputCharset()

static setOutputCharset (   $charset)
static

Set the output charset for column contents

Parameters
string$charset

Definition at line 307 of file Table.php.

308  {
309  self::$_outputCharset = strtolower($charset);
310  }

◆ setPadding()

setPadding (   $padding)

Set the column padding

Parameters
integer$paddingThe padding for the columns
Returns
Zend_Text_Table

Definition at line 244 of file Table.php.

245  {
246  $this->_padding = max(0, (int) $padding);
247  return $this;
248  }

Field Documentation

◆ $_autoSeparate

$_autoSeparate = self::AUTO_SEPARATE_ALL
protected

Definition at line 66 of file Table.php.

◆ $_columnWidths

$_columnWidths = null
protected

Definition at line 52 of file Table.php.

◆ $_decorator

$_decorator = null
protected

Definition at line 45 of file Table.php.

◆ $_defaultColumnAligns

$_defaultColumnAligns = array()
protected

Definition at line 80 of file Table.php.

◆ $_inputCharset

$_inputCharset = 'utf-8'
staticprotected

Definition at line 94 of file Table.php.

◆ $_outputCharset

$_outputCharset = 'utf-8'
staticprotected

Definition at line 101 of file Table.php.

◆ $_padding

$_padding = 0
protected

Definition at line 73 of file Table.php.

◆ $_pluginLoader

$_pluginLoader = null
protected

Definition at line 87 of file Table.php.

◆ $_rows

$_rows = array()
protected

Definition at line 59 of file Table.php.

◆ $_skipOptions

$_skipOptions
protected
Initial value:
= array(
'options',
'config',
'defaultColumnAlign',
)

Definition at line 108 of file Table.php.

◆ AUTO_SEPARATE_ALL

const AUTO_SEPARATE_ALL = 0x4

Definition at line 38 of file Table.php.

◆ AUTO_SEPARATE_FOOTER

const AUTO_SEPARATE_FOOTER = 0x2

Definition at line 37 of file Table.php.

◆ AUTO_SEPARATE_HEADER

const AUTO_SEPARATE_HEADER = 0x1

Definition at line 36 of file Table.php.

◆ AUTO_SEPARATE_NONE

const AUTO_SEPARATE_NONE = 0x0

Auto seperator settings

Definition at line 35 of file Table.php.


The documentation for this class was generated from the following file: