2
2
3
3
namespace ToolkitLab \ASCII \Formatter ;
4
4
5
- use ToolkitLab \ASCII \FormatterInterface ;
6
5
use ToolkitLab \ASCII \Table ;
7
6
8
7
abstract class AbstractFormatter implements FormatterInterface {
@@ -11,93 +10,185 @@ abstract class AbstractFormatter implements FormatterInterface {
11
10
* @var Table
12
11
*/
13
12
protected $ table ;
13
+
14
+ /**
15
+ * @var array
16
+ */
14
17
protected $ metadata = [];
15
18
16
- public function format (Table $ table , $ firstRowAsHeader = true ) {
17
- $ this ->table = $ table ;
18
- $ str = "" ;
19
-
20
- $ firsRow = 0 ;
21
- if ($ firstRowAsHeader ) {
22
- $ str .= $ this ->header ();
23
- $ firsRow = 1 ;
24
- } else if ($ this ->metadata ['useHeader ' ]) {
25
- $ delimiter = $ this ->metadata ["HBL " ];
26
- for ($ i = 0 ; $ i < $ this ->table ->getDimensionX (); $ i ++) {
27
- $ columnLenght = $ this ->table ->getColumnsMaxLenght ($ i ) + 2 ;
28
- $ str .= $ delimiter ;
29
- $ str .= str_repeat ($ this ->metadata ["HPAD " ], $ columnLenght );
30
- $ delimiter = $ this ->metadata ["HBM " ];
31
- }
32
- $ str .= $ this ->metadata ["HBR " ] . PHP_EOL ;
33
- }
19
+ /**
20
+ * @var string
21
+ */
22
+ protected $ output = '' ;
34
23
35
- for ($ i = $ firsRow ; $ i < $ this ->table ->getDimensionY (); $ i ++) {
36
- $ str .= $ this ->line ($ i );
37
- }
24
+ /**
25
+ * The data to be formatted
26
+ * @var array
27
+ */
28
+ protected $ data = [];
38
29
39
- if ($ this ->metadata ['useFooter ' ]) {
40
- $ delimiter = $ this ->metadata ["FBL " ];
41
- for ($ i = 0 ; $ i < $ this ->table ->getDimensionX (); $ i ++) {
42
- $ columnLenght = $ this ->table ->getColumnsMaxLenght ($ i ) + 2 ;
43
- $ str .= $ delimiter ;
44
- $ str .= str_repeat ($ this ->metadata ["FPAD " ], $ columnLenght );
45
- $ delimiter = $ this ->metadata ["FBM " ];
46
- }
47
- $ str .= $ this ->metadata ["FBR " ] . PHP_EOL ;
30
+ /**
31
+ * Formatting parameters
32
+ * @var array
33
+ */
34
+ protected $ params = [
35
+ 'first_row_header ' => false ,
36
+ ];
37
+
38
+ /**
39
+ * Constructor
40
+ * @param array $params
41
+ */
42
+ public function __construct ($ params = []) {
43
+ $ this ->setParams ($ params );
44
+ }
45
+
46
+ /**
47
+ * Converts an array into ASCII-formatted string (table)
48
+ * @param Table $table
49
+ * @param array $params
50
+ * @return string
51
+ */
52
+ public function format ($ data , $ params = []) {
53
+ $ this ->init ($ data , $ params );
54
+ $ this ->addTopBorder ();
55
+ $ this ->addHeader ();
56
+ $ this ->addRows ();
57
+ $ this ->addBottomBorder ();
58
+ return $ this ->output ;
59
+ }
60
+
61
+ /**
62
+ * Initializes the data/parameters before formatting
63
+ * @param Table $table
64
+ * @param array $params
65
+ * @return void
66
+ */
67
+ protected function init ($ data , $ params = []) {
68
+ $ this ->table = new Table ($ data );
69
+ $ this ->output = '' ;
70
+ $ this ->data = $ this ->table ->getData ();
71
+ $ this ->setParams ($ params );
72
+ }
73
+
74
+ /**
75
+ * Updates the parameters with new values
76
+ * @param array $params
77
+ * @throws \InvalidArgumentException
78
+ */
79
+ protected function setParams ($ params ) {
80
+ $ unknownParams = array_diff (array_keys ($ params ), array_keys ($ this ->params ));
81
+ if (count ($ unknownParams )) {
82
+ throw new \InvalidArgumentException ('Unknown parameter(-s): ' . implode (', ' , $ unknownParams ));
48
83
}
84
+ $ this ->params = array_merge ($ this ->params , $ params );
85
+ }
49
86
50
- return $ str ;
87
+ /**
88
+ * Get the specified parameter
89
+ * @param string $key
90
+ * @return mixed
91
+ */
92
+ protected function getParam ($ key ) {
93
+ return $ this ->params [$ key ];
51
94
}
52
95
53
- private function line ($ rowIndex ) {
54
- $ retStr = "" ;
55
- $ delimiter = $ this ->metadata ["BL " ];
56
- for ($ i = 0 ; $ i < $ this ->table ->getDimensionX (); $ i ++) {
57
- $ retStr .= $ delimiter ;
58
- $ cell = $ this ->table ->getCell ($ i , $ rowIndex );
59
- $ retStr .= " " . $ cell . str_repeat (" " , $ this ->table ->getColumnsMaxLenght ($ i ) - strlen ($ cell )) . " " ;
60
- $ delimiter = $ this ->metadata ["BM " ];
96
+ /**
97
+ * Adds the top border to the output
98
+ * @return void
99
+ */
100
+ protected function addTopBorder () {
101
+ if ($ this ->metadata ['topBorder ' ]) {
102
+ $ this ->addRow (
103
+ $ this ->metadata ["HBL " ], $ this ->metadata ["HBM " ], $ this ->metadata ["HBR " ], $ this ->metadata ["HPAD " ]
104
+ );
61
105
}
62
- $ retStr .= $ this ->metadata ["BR " ] . PHP_EOL ;
63
- return $ retStr ;
64
106
}
65
107
66
- protected function header () {
67
- $ retStr = "" ;
68
-
69
- // First row
70
- if ($ this ->metadata ['useHeader ' ]) {
71
- $ delimiter = $ this ->metadata ["HBL " ];
72
- for ($ i = 0 ; $ i < $ this ->table ->getDimensionX (); $ i ++) {
73
- $ columnLenght = $ this ->table ->getColumnsMaxLenght ($ i ) + 2 ;
74
- $ retStr .= $ delimiter ;
75
- $ retStr .= str_repeat ($ this ->metadata ["HPAD " ], $ columnLenght );
76
- $ delimiter = $ this ->metadata ["HBM " ];
77
- }
78
- $ retStr .= $ this ->metadata ["HBR " ] . PHP_EOL ;
108
+ /**
109
+ * Adds the header row to the output
110
+ * @return void
111
+ */
112
+ protected function addHeader () {
113
+ if ($ this ->getParam ('first_row_header ' )) {
114
+ $ this ->addDataRow (array_shift ($ this ->data ));
115
+ $ this ->addHeaderBorder ();
79
116
}
117
+ }
80
118
81
- // Second row
82
- $ delimiter = $ this ->metadata ["BL " ];
83
- for ($ i = 0 ; $ i < $ this ->table ->getDimensionX (); $ i ++) {
84
- $ retStr .= $ delimiter ;
85
- $ retStr .= " " . $ this ->table ->getCell ($ i , 0 ) . " " ;
119
+ /**
120
+ * Adds the rows to the output
121
+ */
122
+ protected function addRows () {
123
+ array_walk ($ this ->data , [$ this , 'addDataRow ' ]);
124
+ }
125
+
126
+ /**
127
+ * Adds the row with the specified data to the output
128
+ * @param array $row
129
+ * @return void
130
+ */
131
+ protected function addDataRow ($ row ) {
132
+ $ this ->addRow (
133
+ $ this ->metadata ["BL " ],
134
+ $ this ->metadata ["BM " ],
135
+ $ this ->metadata ["BR " ],
136
+ ' ' ,
137
+ $ row
138
+ );
139
+ }
140
+
141
+ /**
142
+ * Adds the bottom border of the header to the output
143
+ * @return void
144
+ */
145
+ protected function addHeaderBorder () {
146
+ $ this ->addRow (
147
+ $ this ->metadata ["H2BL " ],
148
+ $ this ->metadata ["H2BM " ],
149
+ $ this ->metadata ["H2BR " ],
150
+ $ this ->metadata ["H2PAD " ]
151
+ );
152
+ }
153
+
154
+ /**
155
+ * Adds the bottom border to the output
156
+ * @return void
157
+ */
158
+ protected function addBottomBorder () {
159
+ if ($ this ->metadata ['bottomBorder ' ]) {
160
+ $ this ->addRow (
161
+ $ this ->metadata ["FBL " ],
162
+ $ this ->metadata ["FBM " ],
163
+ $ this ->metadata ["FBR " ],
164
+ $ this ->metadata ["FPAD " ]
165
+ );
86
166
}
87
- $ retStr .= $ this -> metadata [ " BR " ] . PHP_EOL ;
167
+ }
88
168
89
- // Third row
90
- $ delimiter = $ this ->metadata ["H2BL " ];
169
+ /**
170
+ * Adds the row to the output
171
+ * @param string $lb
172
+ * @param string $bm
173
+ * @param string $br
174
+ * @param string $pad
175
+ * @param array $row
176
+ * @return void
177
+ */
178
+ protected function addRow ($ lb , $ bm , $ br , $ pad , $ row = []) {
179
+ $ delimiter = $ lb ;
91
180
for ($ i = 0 ; $ i < $ this ->table ->getDimensionX (); $ i ++) {
92
- $ columnLenght = $ this ->table ->getColumnsMaxLenght ($ i ) + 2 ;
93
- $ retStr .= $ delimiter ;
94
- $ retStr .= str_repeat ($ this ->metadata ["BPAD " ], $ columnLenght );
95
- $ delimiter = $ this ->metadata ["H2BM " ];
181
+ $ maxLength = $ this ->table ->getColumnsMaxLenght ($ i );
182
+ $ this ->output .= $ delimiter ;
183
+ if (count ($ row )) {
184
+ $ spaces = str_repeat ($ pad , $ maxLength - strlen ($ row [$ i ]));
185
+ $ this ->output .= " {$ row [$ i ]}{$ spaces } " ;
186
+ } else {
187
+ $ this ->output .= str_repeat ($ pad , $ maxLength + 2 );
188
+ }
189
+ $ delimiter = $ bm ;
96
190
}
97
- $ retStr .= $ this ->metadata ["H2BR " ] . PHP_EOL ;
98
-
99
-
100
- return $ retStr ;
191
+ $ this ->output .= $ br . PHP_EOL ;
101
192
}
102
193
103
194
}
0 commit comments