Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Column.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Text/Table.php';
26 
30 #require_once 'Zend/Text/MultiByte.php';
31 
41 {
45  const ALIGN_LEFT = 'left';
46  const ALIGN_CENTER = 'center';
47  const ALIGN_RIGHT = 'right';
48 
54  protected $_content = '';
55 
62 
68  protected $_colSpan = 1;
69 
75  protected $_allowedAligns = array(self::ALIGN_LEFT, self::ALIGN_CENTER, self::ALIGN_RIGHT);
76 
85  public function __construct($content = null, $align = null, $colSpan = null, $charset = null)
86  {
87  if ($content !== null) {
88  $this->setContent($content, $charset);
89  }
90 
91  if ($align !== null) {
92  $this->setAlign($align);
93  }
94 
95  if ($colSpan !== null) {
96  $this->setColSpan($colSpan);
97  }
98  }
99 
112  public function setContent($content, $charset = null)
113  {
114  if (is_string($content) === false) {
115  #require_once 'Zend/Text/Table/Exception.php';
116  throw new Zend_Text_Table_Exception('$content must be a string');
117  }
118 
119  if ($charset === null) {
120  $inputCharset = Zend_Text_Table::getInputCharset();
121  } else {
122  $inputCharset = strtolower($charset);
123  }
124 
125  $outputCharset = Zend_Text_Table::getOutputCharset();
126 
127  if ($inputCharset !== $outputCharset) {
128  if (PHP_OS !== 'AIX') {
129  // AIX does not understand these character sets
130  $content = iconv($inputCharset, $outputCharset, $content);
131  }
132 
133  }
134 
135  $this->_content = $content;
136 
137  return $this;
138  }
139 
147  public function setAlign($align)
148  {
149  if (in_array($align, $this->_allowedAligns) === false) {
150  #require_once 'Zend/Text/Table/Exception.php';
151  throw new Zend_Text_Table_Exception('Invalid align supplied');
152  }
153 
154  $this->_align = $align;
155 
156  return $this;
157  }
158 
166  public function setColSpan($colSpan)
167  {
168  if (is_int($colSpan) === false or $colSpan < 1) {
169  #require_once 'Zend/Text/Table/Exception.php';
170  throw new Zend_Text_Table_Exception('$colSpan must be an integer and greater than 0');
171  }
172 
173  $this->_colSpan = $colSpan;
174 
175  return $this;
176  }
177 
183  public function getColSpan()
184  {
185  return $this->_colSpan;
186  }
187 
197  public function render($columnWidth, $padding = 0)
198  {
199  if (is_int($columnWidth) === false or $columnWidth < 1) {
200  #require_once 'Zend/Text/Table/Exception.php';
201  throw new Zend_Text_Table_Exception('$columnWidth must be an integer and greater than 0');
202  }
203 
204  $columnWidth -= ($padding * 2);
205 
206  if ($columnWidth < 1) {
207  #require_once 'Zend/Text/Table/Exception.php';
208  throw new Zend_Text_Table_Exception('Padding (' . $padding . ') is greater than column width');
209  }
210 
211  switch ($this->_align) {
212  case self::ALIGN_LEFT:
213  $padMode = STR_PAD_RIGHT;
214  break;
215 
216  case self::ALIGN_CENTER:
217  $padMode = STR_PAD_BOTH;
218  break;
219 
220  case self::ALIGN_RIGHT:
221  $padMode = STR_PAD_LEFT;
222  break;
223 
224  default:
225  // This can never happen, but the CS tells I have to have it ...
226  break;
227  }
228 
229  $outputCharset = Zend_Text_Table::getOutputCharset();
230  $lines = explode("\n", Zend_Text_MultiByte::wordWrap($this->_content, $columnWidth, "\n", true, $outputCharset));
231  $paddedLines = array();
232 
233  foreach ($lines AS $line) {
234  $paddedLines[] = str_repeat(' ', $padding)
235  . Zend_Text_MultiByte::strPad($line, $columnWidth, ' ', $padMode, $outputCharset)
236  . str_repeat(' ', $padding);
237  }
238 
239  $result = implode("\n", $paddedLines);
240 
241  return $result;
242  }
243 }
render($columnWidth, $padding=0)
Definition: Column.php:197
setContent($content, $charset=null)
Definition: Column.php:112
static getInputCharset()
Definition: Table.php:297
static wordWrap($string, $width=75, $break="\n", $cut=false, $charset='utf-8')
Definition: MultiByte.php:42
__construct($content=null, $align=null, $colSpan=null, $charset=null)
Definition: Column.php:85
setColSpan($colSpan)
Definition: Column.php:166
static getOutputCharset()
Definition: Table.php:317
static strPad($input, $padLength, $padString=' ', $padType=STR_PAD_RIGHT, $charset='utf-8')
Definition: MultiByte.php:104