3 * @see https://github.com/zendframework/zend-text for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-text/blob/master/LICENSE.md New BSD License
8 namespace Zend\Text\Table
;
10 use Zend\Stdlib\StringUtils
;
13 * Column class for Zend\Text\Table\Row
20 const ALIGN_LEFT
= 'left';
21 const ALIGN_CENTER
= 'center';
22 const ALIGN_RIGHT
= 'right';
25 * Content of the column
29 protected $content = '';
36 protected $align = self
::ALIGN_LEFT
;
39 * Colspan of the column
43 protected $colSpan = 1;
46 * Allowed align parameters
50 protected $allowedAligns = [self
::ALIGN_LEFT
, self
::ALIGN_CENTER
, self
::ALIGN_RIGHT
];
53 * Create a column for a Zend\Text\Table\Row object.
55 * @param string $content The content of the column
56 * @param string $align The align of the content
57 * @param int $colSpan The colspan of the column
58 * @param string $charset The encoding of the content
60 public function __construct($content = null, $align = null, $colSpan = null, $charset = null)
62 if ($content !== null) {
63 $this->setContent($content, $charset);
66 if ($align !== null) {
67 $this->setAlign($align);
70 if ($colSpan !== null) {
71 $this->setColSpan($colSpan);
78 * If $charset is not defined, it is assumed that $content is encoded in
79 * the charset defined via Zend\Text\Table::setInputCharset() (defaults
82 * @param string $content Content of the column
83 * @param string $charset The charset of the content
84 * @throws Exception\InvalidArgumentException When $content is not a string
87 public function setContent($content, $charset = null)
89 if (is_string($content) === false) {
90 throw new Exception\
InvalidArgumentException('$content must be a string');
93 if ($charset === null) {
94 $inputCharset = Table
::getInputCharset();
96 $inputCharset = strtolower($charset);
99 $outputCharset = Table
::getOutputCharset();
101 if ($inputCharset !== $outputCharset) {
102 if (PHP_OS
!== 'AIX') {
103 // AIX does not understand these character sets
104 $strWrapper = StringUtils
::getWrapper($inputCharset, $outputCharset);
105 $content = $strWrapper->convert($content);
109 $this->content
= $content;
117 * @param string $align Align of the column
118 * @throws Exception\OutOfBoundsException When supplied align is invalid
121 public function setAlign($align)
123 if (in_array($align, $this->allowedAligns
) === false) {
124 throw new Exception\
OutOfBoundsException('Invalid align supplied');
127 $this->align
= $align;
135 * @param int $colSpan
136 * @throws Exception\InvalidArgumentException When $colSpan is smaller than 1
139 public function setColSpan($colSpan)
141 if (is_int($colSpan) === false or $colSpan < 1) {
142 throw new Exception\
InvalidArgumentException('$colSpan must be an integer and greater than 0');
145 $this->colSpan
= $colSpan;
155 public function getColSpan()
157 return $this->colSpan
;
161 * Render the column width the given column width
163 * @param int $columnWidth The width of the column
164 * @param int $padding The padding for the column
165 * @throws Exception\InvalidArgumentException When $columnWidth is lower than 1
166 * @throws Exception\OutOfBoundsException When padding is greater than columnWidth
169 public function render($columnWidth, $padding = 0)
171 if (is_int($columnWidth) === false or $columnWidth < 1) {
172 throw new Exception\
InvalidArgumentException('$columnWidth must be an integer and greater than 0');
175 $columnWidth -= ($padding * 2);
177 if ($columnWidth < 1) {
178 throw new Exception\
OutOfBoundsException('Padding (' . $padding . ') is greater than column width');
181 switch ($this->align
) {
182 case self
::ALIGN_LEFT
:
183 $padMode = STR_PAD_RIGHT
;
186 case self
::ALIGN_CENTER
:
187 $padMode = STR_PAD_BOTH
;
190 case self
::ALIGN_RIGHT
:
191 $padMode = STR_PAD_LEFT
;
195 // This can never happen, but the CS tells I have to have it ...
199 $outputCharset = Table
::getOutputCharset();
200 $strWrapper = StringUtils
::getWrapper($outputCharset);
201 $lines = explode("\n", $strWrapper->wordWrap($this->content
, $columnWidth, "\n", true));
204 foreach ($lines as $line) {
205 $paddedLines[] = str_repeat(' ', $padding)
206 . $strWrapper->strPad($line, $columnWidth, ' ', $padMode)
207 . str_repeat(' ', $padding);
210 $result = implode("\n", $paddedLines);