Passed
Branch test (279e1a)
by Roman
03:02
created

AbstractFormatter::setParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
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($data, $params = []) {
69 24
        $table = $data instanceof Table ? $data : new Table($data);
70 24
        $this->init($table, $params);
71 24
        $this->addTopBorder();
72 24
        $this->addHeader();
73 24
        $this->addRows();
74 24
        $this->addBottomBorder();
75 24
        return $this->output;
76
    }
77
78
    /**
79
     * Initializes the data/parameters before formatting
80
     * @param Table $table
81
     * @param array $params
82
     * @return void
83
     */
84 24
    protected function init(Table $table, $params = []) {
85 24
        $this->output = '';
86 24
        $this->setParams($params);
87 24
        $this->table = $table;
88 24
        if ($this->getParam('rotate') !== false) {
89 4
            $this->table->rotate($this->getParam('rotate'));
90 2
        }
91 24
        $maxLength = $this->getParam('max_cell_length');
92 24
        $ending = $this->getParam('max_cell_ending');
93 24
        $this->table->truncate($maxLength, $ending);
94 24
    }
95
    
96
    /**
97
     * Updates the parameters with new values
98
     * @param array $params
99
     * @throws \InvalidArgumentException
100
     */
101 26
    protected function setParams($params) {
102 26
        $unknownParams = array_diff(array_keys($params), array_keys($this->params));
103 26
        if (count($unknownParams)) {
104 2
            throw new \InvalidArgumentException('Unknown parameter(-s): ' . implode(', ', $unknownParams));
105
        }
106 24
        $this->params = array_merge($this->params, $params);
107 24
    }
108
109
    /**
110
     * Get the specified parameter
111
     * @param string $key
112
     * @return mixed
113
     */
114 24
    protected function getParam($key) {
115 24
        return $this->params[$key];
116
    }
117
118
    /**
119
     * Adds the top border to the output
120
     * @return void
121
     */
122 24
    protected function addTopBorder() {
123 24
        if ($this->metadata['top_border']) {
124 20
            $this->addRow(
125 20
                $this->metadata['top_border']['left'],
126 20
                $this->metadata['top_border']['middle'],
127 20
                $this->metadata['top_border']['right'],
128 20
                $this->metadata['top_border']['pad']
129 10
            );
130 10
        }
131 24
    }
132
133
    /**
134
     * Adds the header row to the output
135
     * @return void
136
     */
137 24
    protected function addHeader() {
138 24
        $headerCount = 0;
139 24
        $mode = $this->getParam('mode');
140 24
        $data = $this->table->getData();
141 24
        $dimensionX = $this->table->getDimensionX();
142 24
        $dimensionY = $this->table->getDimensionY();
143 24
        if (($mode & self::HEADER_FIRST_ROW_MODE) === self::HEADER_FIRST_ROW_MODE) {
144 10
            $headerCount++;
145 5
        }
146 24
        if (($mode & self::HEADER_ABC_MODE) === self::HEADER_ABC_MODE) {
147
            // $data = array_merge($this->getAbcRange($dimensionX), $data);
148
            $headerCount++;
149
        }
150 24
        if (($mode & self::HEADER_NUMERIC_MODE) === self::HEADER_NUMERIC_MODE) {
151 2
            $data = array_merge([range(1, $dimensionX)], $data);
152 2
            $headerCount++;
153 1
        }
154 24
        if ($headerCount > 1) {
155
            throw new \LogicException('There should be only one header.');
156
        }
157 24
        if ($headerCount) {
158 12
            $this->addDataRow(array_shift($data));
159 12
            $this->addHeaderBorder();
160 12
            $this->table->setData($data, $dimensionX, $dimensionY);
161 6
        }
162 24
    }
163
164
    /**
165
     * Adds the rows to the output
166
     */
167 24
    protected function addRows() {
168 24
        $data = $this->table->getData();
169 24
        array_walk($data, [$this, 'addDataRow']);
170 24
    }
171
172
    /**
173
     * Adds the row with the specified data to the output
174
     * @param array $row
175
     * @return void
176
     */
177 24
    protected function addDataRow($row) {
178 24
        $this->addRow(
179 24
            $this->metadata['content']['left'],
180 24
            $this->metadata['content']['middle'],
181 24
            $this->metadata['content']['right'],
182 24
            $this->metadata['content']['pad'],
183 12
            $row
184 12
        );
185 24
    }
186
187
    /**
188
     * Adds the bottom border of the header to the output
189
     * @return void
190
     */
191 12
    protected function addHeaderBorder() {
192 12
        $this->addRow(
193 12
            $this->metadata['header']['left'],
194 12
            $this->metadata['header']['middle'],
195 12
            $this->metadata['header']['right'],
196 12
            $this->metadata['header']['pad']
197 6
        );
198 12
    }
199
200
    /**
201
     * Adds the bottom border to the output
202
     * @return void
203
     */
204 24
    protected function addBottomBorder() {
205 24
        if ($this->metadata['bottom_border']) {
206 20
            $this->addRow(
207 20
                $this->metadata['bottom_border']['left'],
208 20
                $this->metadata['bottom_border']['middle'],
209 20
                $this->metadata['bottom_border']['right'],
210 20
                $this->metadata['bottom_border']['pad']
211 10
            );
212 10
        }
213 24
    }
214
215
    /**
216
     * Adds the row to the output
217
     * @param string $lb
218
     * @param string $bm
219
     * @param string $br
220
     * @param string $pad
221
     * @param array $row
222
     * @return void
223
     */
224 24
    protected function addRow($lb, $bm, $br, $pad, $row = []) {
225 24
        $delimiter = $lb;
226 24
        for ($i = 0; $i < $this->table->getDimensionX(); $i++) {
227 24
            $maxLength = $this->table->getColumnsMaxLength($i);
228 24
            $this->output .= $delimiter;
229 24
            if (count($row)) {
230 24
                $spaces = str_repeat($pad, $maxLength - strlen($row[$i]));
231 24
                $this->output .= " {$row[$i]}{$spaces} ";
232 12
            } else {
233 22
                $this->output .= str_repeat($pad, $maxLength + 2);
234
            }
235 24
            $delimiter = $bm;
236 12
        }
237 24
        $this->output .= $br . PHP_EOL;
238 24
    }
239
240
}
241