|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * TableHelper |
| 3 | + * Table |
4 | 4 | * |
5 | 5 | * @author Piotr Olaszewski |
6 | 6 | */ |
|
11 | 11 |
|
12 | 12 | class Table implements HelperInterface |
13 | 13 | { |
14 | | - private $headers = array(); |
15 | | - private $rows = array(); |
| 14 | + private $_headers = array(); |
| 15 | + private $_rows = array(); |
16 | 16 | /** |
17 | 17 | * @var Writer |
18 | 18 | */ |
19 | | - private $output; |
| 19 | + private $_output; |
20 | 20 |
|
21 | 21 | public function setHeaders(array $headers) |
22 | 22 | { |
23 | | - $this->headers = $headers; |
| 23 | + $this->_headers = $headers; |
24 | 24 | return $this; |
25 | 25 | } |
26 | 26 |
|
27 | 27 | public function setRows(array $rows) |
28 | 28 | { |
29 | | - $this->rows = $rows; |
| 29 | + $this->_rows = $rows; |
30 | 30 | return $this; |
31 | 31 | } |
32 | 32 |
|
33 | 33 | public function render(Writer $output) |
34 | 34 | { |
35 | | - $this->output = $output; |
| 35 | + $this->_output = $output; |
36 | 36 |
|
37 | | - $this->renderHeaderRowSeparator(); |
38 | | - $this->generateHeaderRow(); |
39 | | - $this->renderHeaderRowSeparator(); |
| 37 | + $this->_renderHeaderRowSeparator(); |
| 38 | + $this->_generateHeaderRow(); |
| 39 | + $this->_renderHeaderRowSeparator(); |
40 | 40 |
|
41 | | - $this->generateBodyRows(); |
42 | | - $this->renderHeaderRowSeparator(); |
| 41 | + $this->_generateBodyRows(); |
| 42 | + $this->_renderHeaderRowSeparator(); |
43 | 43 | } |
44 | 44 |
|
45 | | - private function renderHeaderRowSeparator() |
| 45 | + private function _renderHeaderRowSeparator() |
46 | 46 | { |
47 | 47 | $separator = '+'; |
48 | | - foreach ($this->headers as $column => $header) { |
49 | | - $columnWidth = $this->getColumnWidth($column); |
| 48 | + foreach ($this->_headers as $column => $header) { |
| 49 | + $columnWidth = $this->_getColumnWidth($column); |
50 | 50 | $separator .= str_repeat('-', $columnWidth) . '+'; |
51 | 51 | } |
52 | | - $this->output->writeMessage($separator); |
| 52 | + $this->_output->writeMessage($separator); |
53 | 53 | } |
54 | 54 |
|
55 | | - private function generateHeaderRow() |
| 55 | + private function _generateHeaderRow() |
56 | 56 | { |
57 | | - $this->renderDataRow($this->headers); |
| 57 | + $this->_renderDataRow($this->_headers); |
58 | 58 | } |
59 | 59 |
|
60 | | - private function generateBodyRows() |
| 60 | + private function _generateBodyRows() |
61 | 61 | { |
62 | | - foreach ($this->rows as $row) { |
63 | | - $this->renderDataRow($row); |
| 62 | + foreach ($this->_rows as $row) { |
| 63 | + $this->_renderDataRow($row); |
64 | 64 | } |
65 | 65 | } |
66 | 66 |
|
67 | | - private function renderDataRow($row) |
| 67 | + private function _renderDataRow($row) |
68 | 68 | { |
69 | 69 | $line = '|'; |
70 | 70 | foreach ($row as $column => $name) { |
71 | | - $columnWidth = $this->getColumnWidth($column); |
| 71 | + $columnWidth = $this->_getColumnWidth($column); |
72 | 72 | $spaces = $columnWidth - strlen($name) - 1; |
73 | 73 | $line .= ' ' . $name . str_repeat(' ', $spaces) . '|'; |
74 | 74 | } |
75 | | - $this->output->writeMessage($line); |
| 75 | + $this->_output->writeMessage($line); |
76 | 76 | } |
77 | 77 |
|
78 | | - private function getColumnWidth($column) |
| 78 | + private function _getColumnWidth($column) |
79 | 79 | { |
80 | 80 | $width = 0; |
81 | | - if (isset($this->headers[$column])) { |
82 | | - $width = strlen($this->headers[$column]); |
| 81 | + if (isset($this->_headers[$column])) { |
| 82 | + $width = strlen($this->_headers[$column]); |
83 | 83 | } |
84 | 84 | array_map(function ($element) use (&$width, $column) { |
85 | 85 | $length = strlen($element[$column]); |
86 | 86 | if ($length > $width) { |
87 | 87 | $width = $length; |
88 | 88 | } |
89 | | - }, $this->rows); |
90 | | - return $this->widthWithSpaces($width); |
| 89 | + }, $this->_rows); |
| 90 | + return $this->_widthWithSpaces($width); |
91 | 91 | } |
92 | 92 |
|
93 | | - private function widthWithSpaces($width) |
| 93 | + private function _widthWithSpaces($width) |
94 | 94 | { |
95 | 95 | return $width + 2; |
96 | 96 | } |
|
0 commit comments