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