Passed
Push — master ( bbac88...df8a5f )
by Roman
07:16
created

Table::rotateCounterclockwise()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 8
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
    /**
13
     * Constructor
14
     * @param array $data
15
     * @param int $dimensionX
16
     * @param int $dimensionY
17
     */
18 36
    public function __construct($data, $dimensionX = null, $dimensionY = null) {
19 36
        $this->setData($data, $dimensionX, $dimensionY);
20 36
    }
21
22
    /**
23
     * Returns the specified cell content
24
     * @param int $x
25
     * @param int $y
26
     * @return string
27
     * @throws \InvalidArgumentException
28
     */
29 28
    public function getCell($x, $y) {
30 28
        if ($x >= $this->dimensionX || $y >= $this->dimensionY) {
31 2
            throw new \InvalidArgumentException("Index Out of range");
32
        }
33 26
        if (!isset($this->data[$y][$x])) {
34 2
            return "";
35
        }
36 26
        return $this->data[$y][$x];
37
    }
38
39
    /**
40
     * Return the maximum length of the column
41
     * @param int $columnIndex
42
     * @return int
43
     */
44 22
    public function getColumnsMaxLenght($columnIndex) {
45 22
        if (isset($this->columnsMaxLenght[$columnIndex])) {
46 20
                    return $this->columnsMaxLenght[$columnIndex];
47
        }
48 22
        $width = 0;
49 22
        for ($y = 0; $y < $this->dimensionY; $y++) {
50 22
            $len = strlen($this->getCell($columnIndex, $y));
51 22
            if ($len > $width) {
52 22
                            $width = $len;
53 11
            }
54 11
        }
55
56 22
        $this->columnsMaxLenght[$columnIndex] = $width;
57 22
        return $width;
58
    }
59
60
    /**
61
     * Returns the data
62
     * @return array
63
     */
64 24
    public function getData() {
65 24
        return $this->data;
66
    }
67
68
    /**
69
     * Calculates the dimensions
70
     */
71 34
    private function calculateDimensions() {
72 34
        $this->dimensionY = $this->dimensionX = 0;
73
74 34
        foreach ($this->data as $row) {
75 34
            $cnt = count($row);
76 34
            if ($cnt > $this->dimensionX) {
77 34
                            $this->dimensionX = $cnt;
78 17
            }
79 17
        }
80 34
        $this->dimensionY = count($this->data);
81 34
        $this->columnsMaxLenght = [];
82 34
    }
83
84
    /**
85
     * Sets the data and calculates the dimensions (if not specified)
86
     * @param array $data
87
     */
88 36
    public function setData($data, $dimensionX = null, $dimensionY = null) {
89 36
        $this->data = $data;
90 36
        if (is_null($dimensionX)) {
91 34
            $this->calculateDimensions();
92 17
        } else {
93 4
            $this->dimensionX = $dimensionX;
94 4
            $this->dimensionY = $dimensionY;
95
        }
96 36
    }
97
98
    /**
99
     * Return the length of the dimension X (width of the table)
100
     * @return int
101
     */
102 26
    public function getDimensionX() {
103 26
        return $this->dimensionX;
104
    }
105
106
    /**
107
     * Return the length of the dimension Y (height of the table)
108
     * @return int
109
     */
110 4
    public function getDimensionY() {
111 4
        return $this->dimensionY;
112
    }
113
    
114
    /**
115
     * Rotates the table
116
     * @param int $angle 90, 180, 270, -90, -180, -270
117
     * @return $this
118
     * @throws \InvalidArgumentException
119
     */
120 4
    public function rotate($angle) {
121 4
        if (in_array($angle, [90, -270])) {
122 2
            return $this->rotateClockwise();
123 4
        } elseif (in_array($angle, [-90, 270])) {
124 2
            return $this->rotateCounterclockwise();
125 4
        } elseif (in_array($angle, [180, -180])) {
126 2
            return $this->rotateClockwise()->rotateClockwise();
127
        }
128 2
        throw new \InvalidArgumentException("The angle should be one of the following: 90, 180, 270, -90, -180, -270");
129
    }
130
    
131
    /**
132
     * Rotates the data clockwise
133
     * @return $this
134
     */
135 2
    private function rotateClockwise() {
136 2
        $data = [];
137 2
        for ($i = count($this->data) - 1; $i >= 0; $i--) {
138 2
            foreach ($this->data[$i] as $key => $val) {
139 2
                $data[$key][] = $val;
140 1
            }
141 1
        }
142 2
        $this->setData($data);
143 2
        return $this;
144
    }
145
    
146
    /**
147
     * Rotates the data counterclockwise
148
     * @return $this
149
     */
150 2
    private function rotateCounterclockwise() {
151 2
        $data = [];
152 2
        for ($i = $this->getDimensionX() - 1; $i >= 0; $i--) {
153 2
            $row = [];
154 2
            foreach ($this->data as $val) {
155 2
                $row[] = $val[$i];
156 1
            }
157 2
            $data[] = $row;
158 1
        }
159 2
        $this->setData($data);
160 2
        return $this;
161
    }
162
163
}
164