Table::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
4
namespace Hyperized\Benchmark\Generic;
5
6
/**
7
 * Class Table
8
 *
9
 * Source from: https://gist.github.com/RamadhanAmizudin/ca87f7be83c6237bb070
10
 *
11
 * @package Hyperized\Benchmark\Generic
12
 */
13
class Table
14
{
15
    /**
16
     * @var int
17
     */
18
    private static $spacingX = 1;
19
    /**
20
     * @var int
21
     */
22
    private static $spacingY = 0;
23
    /**
24
     * @var string
25
     */
26
    private static $jointCharacter = '+';
27
    /**
28
     * @var string
29
     */
30
    private static $lineXCharacter = '-';
31
    /**
32
     * @var string
33
     */
34
    private static $lineYCharacter = '|';
35
    /**
36
     * @var string
37
     */
38
    private static $newLine = "\n";
39
40
    /**
41
     * @var
42
     */
43
    private $tableArray;
44
    /**
45
     * @var
46
     */
47
    private $columnHeaders;
48
    /**
49
     * @var
50
     */
51
    private $columnLength;
52
    /**
53
     * @var
54
     */
55
    private $rowSeparator;
56
    /**
57
     * @var
58
     */
59
    private $rowSpacer;
60
    /**
61
     * @var
62
     */
63
    private $rowHeaders;
64
65
    /**
66
     * @param array $tableArray
67
     */
68
    public function __construct(array $tableArray)
69
    {
70
        $this->tableArray = $tableArray;
71
        $this->columnHeaders = $this->columnHeaders($this->tableArray);
72
        $this->columnLength = $this->columnLengths($this->tableArray, $this->columnHeaders);
73
        $this->rowSeparator = $this->rowSeparator($this->columnLength);
74
        $this->rowSpacer = $this->rowSpacer($this->columnLength);
75
        $this->rowHeaders = $this->rowHeaders($this->columnHeaders, $this->columnLength);
76
77
        $this->render();
78
    }
79
80
    /**
81
     * @param $table
82
     *
83
     * @return array
84
     */
85
    private function columnHeaders($table): array
86
    {
87
        return \array_keys(\reset($table));
88
    }
89
90
    /**
91
     * @param $table
92
     * @param $columnHeaders
93
     *
94
     * @return array
95
     */
96
    private function columnLengths($table, $columnHeaders): array
97
    {
98
        $lengths = [];
99
        foreach ($columnHeaders as $header) {
100
            $header_length = \strlen($header);
101
            $max = $header_length;
102
            foreach ($table as $row) {
103
                $length = \strlen($row[$header]);
104
                if ($length > $max) {
105
                    $max = $length;
106
                }
107
            }
108
109
            if (($max % 2) !== ($header_length % 2)) {
110
                ++$max;
111
            }
112
113
            $lengths[$header] = $max;
114
        }
115
116
        return $lengths;
117
    }
118
119
    /**
120
     * @param $columnLengths
121
     *
122
     * @return string
123
     */
124
    private function rowSeparator($columnLengths): string
125
    {
126
        $row = '';
127
        foreach ($columnLengths as $columnLength) {
128
            $row .= self::$jointCharacter . \str_repeat(self::$lineXCharacter,
129
                    (self::$spacingX * 2) + $columnLength);
130
        }
131
        $row .= self::$jointCharacter;
132
133
        return $row;
134
    }
135
136
    /**
137
     * @param $columnLengths
138
     *
139
     * @return string
140
     */
141
    private function rowSpacer($columnLengths): string
142
    {
143
        $row = '';
144
        foreach ($columnLengths as $columnLength) {
145
            $row .= self::$lineYCharacter . \str_repeat(' ', (self::$spacingX * 2) + $columnLength);
146
        }
147
        $row .= self::$lineYCharacter;
148
149
        return $row;
150
    }
151
152
    /**
153
     * @param $columnHeaders
154
     * @param $columnLengths
155
     *
156
     * @return string
157
     */
158
    private function rowHeaders($columnHeaders, $columnLengths): string
159
    {
160
        $row = '';
161
        foreach ($columnHeaders as $header) {
162
            $row .= self::$lineYCharacter . \str_pad($header, (self::$spacingX * 2) + $columnLengths[$header], ' ',
163
                    STR_PAD_BOTH);
164
        }
165
        $row .= self::$lineYCharacter;
166
167
        return $row;
168
    }
169
170
    /**
171
     *
172
     */
173
    private function render(): void
174
    {
175
        echo $this->rowSeparator . self::$newLine;
176
        echo \str_repeat($this->rowSpacer . self::$newLine, self::$spacingY);
177
        echo $this->rowHeaders . self::$newLine;
178
        echo \str_repeat($this->rowSpacer . self::$newLine, self::$spacingY);
179
        echo $this->rowSeparator . self::$newLine;
180
        echo \str_repeat($this->rowSpacer . self::$newLine, self::$spacingY);
181
        foreach ($this->tableArray as $rowCells) {
182
            $rowCells = $this->rowCells($rowCells, $this->columnHeaders, $this->columnLength);
183
            echo $rowCells . self::$newLine;
184
            echo \str_repeat($this->rowSpacer . self::$newLine, self::$spacingY);
185
        }
186
        echo $this->rowSeparator . self::$newLine;
187
    }
188
189
    /**
190
     * @param $rowCells
191
     * @param $columnHeaders
192
     * @param $columnLengths
193
     *
194
     * @return string
195
     */
196
    private function rowCells($rowCells, $columnHeaders, $columnLengths): string
197
    {
198
        $row = '';
199
        foreach ($columnHeaders as $header) {
200
            $row .= self::$lineYCharacter . \str_repeat(' ', self::$spacingX) . \str_pad($rowCells[$header],
201
                    self::$spacingX + $columnLengths[$header], ' ', STR_PAD_RIGHT);
202
        }
203
        $row .= self::$lineYCharacter;
204
205
        return $row;
206
    }
207
208
}