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 = []; |
101
|
|
|
switch($angle) { |
102
|
4 |
|
case 90: |
103
|
2 |
|
for($i = count($this->data) - 1; $i >= 0; $i--) { |
104
|
2 |
|
foreach($this->data[$i] as $key => $val) { |
105
|
2 |
|
$data[$key][] = $val; |
106
|
1 |
|
} |
107
|
1 |
|
} |
108
|
2 |
|
break; |
109
|
4 |
|
case 180: |
110
|
2 |
|
$this->rotate(90); |
111
|
2 |
|
$this->rotate(90); |
112
|
2 |
|
return; |
113
|
4 |
|
case 270: |
114
|
2 |
|
$this->rotate(-90); |
115
|
2 |
|
return; |
116
|
4 |
|
case -90: |
117
|
2 |
|
for ($i = $this->getDimensionX() -1; $i >= 0; $i--) { |
118
|
2 |
|
$row = []; |
119
|
2 |
|
foreach ($this->data as $val) { |
120
|
2 |
|
$row[] = $val[$i]; |
121
|
1 |
|
} |
122
|
2 |
|
$data[] = $row; |
123
|
1 |
|
} |
124
|
2 |
|
break; |
125
|
4 |
|
case -180: |
126
|
2 |
|
$this->rotate(90); |
127
|
2 |
|
$this->rotate(90); |
128
|
2 |
|
return; |
129
|
|
|
break; |
|
|
|
|
130
|
4 |
|
case -270: |
131
|
2 |
|
$this->rotate(90); |
132
|
2 |
|
return; |
133
|
1 |
|
default: |
134
|
2 |
|
throw new \InvalidArgumentException("The angle should be one of the following: 90, 180, 270, -90, -180, -270"); |
135
|
1 |
|
} |
136
|
2 |
|
$this->data = $data; |
137
|
2 |
|
$this->calculateDimensions(); |
138
|
2 |
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.