Passed
Branch test (87187c)
by Roman
03:25
created

AbstractFormatter::getParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
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              = 0x00,
11
12
        HEADER_FIRST_ROW_MODE     = 0X01,
13
        HEADER_NUMERIC_MODE       = 0x02,
14
        HEADER_ABC_MODE           = 0X04,
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 40
    public function __construct($params = []) {
63 40
        $this->setParams($params);
64 38
    }
65
66
    /**
67
     * Converts an array into ASCII-formatted string (table)
68
     * @param Table $table
69
     * @param array $params
70
     * @return string
71
     */
72 38
    public function format($data, $params = []) {
73 38
        $table = $data instanceof Table ? $data : new Table($data);
74 38
        $this->init($table, $params);
75 34
        $this->addTopBorder();
76 34
        $this->addHeader();
77 34
        $this->addRows();
78 34
        $this->addBottomBorder();
79 34
        return $this->output;
80
    }
81
82
    /**
83
     * Initializes the data/parameters before formatting
84
     * @param Table $table
85
     * @param array $params
86
     * @return void
87
     */
88 38
    protected function init(Table $table, $params = []) {
89 38
        $this->output = '';
90 38
        $this->setParams($params);
91 38
        $this->table = $table;
92 38
        if ($this->getParam('rotate') !== false) {
93 8
            $this->table->rotate($this->getParam('rotate'));
94 4
        }
95 38
        $this->initHeader();
96 36
        $this->initSidebar();
97 34
        $maxLength = $this->getParam('max_cell_length');
98 34
        $ending = $this->getParam('max_cell_ending');
99 34
        $this->table->truncate($maxLength, $ending);
100 34
    }
101
    
102
    /**
103
     * Initializes the header
104
     * @throws \LogicException
105
     */
106 38
    protected function initHeader() {
107 38
        $count = 0;
108 38
        $mode = $this->getParam('mode');
109 38
        $data = $this->table->getData();
110 38
        $x = $this->table->getDimensionX();
111 38
        if (($mode & self::HEADER_FIRST_ROW_MODE) === self::HEADER_FIRST_ROW_MODE) {
112 10
            $count++;
113 5
        }
114 38
        if (($mode & self::HEADER_ABC_MODE) === self::HEADER_ABC_MODE) {
115 6
            $data = array_merge([$this->getAbcRange($x)], $data);
116 6
            $count++;
117 3
        }
118 38
        if (($mode & self::HEADER_NUMERIC_MODE) === self::HEADER_NUMERIC_MODE) {
119 4
            $data = array_merge([range(1, $x)], $data);
120 4
            $count++;
121 2
        }
122 38
        if ($count > 1) {
123 2
            throw new \LogicException('There should be only one header.');
124
        }
125 36
        if ($count) {
126 16
            $this->useHeader = true;
127 16
            $this->table->setData($data);
128 8
        }
129 36
    }
130
    
131
    /**
132
     * Initializes the sidebar
133
     * @throws \LogicException
134
     */
135 36
    protected function initSidebar() {
136 36
        $count = 0;
137 36
        $mode = $this->getParam('mode');
138 36
        $data = $this->table->getData();
139 36
        if (($mode & self::SIDEBAR_ABC_MODE) === self::SIDEBAR_ABC_MODE) {
140 4
            $y = $this->table->getDimensionY();
141 4
            $abc = $this->getAbcRange($y);
142 4
            foreach ($data as $key => $val) {
143 4
                $data[$key] = array_merge([array_shift($abc)], $val);
144 2
            }
145 4
            $count++;
146 2
        }
147 36
        if (($mode & self::SIDEBAR_NUMERIC_MODE) === self::SIDEBAR_NUMERIC_MODE) {
148 4
            foreach ($data as $key => $val) {
149 4
                $data[$key] = array_merge([$key ? $key : ''], $val);
150 2
            }
151 4
            $count++;
152 2
        }
153 36
        if ($count > 1) {
154 2
            throw new \LogicException('There should be only one sidebar.');
155
        }
156
        
157 34
        if ($count) {
158 4
            $this->table->setData($data);
159 2
        }
160 34
    }
161
    
162
    /**
163
     * Updates the parameters with new values
164
     * @param array $params
165
     * @throws \InvalidArgumentException
166
     */
167 40
    protected function setParams($params) {
168 40
        $unknownParams = array_diff(array_keys($params), array_keys($this->params));
169 40
        if (count($unknownParams)) {
170 2
            throw new \InvalidArgumentException('Unknown parameter(-s): ' . implode(', ', $unknownParams));
171
        }
172 38
        $this->params = array_merge($this->params, $params);
173 38
    }
174
175
    /**
176
     * Get the specified parameter
177
     * @param string $key
178
     * @return mixed
179
     */
180 38
    protected function getParam($key) {
181 38
        return $this->params[$key];
182
    }
183
184
    /**
185
     * Adds the top border to the output
186
     * @return void
187
     */
188 34
    protected function addTopBorder() {
189 34
        if ($this->metadata['top_border']) {
190 30
            $this->addRow(
191 30
                $this->metadata['top_border']['left'],
192 30
                $this->metadata['top_border']['middle'],
193 30
                $this->metadata['top_border']['right'],
194 30
                $this->metadata['top_border']['pad']
195 15
            );
196 15
        }
197 34
    }
198
199
    /**
200
     * Adds the header row to the output
201
     * @return void
202
     */
203 34
    protected function addHeader() {
204 34
        if ($this->useHeader) {
205 16
            $data = $this->table->getData();
206 16
            $x = $this->table->getDimensionX();
207 16
            $y = $this->table->getDimensionY();
208 16
            $this->addDataRow(array_shift($data));
209 16
            $this->addHeaderBorder();
210 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...
211 8
        }
212 34
    }
213
    
214 10
    final protected function getAbcRange($length) {
215 10
        $baseRange = range('A', 'Z');
216 10
        $range = range('A', 'Z');
217 10
        while (count($range) < $length && count($baseRange) > 1) {
218 2
            $letter = array_shift($baseRange);
219 2
            $tmpRange = range('A', 'Z');
220
            array_walk($tmpRange, function(&$e) use ($letter) { $e = $letter . $e; });
221 2
            $range = array_merge($range, $tmpRange);
222 1
        }
223 10
        return array_slice($range, 0, $length);
224
    }
225
226
    /**
227
     * Adds the rows to the output
228
     */
229 34
    protected function addRows() {
230 34
        $data = $this->table->getData();
231 34
        array_walk($data, [$this, 'addDataRow']);
232 34
    }
233
234
    /**
235
     * Adds the row with the specified data to the output
236
     * @param array $row
237
     * @return void
238
     */
239 34
    protected function addDataRow($row) {
240 34
        $this->addRow(
241 34
            $this->metadata['content']['left'],
242 34
            $this->metadata['content']['middle'],
243 34
            $this->metadata['content']['right'],
244 34
            $this->metadata['content']['pad'],
245 17
            $row
246 17
        );
247 34
    }
248
249
    /**
250
     * Adds the bottom border of the header to the output
251
     * @return void
252
     */
253 16
    protected function addHeaderBorder() {
254 16
        $this->addRow(
255 16
            $this->metadata['header']['left'],
256 16
            $this->metadata['header']['middle'],
257 16
            $this->metadata['header']['right'],
258 16
            $this->metadata['header']['pad']
259 8
        );
260 16
    }
261
262
    /**
263
     * Adds the bottom border to the output
264
     * @return void
265
     */
266 34
    protected function addBottomBorder() {
267 34
        if ($this->metadata['bottom_border']) {
268 30
            $this->addRow(
269 30
                $this->metadata['bottom_border']['left'],
270 30
                $this->metadata['bottom_border']['middle'],
271 30
                $this->metadata['bottom_border']['right'],
272 30
                $this->metadata['bottom_border']['pad']
273 15
            );
274 15
        }
275 34
    }
276
277
    /**
278
     * Adds the row to the output
279
     * @param string $lb
280
     * @param string $bm
281
     * @param string $br
282
     * @param string $pad
283
     * @param array $row
284
     * @return void
285
     */
286 34
    protected function addRow($lb, $bm, $br, $pad, $row = []) {
287 34
        $delimiter = $lb;
288 34
        for ($i = 0; $i < $this->table->getDimensionX(); $i++) {
289 34
            $maxLength = $this->table->getColumnsMaxLength($i);
290 34
            $this->output .= $delimiter;
291 34
            if (count($row)) {
292 34
                $spaces = str_repeat($pad, $maxLength - strlen($row[$i]));
293 34
                $this->output .= " {$row[$i]}{$spaces} ";
294 17
            } else {
295 32
                $this->output .= str_repeat($pad, $maxLength + 2);
296
            }
297 34
            $delimiter = $bm;
298 17
        }
299 34
        $this->output .= $br . PHP_EOL;
300 34
    }
301
302
}
303