composer package updates
[openemr.git] / vendor / zendframework / zend-text / src / Table / Column.php
bloba88663060d078913650331e744c2c444f1d03d9a
1 <?php
2 /**
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
6 */
8 namespace Zend\Text\Table;
10 use Zend\Stdlib\StringUtils;
12 /**
13 * Column class for Zend\Text\Table\Row
15 class Column
17 /**
18 * Aligns for columns
20 const ALIGN_LEFT = 'left';
21 const ALIGN_CENTER = 'center';
22 const ALIGN_RIGHT = 'right';
24 /**
25 * Content of the column
27 * @var string
29 protected $content = '';
31 /**
32 * Align of the column
34 * @var string
36 protected $align = self::ALIGN_LEFT;
38 /**
39 * Colspan of the column
41 * @var int
43 protected $colSpan = 1;
45 /**
46 * Allowed align parameters
48 * @var array
50 protected $allowedAligns = [self::ALIGN_LEFT, self::ALIGN_CENTER, self::ALIGN_RIGHT];
52 /**
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);
75 /**
76 * Set the content.
78 * If $charset is not defined, it is assumed that $content is encoded in
79 * the charset defined via Zend\Text\Table::setInputCharset() (defaults
80 * to utf-8).
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
85 * @return Column
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();
95 } else {
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;
111 return $this;
115 * Set the align
117 * @param string $align Align of the column
118 * @throws Exception\OutOfBoundsException When supplied align is invalid
119 * @return Column
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;
129 return $this;
133 * Set the colspan
135 * @param int $colSpan
136 * @throws Exception\InvalidArgumentException When $colSpan is smaller than 1
137 * @return Column
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;
147 return $this;
151 * Get the colspan
153 * @return int
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
167 * @return string
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;
184 break;
186 case self::ALIGN_CENTER:
187 $padMode = STR_PAD_BOTH;
188 break;
190 case self::ALIGN_RIGHT:
191 $padMode = STR_PAD_LEFT;
192 break;
194 default:
195 // This can never happen, but the CS tells I have to have it ...
196 break;
199 $outputCharset = Table::getOutputCharset();
200 $strWrapper = StringUtils::getWrapper($outputCharset);
201 $lines = explode("\n", $strWrapper->wordWrap($this->content, $columnWidth, "\n", true));
202 $paddedLines = [];
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);
212 return $result;