Passed
Branch test (317661)
by Roman
02:44
created

AbstractFormatter::addHeaderBorder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ToolkitLab\ASCII;
4
5
use ToolkitLab\ASCII\Table;
6
7
abstract class AbstractFormatter implements FormatterInterface {
8
    
9
    const
10
        DEFAULT_MODE              = 0x0,
11
12
        HEADER_FIRST_ROW_MODE     = 0X1,
13
        HEADER_NUMERIC_MODE       = 0x2,
14
        HEADER_ABC_MODE           = 0X4,
15
            
16
        SIDEBAR_NUMERIC_MODE      = 0X10,
17
        SIDEBAR_ABC_MODE          = 0x20,
18
            
19
        SPREADSHEET_MODE          = 0X14;
20
21
    /**
22
     * @var Table
23
     */
24
    protected $table;
25
26
    /**
27
     * @var array
28
     */
29
    protected $metadata = [];
30
31
    /**
32
     * @var string
33
     */
34
    protected $output = '';
35
36
    /**
37
     * The data to be formatted
38
     * @var array
39
     */
40
    protected $data = [];
41
    
42
    /**
43
     * @var boolean
44
     */
45
    protected $useHeader = false;
46
47
    /**
48
     * Formatting parameters
49
     * @var array
50
     */
51
    protected $params = [
52
        'mode' => self::DEFAULT_MODE,
53
        'rotate' => false,
54
        'max_cell_length' => 100,
55
        'max_cell_ending' => '...',
56
    ];
57
58
    /**
59
     * Constructor
60
     * @param array $params
61
     */
62 30
    public function __construct($params = []) {
63 30
        $this->setParams($params);
64 28
    }
65
66
    /**
67
     * Converts an array into ASCII-formatted string (table)
68
     * @param Table $table
69
     * @param array $params
70
     * @return string
71
     */
72 28
    public function format($data, $params = []) {
73 28
        $table = $data instanceof Table ? $data : new Table($data);
74 28
        $this->init($table, $params);
75
//                if (($this->getParam('mode') & self::SPREADSHEET_MODE) === self::SPREADSHEET_MODE) {
76
//            
77
//            print '<pre>';
78
//            print_r($this->table->getDimensionX());
79
//            print '</pre>';
80
//            exit;
81
//        }
82 28
        $this->addTopBorder();
83 28
        $this->addHeader();
84 28
        $this->addRows();
85 28
        $this->addBottomBorder();
86 28
        return $this->output;
87
    }
88
89
    /**
90
     * Initializes the data/parameters before formatting
91
     * @param Table $table
92
     * @param array $params
93
     * @return void
94
     */
95 28
    protected function init(Table $table, $params = []) {
96 28
        $this->output = '';
97 28
        $this->setParams($params);
98 28
        $this->table = $table;
99 28
        if ($this->getParam('rotate') !== false) {
100 8
            $this->table->rotate($this->getParam('rotate'));
101 4
        }
102 28
        $this->initHeader();
103 28
        $this->initSidebar();
104 28
        $maxLength = $this->getParam('max_cell_length');
105 28
        $ending = $this->getParam('max_cell_ending');
106 28
        $this->table->truncate($maxLength, $ending);
107 28
    }
108
    
109 28
    protected function initHeader() {
110 28
        $count = 0;
111 28
        $mode = $this->getParam('mode');
112 28
        $data = $this->table->getData();
113 28
        $x = $this->table->getDimensionX();
114 28
        if (($mode & self::HEADER_FIRST_ROW_MODE) === self::HEADER_FIRST_ROW_MODE) {
115 10
            $count++;
116 5
        }
117 28
        if (($mode & self::HEADER_ABC_MODE) === self::HEADER_ABC_MODE) {
118 4
            $data = array_merge([$this->getAbcRange($x)], $data);
119 4
            $count++;
120 2
        }
121 28
        if (($mode & self::HEADER_NUMERIC_MODE) === self::HEADER_NUMERIC_MODE) {
122 2
            $data = array_merge([range(1, $x)], $data);
123 2
            $count++;
124 1
        }
125 28
        if ($count > 1) {
126
            throw new \LogicException('There should be only one header.');
127
        }
128 28
        if ($count) {
129 16
            $this->useHeader = true;
130 16
            $this->table->setData($data);
131 8
        }
132 28
    }
133
    
134 28
    protected function initSidebar() {
135 28
        $count = 0;
136 28
        $mode = $this->getParam('mode');
137 28
        $data = $this->table->getData();
138 28
        $x = $this->table->getDimensionX();
139 28
        if (($mode & self::SIDEBAR_ABC_MODE) === self::SIDEBAR_ABC_MODE) {
140
            foreach ($data as $key => $val) {
141
                $data[$key] = array_merge($this->getAbcRange($x), $val);
142
            }
143
            $count++;
144
        }
145 28
        if (($mode & self::SIDEBAR_NUMERIC_MODE) === self::SIDEBAR_NUMERIC_MODE) {
146 2
            foreach ($data as $key => $val) {
147 2
                $data[$key] = array_merge([$key ? $key : ''], $val);
148 1
            }
149 2
            $count++;
150 1
        }
151 28
        if ($count > 1) {
152
            throw new \LogicException('There should be only one sidebar.');
153
        }
154
        
155 28
        if ($count) {
156 2
            $this->table->setData($data);
157 1
        }
158 28
    }
159
    
160
    /**
161
     * Updates the parameters with new values
162
     * @param array $params
163
     * @throws \InvalidArgumentException
164
     */
165 30
    protected function setParams($params) {
166 30
        $unknownParams = array_diff(array_keys($params), array_keys($this->params));
167 30
        if (count($unknownParams)) {
168 2
            throw new \InvalidArgumentException('Unknown parameter(-s): ' . implode(', ', $unknownParams));
169
        }
170 28
        $this->params = array_merge($this->params, $params);
171 28
    }
172
173
    /**
174
     * Get the specified parameter
175
     * @param string $key
176
     * @return mixed
177
     */
178 28
    protected function getParam($key) {
179 28
        return $this->params[$key];
180
    }
181
182
    /**
183
     * Adds the top border to the output
184
     * @return void
185
     */
186 28
    protected function addTopBorder() {
187 28
        if ($this->metadata['top_border']) {
188 24
            $this->addRow(
189 24
                $this->metadata['top_border']['left'],
190 24
                $this->metadata['top_border']['middle'],
191 24
                $this->metadata['top_border']['right'],
192 24
                $this->metadata['top_border']['pad']
193 12
            );
194 12
        }
195 28
    }
196
197
    /**
198
     * Adds the header row to the output
199
     * @return void
200
     */
201 28
    protected function addHeader() {
202 28
        if ($this->useHeader) {
203 16
            $data = $this->table->getData();
204 16
            $x = $this->table->getDimensionX();
205 16
            $y = $this->table->getDimensionY();
206 16
            $this->addDataRow(array_shift($data));
207 16
            $this->addHeaderBorder();
208 16
            $data = $this->table->setData($data, $x, $y);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $this->table->setData($data, $x, $y) targeting ToolkitLab\ASCII\Table::setData() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
209 8
        }
210 28
    }
211
    
212 4
    final protected function getAbcRange($length) {
213 4
        $baseRange = range('A', 'Z');
214 4
        $range = range('A', 'Z');
215 4
        while (count($range) < $length && count($baseRange) > 1) {
216 2
            $letter = array_shift($baseRange);
217 2
            $tmpRange = range('A', 'Z');
218
            array_walk($tmpRange, function(&$e) use ($letter) { $e = $letter . $e; });
219 2
            $range = array_merge($range, $tmpRange);
220 1
        }
221 4
        return array_slice($range, 0, $length);
222
    }
223
224
    /**
225
     * Adds the rows to the output
226
     */
227 28
    protected function addRows() {
228 28
        $data = $this->table->getData();
229 28
        array_walk($data, [$this, 'addDataRow']);
230 28
    }
231
232
    /**
233
     * Adds the row with the specified data to the output
234
     * @param array $row
235
     * @return void
236
     */
237 28
    protected function addDataRow($row) {
238 28
        $this->addRow(
239 28
            $this->metadata['content']['left'],
240 28
            $this->metadata['content']['middle'],
241 28
            $this->metadata['content']['right'],
242 28
            $this->metadata['content']['pad'],
243 14
            $row
244 14
        );
245 28
    }
246
247
    /**
248
     * Adds the bottom border of the header to the output
249
     * @return void
250
     */
251 16
    protected function addHeaderBorder() {
252 16
        $this->addRow(
253 16
            $this->metadata['header']['left'],
254 16
            $this->metadata['header']['middle'],
255 16
            $this->metadata['header']['right'],
256 16
            $this->metadata['header']['pad']
257 8
        );
258 16
    }
259
260
    /**
261
     * Adds the bottom border to the output
262
     * @return void
263
     */
264 28
    protected function addBottomBorder() {
265 28
        if ($this->metadata['bottom_border']) {
266 24
            $this->addRow(
267 24
                $this->metadata['bottom_border']['left'],
268 24
                $this->metadata['bottom_border']['middle'],
269 24
                $this->metadata['bottom_border']['right'],
270 24
                $this->metadata['bottom_border']['pad']
271 12
            );
272 12
        }
273 28
    }
274
275
    /**
276
     * Adds the row to the output
277
     * @param string $lb
278
     * @param string $bm
279
     * @param string $br
280
     * @param string $pad
281
     * @param array $row
282
     * @return void
283
     */
284 28
    protected function addRow($lb, $bm, $br, $pad, $row = []) {
285 28
        $delimiter = $lb;
286 28
        for ($i = 0; $i < $this->table->getDimensionX(); $i++) {
287 28
            $maxLength = $this->table->getColumnsMaxLength($i);
288 28
            $this->output .= $delimiter;
289 28
            if (count($row)) {
290 28
                $spaces = str_repeat($pad, $maxLength - strlen($row[$i]));
291 28
                $this->output .= " {$row[$i]}{$spaces} ";
292 14
            } else {
293 26
                $this->output .= str_repeat($pad, $maxLength + 2);
294
            }
295 28
            $delimiter = $bm;
296 14
        }
297 28
        $this->output .= $br . PHP_EOL;
298 28
    }
299
300
}
301