-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextTable.php
446 lines (384 loc) · 11.5 KB
/
TextTable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
namespace kabachello\phpTextTable;
/**
* This class will transform the given PHP array into a plain text table with or without separators.
* It can be used to create
* text tables, CSV files or ASCII-files with fixed field lengts.
*
* Basic usage: new TextTable($rows)->print();
* See more on https://github.com/kabachello/phpTextTable
*
* @author Andrej Kabachnik
* @license MIT
* @link https://github.com/kabachello/phpTextTable
*
*/
class TextTable
{
const COLUMN_ALIGNMENT_LEFT = 'left';
const COLUMN_ALIGNMENT_RIGHT = 'right';
const COLUMN_ALIGNMENT_CENTER = 'center';
/**
*
* @var array The Column index of keys
*/
private $keys = array();
/**
*
* @var array The array for processing
*/
private $rows;
/**
*
* @var array The column width settings
*/
private $column_widths = array();
/**
*
* @var int Max column width (chars)
*/
private $column_width_max = array();
/**
*
* @var boolean
*/
private $column_width_auto = true;
/**
*
* @var array
*/
private $column_alignments = array();
/**
*
* @var int Max row height within a column (lines)
*/
private $row_height_max = null;
/**
*
* @var array The Row lines settings
*/
private $row_heights = array();
/**
*
* @var boolean
*/
private $row_height_auto = true;
/**
*
* @var boolean
*/
private $print_header = false;
/**
*
* @var string
*/
private $separator_crossing = "+";
/**
*
* @var string
*/
private $separator_row = "-";
/**
*
* @var string
*/
private $separator_column = "|";
/**
*
* @param array $rows
*/
public function __construct(array $rows)
{
$this->setRows($rows);
}
/**
* Returns an array of keys, which will be used for the header
*
* @return array
*/
public function getKeys()
{
return $this->keys;
}
/**
* Sets the header values
*
* @param array $array_of_keys
* @return \kabachello\phpTextTable\TextTable
*/
public function setKeys(array $array_of_keys)
{
$this->keys = $array_of_keys;
return $this;
}
/**
* Returns the array to be printed
*
* @return array
*/
public function getRows()
{
return $this->rows;
}
/**
* Sets the array to be printed
*
* @param array $rows
* @return \kabachello\phpTextTable\TextTable
*/
public function setRows(array $rows)
{
$this->rows = $rows;
$this->setKeys(array_keys($this->rows[0]));
return $this;
}
/**
* Calculates the dimensions of the table
*
* @return TextTable
*/
protected function calculateDimensions()
{
foreach ($this->getRows() as $x => $row) {
foreach ($row as $y => $value) {
$this->calculateDimensionsForCell($x, $y, $value);
}
}
return $this;
}
/**
*
* @return boolean
*/
public function getPrintHeader()
{
return $this->print_header;
}
/**
*
* @param boolean $value
* @return \kabachello\phpTextTable\TextTable
*/
public function setPrintHeader($value)
{
$this->print_header = filter_var($value, FILTER_VALIDATE_BOOLEAN);
return $this;
}
/**
* Prints the data to a text table
*
* @return string
*/
public function print($row_key)
{
$this->calculateDimensions();
$result = '';
if ($this->getPrintHeader()) {
$result .= $this->printHeader();
} else {
$result .= $this->printRowLine();
}
if (! is_null($row_key)) {
$result .= $this->printRow($row_key, $this->getRows()[$row_key]);
} else {
foreach ($this->getRows() as $key => $data) {
$result .= $this->printRow($key, $data);
}
}
$result .= $this->printRowLine(false);
return $result;
}
protected function printRowLine($append_newline = true)
{
$result = '';
if ($this->getSeparatorRow()) {
$result .= $this->getSeparatorCrossing();
foreach ($this->getColumnWidths() as $val) {
$result .= $this->getSeparatorRow() . static::mb_str_pad('', $val, $this->getSeparatorRow(), STR_PAD_RIGHT) . $this->getSeparatorRow() . $this->getSeparatorCrossing();
}
}
if ($append_newline)
$result .= "\n";
return $result;
}
protected function printHeader()
{
$result = '';
// Render the header contents
$header_data = array();
foreach ($this->getKeys() as $value) {
$this->calculateDimensionsForCell(false, $value, $value);
$header_data[$value] = strtoupper($value);
}
$result .= $this->printRowLine();
$result .= $this->getSeparatorColumn();
foreach ($this->getColumnWidths() as $key => $val) {
$result .= ' ' . static::mb_str_pad($header_data[$key], $val, ' ', STR_PAD_BOTH) . ' ' . $this->getSeparatorColumn();
}
$result .= "\n";
$result .= $this->printRowLine();
return $result;
}
protected function printRow($row_key, array $row_data)
{
$result = '';
for ($line = 1; $line <= $this->row_heights[$row_key]; $line ++) {
$result .= $this->getSeparatorColumn();
foreach ($row_data as $field => $value) {
switch (mb_strtolower($this->getColumnAlignments()[$field])) {
case self::COLUMN_ALIGNMENT_CENTER:
$strpad_options = STR_PAD_BOTH;
break;
case self::COLUMN_ALIGNMENT_RIGHT:
$strpad_options = STR_PAD_LEFT;
break;
default:
$strpad_options = STR_PAD_RIGHT;
}
$result .= " ";
$result .= static::mb_str_pad(mb_substr($value, ($this->getColumnWidths()[$field] * ($line - 1)), $this->getColumnWidths()[$field]), $this->getColumnWidths()[$field], ' ', $strpad_options);
$result .= " " . $this->getSeparatorColumn();
}
$result .= "\n";
}
return $result;
}
protected function calculateDimensionsForCell($row_key, $column_key, $value)
{
$width = mb_strlen($value);
$width_max = $this->getColumnWidthMax()[$column_key];
$height = 1;
if (! is_null($width_max)) {
if ($width > $width_max) {
$height = is_null($width_max) ? 1 : ceil($width % $width_max);
if ($height > $this->getRowHeightMax()) {
$height = $this->getRowHeightMax();
}
$width = $width_max;
} else {
if (! $this->getColumnWidthAuto()) {
$width = $width_max;
}
}
}
if (! isset($this->column_widths[$column_key]) || $this->column_widths[$column_key] < $width) {
$this->column_widths[$column_key] = $width;
}
if ($row_key !== false && (! isset($this->row_heights[$row_key]) || ($this->getRowHeightAuto() && $this->row_heights[$row_key] < $height))) {
$this->row_heights[$row_key] = $height;
}
return $this;
}
public function getColumnWidthMax()
{
return $this->column_width_max;
}
public function getColumnWidthAuto()
{
return $this->column_width_auto;
}
public function setColumnWidthAuto($value)
{
$this->column_width_auto = $value;
return $this;
}
public function getRowHeightAuto()
{
return $this->row_height_auto;
}
public function setRowHeightAuto($value)
{
$this->row_height_auto = $value;
return $this;
}
public function getColumnWidths()
{
return $this->column_widths;
}
public function setColumnWidthMax($array_or_int)
{
if (is_array($array_or_int)) {
$this->column_width_max = array_merge($this->column_width_max, $array_or_int);
} else {
foreach ($this->getKeys() as $key) {
$this->column_width_max[$key] = $array_or_int;
}
}
return $this;
}
public function getColumnAlignments()
{
return $this->column_alignments;
}
public function setColumnAlignments(array $array)
{
$this->column_alignments = array_merge($this->column_alignments, $array);
return $this;
}
public function getRowHeights()
{
return $this->row_heights;
}
public function setRowHeights($array_or_int)
{
if (is_array($array_or_int)) {
$this->row_heights = array_merge($this->row_heights, $array_or_int);
} else {
foreach (array_keys($this->getRows()) as $key) {
$this->row_heights[$key] = $array_or_int;
}
}
return $this;
}
public function getRowHeightMax()
{
return $this->row_height_max;
}
public function setRowHeightMax($value)
{
$this->row_height_max = $value;
return $this;
}
public function getSeparatorCrossing()
{
return $this->separator_crossing;
}
public function setSeparatorCrossing($value)
{
$this->separator_crossing = $value;
return $this;
}
public function getSeparatorRow()
{
return $this->separator_row;
}
public function setSeparatorRow($value)
{
$this->separator_row = $value;
return $this;
}
public function getSeparatorColumn()
{
return $this->separator_column;
}
public function setSeparatorColumn($value)
{
$this->separator_column = $value;
return $this;
}
protected function mb_str_pad($str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = NULL)
{
$encoding = $encoding === NULL ? mb_internal_encoding() : $encoding;
$padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT;
$padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT;
$pad_len -= mb_strlen($str, $encoding);
$targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len;
$strToRepeatLen = mb_strlen($pad_str, $encoding);
$repeatTimes = ceil($targetLen / $strToRepeatLen);
$repeatedString = str_repeat($pad_str, max(0, $repeatTimes)); // safe if used with valid utf-8 strings
$before = $padBefore ? mb_substr($repeatedString, 0, floor($targetLen), $encoding) : '';
$after = $padAfter ? mb_substr($repeatedString, 0, ceil($targetLen), $encoding) : '';
return $before . $str . $after;
}
}
?>