Passed
Branch test (e21cbb)
by Roman
02:20
created

AbstractFormatter::addLine()   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
dl 0
loc 14
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 5
crap 3
1
<?php
2
3
namespace ToolkitLab\ASCII\Formatter;
4
5
use ToolkitLab\ASCII\FormatterInterface;
6
use ToolkitLab\ASCII\Table;
7
8
abstract class AbstractFormatter implements FormatterInterface {
9
10
    /**
11
     * @var Table
12
     */
13
    protected $table;
14
    protected $metadata = [];
15
    protected $output = '';
16
17 20
    public function format(Table $table, $firstRowAsHeader = true) {
18 20
        $this->table = $table;
19 20
        $this->output = '';
20 20
        $data = $table->getData();
21 20
        $this->addTopBorder();
22 20
        if ($firstRowAsHeader) {
23 10
            $this->addRow(array_shift($data));
24 10
            $this->addHeaderBorder();
25 5
        }
26 20
        array_walk($data, [$this, 'addRow']);
27 20
        $this->addBottomBorder();
28 20
        return $this->output;
29
    }
30
31
32 20
    protected function addTopBorder() {
33 20
        if ($this->metadata['topBorder']) {
34 16
            $this->addLine(
35 16
                $this->metadata["HBL"],
36 16
                $this->metadata["HBM"],
37 16
                $this->metadata["HBR"],
38 16
                $this->metadata["HPAD"]
39 8
            );
40 8
        }
41 20
    }
42
43 20
    protected function addBottomBorder() {
44 20
        if ($this->metadata['bottomBorder']) {
45 16
            $this->addLine(
46 16
                    $this->metadata["FBL"], 
47 16
                    $this->metadata["FBM"], 
48 16
                    $this->metadata["FBR"], 
49 16
                    $this->metadata["FPAD"]
50 8
            );
51 8
        }
52 20
    }
53
54 10
    protected function addHeaderBorder() {
55 10
        $this->addLine(
56 10
                $this->metadata["H2BL"], 
57 10
                $this->metadata["H2BM"], 
58 10
                $this->metadata["H2BR"], 
59 10
                $this->metadata["H2PAD"]
60 5
        );
61 10
    }
62
    
63
        
64 20
    protected function addRow($values) {
65 20
        $this->addLine(
66 20
            $this->metadata["BL"],
67 20
            $this->metadata["BM"],
68 20
            $this->metadata["BR"],
69 20
            ' ',
70 10
            $values
71 10
        );
72 20
    }
73
74 20
    protected function addLine($lb, $bm, $br, $pad = ' ', $values = []) {
75 20
        $delimiter = $lb;
76 20
        for ($i = 0; $i < $this->table->getDimensionX(); $i++) {
77 20
            $maxLength = $this->table->getColumnsMaxLenght($i);
78 20
            $this->output .= $delimiter;
79 20
            if (count($values)) {
80 20
                $spaces = str_repeat($pad, $maxLength - strlen($values[$i]));
81 20
                $this->output .= " {$values[$i]}{$spaces} ";
82 10
            } else {
83 18
                $this->output .= str_repeat($pad, $maxLength + 2);
84
            }
85 20
            $delimiter = $bm;
86 10
        }
87 20
        $this->output .= $br . PHP_EOL;
88 20
    }
89
    
90
91
92
}
93