Passed
Push — master ( bbac88...df8a5f )
by Roman
07:16
created

AbstractFormatter::addBottomBorder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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