Passed
Branch test (96f424)
by Roman
07:30
created

AbstractFormatter::addRow()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 13
cts 13
cp 1
rs 9.4285
cc 3
eloc 11
nc 3
nop 5
crap 3
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_FIRST_COLUMN_MODE = 0X8,
17
        SIDEBAR_NUMERIC_MODE      = 0X10,
18
        SIDEBAR_ABC_MODE          = 0x20,
19
            
20
        SPREADSHEET_MODE          = 0X14;
21
22
    /**
23
     * @var Table
24
     */
25
    protected $table;
26
27
    /**
28
     * @var array
29
     */
30
    protected $metadata = [];
31
32
    /**
33
     * @var string
34
     */
35
    protected $output = '';
36
37
    /**
38
     * The data to be formatted
39
     * @var array
40
     */
41
    protected $data = [];
42
43
    /**
44
     * Formatting parameters
45
     * @var array
46
     */
47
    protected $params = [
48
        'mode' => self::DEFAULT_MODE,
49
        'rotate' => false,
50
        'max_cell_length' => 100,
51
        'max_cell_ending' => '...',
52
    ];
53
54
    /**
55
     * Constructor
56
     * @param array $params
57
     */
58 26
    public function __construct($params = []) {
59 26
        $this->setParams($params);
60 24
    }
61
62
    /**
63
     * Converts an array into ASCII-formatted string (table)
64
     * @param Table $table
65
     * @param array $params
66
     * @return string
67
     */
68 24
    public function format($table, $params = []) {
69 24
        if (is_array($table)) {
0 ignored issues
show
introduced by
The condition is_array($table) can never be true.
Loading history...
70 24
            $table = new Table($table);
71 24
        }
72 24
        $this->init($table, $params);
73 24
        $this->addTopBorder();
74 24
        $this->addHeader();
75 24
        $this->addRows();
76 24
        $this->addBottomBorder();
77 24
        return $this->output;
78
    }
79
80
    /**
81
     * Initializes the data/parameters before formatting
82
     * @param Table $table
83
     * @param array $params
84
     * @return void
85
     */
86 24
    protected function init(Table $table, $params = []) {
87 24
        $this->output = '';
88 24
        $this->setParams($params);
89 24
        $this->table = $table;
90 24
        if ($this->getParam('rotate') !== false) {
91 4
            $this->table->rotate($this->getParam('rotate'));
92 4
        }
93 24
        $maxLength = $this->getParam('max_cell_length');
94 24
        $ending = $this->getParam('max_cell_ending');
95 24
        $this->table->truncate($maxLength, $ending);
96 24
    }
97
    
98
    /**
99
     * Updates the parameters with new values
100
     * @param array $params
101
     * @throws \InvalidArgumentException
102
     */
103 26
    protected function setParams($params) {
104 26
        $unknownParams = array_diff(array_keys($params), array_keys($this->params));
105 26
        if (count($unknownParams)) {
106 2
            throw new \InvalidArgumentException('Unknown parameter(-s): ' . implode(', ', $unknownParams));
107
        }
108 24
        $this->params = array_merge($this->params, $params);
109 24
    }
110
111
    /**
112
     * Get the specified parameter
113
     * @param string $key
114
     * @return mixed
115
     */
116 24
    protected function getParam($key) {
117 24
        return $this->params[$key];
118
    }
119
120
    /**
121
     * Adds the top border to the output
122
     * @return void
123
     */
124 24
    protected function addTopBorder() {
125 24
        if ($this->metadata['top_border']) {
126 20
            $this->addRow(
127 20
                $this->metadata['top_border']['left'],
128 20
                $this->metadata['top_border']['middle'],
129 20
                $this->metadata['top_border']['right'],
130 20
                $this->metadata['top_border']['pad']
131 20
            );
132 20
        }
133 24
    }
134
135
    /**
136
     * Adds the header row to the output
137
     * @return void
138
     */
139 24
    protected function addHeader() {
140 24
        $headerCount = 0;
141 24
        $mode = $this->getParam('mode');
142 24
        $data = $this->table->getData();
143 24
        $dimensionX = $this->table->getDimensionX();
144 24
        $dimensionY = $this->table->getDimensionY();
145 24
        if (($mode & self::HEADER_FIRST_ROW_MODE) === self::HEADER_FIRST_ROW_MODE) {
146 10
            $headerCount++;
147 10
        }
148 24
        if (($mode & self::HEADER_ABC_MODE) === self::HEADER_ABC_MODE) {
149
            // $data = array_merge($this->getAbcRange($dimensionX), $data);
150
            $headerCount++;
151
        }
152 24
        if (($mode & self::HEADER_NUMERIC_MODE) === self::HEADER_NUMERIC_MODE) {
153 2
            $data = array_merge([range(1, $dimensionX)], $data);
154 2
            $headerCount++;
155 2
        }
156 24
        if ($headerCount > 1) {
157
            throw new \LogicException('There should be only one header.');
158
        }
159 24
        if ($headerCount) {
160 12
            $this->addDataRow(array_shift($data));
161 12
            $this->addHeaderBorder();
162 12
            $this->table->setData($data, $dimensionX, $dimensionY);
163 12
        }
164 24
    }
165
166
    /**
167
     * Adds the rows to the output
168
     */
169 24
    protected function addRows() {
170 24
        $data = $this->table->getData();
171 24
        array_walk($data, [$this, 'addDataRow']);
172 24
    }
173
174
    /**
175
     * Adds the row with the specified data to the output
176
     * @param array $row
177
     * @return void
178
     */
179 24
    protected function addDataRow($row) {
180 24
        $this->addRow(
181 24
            $this->metadata['content']['left'],
182 24
            $this->metadata['content']['middle'],
183 24
            $this->metadata['content']['right'],
184 24
            $this->metadata['content']['pad'],
185
            $row
186 24
        );
187 24
    }
188
189
    /**
190
     * Adds the bottom border of the header to the output
191
     * @return void
192
     */
193 12
    protected function addHeaderBorder() {
194 12
        $this->addRow(
195 12
            $this->metadata['header']['left'],
196 12
            $this->metadata['header']['middle'],
197 12
            $this->metadata['header']['right'],
198 12
            $this->metadata['header']['pad']
199 12
        );
200 12
    }
201
202
    /**
203
     * Adds the bottom border to the output
204
     * @return void
205
     */
206 24
    protected function addBottomBorder() {
207 24
        if ($this->metadata['bottom_border']) {
208 20
            $this->addRow(
209 20
                $this->metadata['bottom_border']['left'],
210 20
                $this->metadata['bottom_border']['middle'],
211 20
                $this->metadata['bottom_border']['right'],
212 20
                $this->metadata['bottom_border']['pad']
213 20
            );
214 20
        }
215 24
    }
216
217
    /**
218
     * Adds the row to the output
219
     * @param string $lb
220
     * @param string $bm
221
     * @param string $br
222
     * @param string $pad
223
     * @param array $row
224
     * @return void
225
     */
226 24
    protected function addRow($lb, $bm, $br, $pad, $row = []) {
227 24
        $delimiter = $lb;
228 24
        for ($i = 0; $i < $this->table->getDimensionX(); $i++) {
229 24
            $maxLength = $this->table->getColumnsMaxLength($i);
230 24
            $this->output .= $delimiter;
231 24
            if (count($row)) {
232 24
                $spaces = str_repeat($pad, $maxLength - strlen($row[$i]));
233 24
                $this->output .= " {$row[$i]}{$spaces} ";
234 24
            } else {
235 22
                $this->output .= str_repeat($pad, $maxLength + 2);
236
            }
237 24
            $delimiter = $bm;
238 24
        }
239 24
        $this->output .= $br . PHP_EOL;
240 24
    }
241
242
}
243