1 | <?php |
||
8 | class ArrayToTextTable |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private $data; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $columnsList = []; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | private $maxLineLength = 40; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $columnsLength = []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $result = []; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $charset = 'UTF-8'; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $renderHeader = true; |
||
44 | |||
45 | |||
46 | public function __construct(array $data) |
||
50 | |||
51 | /** |
||
52 | * Calculates list of columns in data |
||
53 | */ |
||
54 | protected function calcColumnsList() |
||
58 | |||
59 | /** |
||
60 | * Calculates length for string |
||
61 | */ |
||
62 | protected function length($str) |
||
66 | |||
67 | /** |
||
68 | * Calculate maximum string length for each column |
||
69 | */ |
||
70 | private function calcColumnsLength() |
||
90 | |||
91 | /** |
||
92 | * Append a line separator to result |
||
93 | */ |
||
94 | private function lineSeparator() |
||
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | private function column($columnKey, $value) |
||
112 | |||
113 | /** |
||
114 | * Render header part |
||
115 | * @return void |
||
116 | */ |
||
117 | private function renderHeader() |
||
135 | |||
136 | /** |
||
137 | * Render body section of table |
||
138 | * @return void |
||
139 | */ |
||
140 | private function renderBody() |
||
158 | |||
159 | /** |
||
160 | * Set custom charset for columns values |
||
161 | * @return self |
||
162 | */ |
||
163 | public function charset($charset) |
||
176 | |||
177 | /** |
||
178 | * Set mode to print no header in the table |
||
179 | * @return self |
||
180 | */ |
||
181 | public function noHeader() |
||
187 | |||
188 | /** |
||
189 | * @param int $length |
||
190 | * @return self |
||
191 | * @throws Exception |
||
192 | */ |
||
193 | public function maxLineLength($length) |
||
203 | |||
204 | /** |
||
205 | * Build your ascii table and return the result |
||
206 | * @return string |
||
207 | */ |
||
208 | public function render() |
||
229 | } |