Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Text / Table / Column.php
blob833b9377c4db47efa36c55e9cfe5bc9c4e79906b
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Text\Table;
12 use Zend\Stdlib\StringUtils;
13 use Zend\Text;
15 /**
16 * Column class for Zend\Text\Table\Row
18 class Column
20 /**
21 * Aligns for columns
23 const ALIGN_LEFT = 'left';
24 const ALIGN_CENTER = 'center';
25 const ALIGN_RIGHT = 'right';
27 /**
28 * Content of the column
30 * @var string
32 protected $content = '';
34 /**
35 * Align of the column
37 * @var string
39 protected $align = self::ALIGN_LEFT;
41 /**
42 * Colspan of the column
44 * @var int
46 protected $colSpan = 1;
48 /**
49 * Allowed align parameters
51 * @var array
53 protected $allowedAligns = array(self::ALIGN_LEFT, self::ALIGN_CENTER, self::ALIGN_RIGHT);
55 /**
56 * Create a column for a Zend\Text\Table\Row object.
58 * @param string $content The content of the column
59 * @param string $align The align of the content
60 * @param int $colSpan The colspan of the column
61 * @param string $charset The encoding of the content
63 public function __construct($content = null, $align = null, $colSpan = null, $charset = null)
65 if ($content !== null) {
66 $this->setContent($content, $charset);
69 if ($align !== null) {
70 $this->setAlign($align);
73 if ($colSpan !== null) {
74 $this->setColSpan($colSpan);
78 /**
79 * Set the content.
81 * If $charset is not defined, it is assumed that $content is encoded in
82 * the charset defined via Zend\Text\Table::setInputCharset() (defaults
83 * to utf-8).
85 * @param string $content Content of the column
86 * @param string $charset The charset of the content
87 * @throws Exception\InvalidArgumentException When $content is not a string
88 * @return Column
90 public function setContent($content, $charset = null)
92 if (is_string($content) === false) {
93 throw new Exception\InvalidArgumentException('$content must be a string');
96 if ($charset === null) {
97 $inputCharset = Table::getInputCharset();
98 } else {
99 $inputCharset = strtolower($charset);
102 $outputCharset = Table::getOutputCharset();
104 if ($inputCharset !== $outputCharset) {
105 if (PHP_OS !== 'AIX') {
106 // AIX does not understand these character sets
107 $strWrapper = StringUtils::getWrapper($inputCharset, $outputCharset);
108 $content = $strWrapper->convert($content);
113 $this->content = $content;
115 return $this;
119 * Set the align
121 * @param string $align Align of the column
122 * @throws Exception\OutOfBoundsException When supplied align is invalid
123 * @return Column
125 public function setAlign($align)
127 if (in_array($align, $this->allowedAligns) === false) {
128 throw new Exception\OutOfBoundsException('Invalid align supplied');
131 $this->align = $align;
133 return $this;
137 * Set the colspan
139 * @param int $colSpan
140 * @throws Exception\InvalidArgumentException When $colSpan is smaller than 1
141 * @return Column
143 public function setColSpan($colSpan)
145 if (is_int($colSpan) === false or $colSpan < 1) {
146 throw new Exception\InvalidArgumentException('$colSpan must be an integer and greater than 0');
149 $this->colSpan = $colSpan;
151 return $this;
155 * Get the colspan
157 * @return int
159 public function getColSpan()
161 return $this->colSpan;
165 * Render the column width the given column width
167 * @param int $columnWidth The width of the column
168 * @param int $padding The padding for the column
169 * @throws Exception\InvalidArgumentException When $columnWidth is lower than 1
170 * @throws Exception\OutOfBoundsException When padding is greater than columnWidth
171 * @return string
173 public function render($columnWidth, $padding = 0)
175 if (is_int($columnWidth) === false or $columnWidth < 1) {
176 throw new Exception\InvalidArgumentException('$columnWidth must be an integer and greater than 0');
179 $columnWidth -= ($padding * 2);
181 if ($columnWidth < 1) {
182 throw new Exception\OutOfBoundsException('Padding (' . $padding . ') is greater than column width');
185 switch ($this->align) {
186 case self::ALIGN_LEFT:
187 $padMode = STR_PAD_RIGHT;
188 break;
190 case self::ALIGN_CENTER:
191 $padMode = STR_PAD_BOTH;
192 break;
194 case self::ALIGN_RIGHT:
195 $padMode = STR_PAD_LEFT;
196 break;
198 default:
199 // This can never happen, but the CS tells I have to have it ...
200 break;
203 $outputCharset = Table::getOutputCharset();
204 $strWrapper = StringUtils::getWrapper($outputCharset);
205 $lines = explode("\n", $strWrapper->wordWrap($this->content, $columnWidth, "\n", true));
206 $paddedLines = array();
208 foreach ($lines as $line) {
209 $paddedLines[] = str_repeat(' ', $padding)
210 . $strWrapper->strPad($line, $columnWidth, ' ', $padMode)
211 . str_repeat(' ', $padding);
214 $result = implode("\n", $paddedLines);
216 return $result;