Passed
Push — master ( 29d188...e3a586 )
by Roman
03:39
created

Table::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ToolkitLab\ASCII;
4
5
class Table {
6
7
    private $data = [];
8
    private $dimensionX;
9
    private $dimensionY;
10
    private $columnsMaxLenght = [];
11
12 32
    public function __construct($_data, $_dimensionX = null, $_dimensionY = null) {
13 32
        $this->data = $_data;
14 32
        if (is_null($_dimensionX)) {
15 30
            $this->calculateDimensions();
16 30
        } else {
17 4
            $this->dimensionX = $_dimensionX;
18 4
            $this->dimensionY = $_dimensionY;
19
        }
20 32
    }
21
22 28
    public function getCell($x, $y) {
23 28
        if ($x >= $this->dimensionX || $y >= $this->dimensionY) {
24 2
            throw new \InvalidArgumentException("Index Out of range");
25
        }
26 26
        if (!isset($this->data[$y][$x])) {
27 2
            return "";
28
        }
29 26
        return $this->data[$y][$x];
30
    }
31
32
    /**
33
     * @param int $columnIndex
34
     * @return int
35
     */
36 22
    public function getColumnsMaxLenght($columnIndex) {
37 22
        if (isset($this->columnsMaxLenght[$columnIndex])) {
38 20
                    return $this->columnsMaxLenght[$columnIndex];
39
        }
40 22
        $width = 0;
41 22
        for ($y = 0; $y < $this->dimensionY; $y++) {
42 22
            $len = strlen($this->getCell($columnIndex, $y));
43 22
            if ($len > $width) {
44 22
                            $width = $len;
45 22
            }
46 22
        }
47
48 22
        $this->columnsMaxLenght[$columnIndex] = $width;
49 22
        return $width;
50
    }
51
52
    /**
53
     * @return array
54
     */
55 2
    public function getData() {
56 2
        return $this->data;
57
    }
58
59 30
    private function calculateDimensions() {
60 30
        $this->dimensionY = $this->dimensionX = 0;
61
62 30
        foreach ($this->data as $row) {
63 30
            $cnt = count($row);
64 30
            if ($cnt > $this->dimensionX) {
65 30
                            $this->dimensionX = $cnt;
66 30
            }
67 30
        }
68 30
        $this->dimensionY = count($this->data);
69 30
    }
70
71
    /**
72
     * @param array $data
73
     */
74 2
    public function setData($data) {
75 2
        $this->data = $data;
76 2
        $this->calculateDimensions();
77 2
    }
78
79
    /**
80
     * @return mixed
81
     */
82 24
    public function getDimensionX() {
83 24
        return $this->dimensionX;
84
    }
85
86
    /**
87
     * @return mixed
88
     */
89 24
    public function getDimensionY() {
90 24
        return $this->dimensionY;
91
    }
92
93
}
94