Passed
Branch test (df826b)
by Roman
02:13
created

Table::rotateClockwise()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 9
cts 9
cp 1
rs 9.6666
cc 3
eloc 6
nc 3
nop 0
crap 3
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 36
    public function __construct($_data, $_dimensionX = null, $_dimensionY = null) {
13 36
        $this->data = $_data;
14 36
        if (is_null($_dimensionX)) {
15 34
            $this->calculateDimensions();
16 17
        } else {
17 4
            $this->dimensionX = $_dimensionX;
18 4
            $this->dimensionY = $_dimensionY;
19
        }
20 36
    }
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 11
            }
46 11
        }
47
48 22
        $this->columnsMaxLenght[$columnIndex] = $width;
49 22
        return $width;
50
    }
51
52
    /**
53
     * @return array
54
     */
55 4
    public function getData() {
56 4
        return $this->data;
57
    }
58
59 34
    private function calculateDimensions() {
60 34
        $this->dimensionY = $this->dimensionX = 0;
61
62 34
        foreach ($this->data as $row) {
63 34
            $cnt = count($row);
64 34
            if ($cnt > $this->dimensionX) {
65 34
                            $this->dimensionX = $cnt;
66 17
            }
67 17
        }
68 34
        $this->dimensionY = count($this->data);
69 34
    }
70
71
    /**
72
     * @param array $data
73
     */
74 6
    public function setData($data) {
75 6
        $this->data = $data;
76 6
        $this->calculateDimensions();
77 6
    }
78
79
    /**
80
     * @return mixed
81
     */
82 26
    public function getDimensionX() {
83 26
        return $this->dimensionX;
84
    }
85
86
    /**
87
     * @return mixed
88
     */
89 24
    public function getDimensionY() {
90 24
        return $this->dimensionY;
91
    }
92
    
93
    /**
94
     * Rotate the table
95
     * @param int $angle 90, 180, 270, -90, -180, -270
96
     * @return void
97
     * @throws \InvalidArgumentException
98
     */
99 4
    public function rotate($angle) {
100 4
        $data = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
101
        switch ($angle) {
102 4
            case 90:
103 4
            case -270:
104 2
                $this->rotateClockwise();
105 2
                return;
106 4
            case -90:
107 4
            case 270:
108 2
                $this->rotateCounterclockwise();
109 2
                return;
110 4
            case 180:
111 4
            case -180:
112 2
                $this->rotateClockwise()->rotateClockwise();
113 2
                return;
114 1
            default:
115 2
                throw new \InvalidArgumentException("The angle should be one of the following: 90, 180, 270, -90, -180, -270");
116 1
        }
117
    }
118
    
119
    /**
120
     * Rotate the data clockwise
121
     * @return $this
122
     */
123 2
    private function rotateClockwise() {
124 2
        $data = [];
125 2
        for ($i = count($this->data) - 1; $i >= 0; $i--) {
126 2
            foreach ($this->data[$i] as $key => $val) {
127 2
                $data[$key][] = $val;
128 1
            }
129 1
        }
130 2
        $this->setData($data);
131 2
        return $this;
132
    }
133
    
134
    /**
135
     * Rotate the data counterclockwise
136
     * @return $this
137
     */
138 2
    private function rotateCounterclockwise() {
139 2
        $data = [];
140 2
        for ($i = $this->getDimensionX() - 1; $i >= 0; $i--) {
141 2
            $row = [];
142 2
            foreach ($this->data as $val) {
143 2
                $row[] = $val[$i];
144 1
            }
145 2
            $data[] = $row;
146 1
        }
147 2
        $this->setData($data);
148 2
        return $this;
149
    }
150
151
}
152