Passed
Push — main ( c551fb...edb315 )
by Siad
06:32
created

Table::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Util;
22
23
/**
24
 * @author  Siad Ardroumli <[email protected]>
25
 */
26
class Table
27
{
28
    private $header;
29
30
    private $output;
31
32
    private $maxLengths;
33
34 1
    public function __construct(array $header = [], $rows = 0)
35
    {
36 1
        $this->header = $header;
37 1
        $columnSize = ($rows >= 0) ? $rows + 1 : 1;
38 1
        $this->output = array_fill(0, $columnSize, array_fill(0, count($this->header), null));
39 1
        $this->output[0] = $this->header;
40 1
        $this->maxLengths = $this->getHeaderLengths();
41 1
    }
42
43 1
    public function getMaxLengths()
44
    {
45 1
        return $this->maxLengths;
46
    }
47
48 1
    public function put($x, $y, $value)
49
    {
50 1
        $this->maxLengths[$y] = $this->max($y, strlen((string) $value));
51 1
        $this->output[$x][$y] = $value;
52 1
    }
53
54 1
    public function get($x, $y)
55
    {
56 1
        return $this->output[$x][$y];
57
    }
58
59 1
    public function rows()
60
    {
61 1
        return count($this->output);
62
    }
63
64 1
    public function columns()
65
    {
66 1
        return count($this->header);
67
    }
68
69 1
    private function getHeaderLengths()
70
    {
71 1
        return array_map('strlen', $this->header);
72
    }
73
74 1
    private function max($column, $length)
75
    {
76 1
        $max = $length;
77 1
        $total = count($this->output);
78 1
        for ($i = 0; $i < $total; ++$i) {
79 1
            $valueLength = (null !== $this->output[$i][$column]) ? strlen($this->output[$i][$column]) : 0;
80 1
            $max = max([$max, $valueLength]);
81
        }
82
83 1
        return $max;
84
    }
85
}
86