Passed
Branch test (b47ac4)
by Roman
02:07
created

Table::rotate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 4
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 24
    public function getData() {
56 24
        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
        $this->columnsMaxLenght = [];
70 34
    }
71
72
    /**
73
     * @param array $data
74
     */
75 8
    public function setData($data) {
76 8
        $this->data = $data;
77 8
        $this->calculateDimensions();
78 8
    }
79
80
    /**
81
     * @return mixed
82
     */
83 26
    public function getDimensionX() {
84 26
        return $this->dimensionX;
85
    }
86
87
    /**
88
     * @return mixed
89
     */
90 4
    public function getDimensionY() {
91 4
        return $this->dimensionY;
92
    }
93
    
94
    /**
95
     * Rotate the table
96
     * @param int $angle 90, 180, 270, -90, -180, -270
97
     * @return void
98
     * @throws \InvalidArgumentException
99
     */
100 4
    public function rotate($angle) {
101 4
        if (in_array($angle, [90, -270])) {
102 2
            $this->rotateClockwise();
103 2
            return;
104
        }
105 4
        if (in_array($angle, [-90, 270])) {
106 2
            $this->rotateCounterclockwise();
107 2
            return;
108
        }
109 4
        if (in_array($angle, [180, -180])) {
110 2
            $this->rotateClockwise()->rotateClockwise();
111 2
            return;
112
        }
113 2
        throw new \InvalidArgumentException("The angle should be one of the following: 90, 180, 270, -90, -180, -270");
114
    }
115
    
116
    /**
117
     * Rotate the data clockwise
118
     * @return $this
119
     */
120 2
    private function rotateClockwise() {
121 2
        $data = [];
122 2
        for ($i = count($this->data) - 1; $i >= 0; $i--) {
123 2
            foreach ($this->data[$i] as $key => $val) {
124 2
                $data[$key][] = $val;
125 1
            }
126 1
        }
127 2
        $this->setData($data);
128 2
        return $this;
129
    }
130
    
131
    /**
132
     * Rotate the data counterclockwise
133
     * @return $this
134
     */
135 2
    private function rotateCounterclockwise() {
136 2
        $data = [];
137 2
        for ($i = $this->getDimensionX() - 1; $i >= 0; $i--) {
138 2
            $row = [];
139 2
            foreach ($this->data as $val) {
140 2
                $row[] = $val[$i];
141 1
            }
142 2
            $data[] = $row;
143 1
        }
144 2
        $this->setData($data);
145 2
        return $this;
146
    }
147
148
}
149