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
|
40 |
|
public function __construct($data, $dimensionX = null, $dimensionY = null) { |
19
|
40 |
|
$this->setData($data, $dimensionX, $dimensionY); |
20
|
40 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Truncates the values, which are longer that $maxLength |
24
|
|
|
* @param int $maxLength |
25
|
|
|
* @param string $ending |
26
|
|
|
*/ |
27
|
22 |
|
public function truncate($maxLength, $ending = '...') { |
28
|
22 |
|
foreach ($this->data as &$row) { |
29
|
22 |
|
foreach ($row as &$cell) { |
30
|
22 |
|
$cell = trim($cell); |
31
|
22 |
|
if (strlen($cell) > $maxLength) { |
32
|
2 |
|
$cell = substr($cell, 0, $maxLength); |
33
|
12 |
|
$cell = trim($cell, ';,. ') . $ending; |
34
|
1 |
|
} |
35
|
11 |
|
} |
36
|
11 |
|
} |
37
|
22 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the specified cell content |
41
|
|
|
* @param int $x |
42
|
|
|
* @param int $y |
43
|
|
|
* @return string |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
*/ |
46
|
28 |
|
public function getCell($x, $y) { |
47
|
28 |
|
if ($x >= $this->dimensionX || $y >= $this->dimensionY) { |
48
|
2 |
|
throw new \InvalidArgumentException("Index Out of range"); |
49
|
|
|
} |
50
|
26 |
|
if (!isset($this->data[$y][$x])) { |
51
|
2 |
|
return ""; |
52
|
|
|
} |
53
|
26 |
|
return $this->data[$y][$x]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return the maximum length of the column |
58
|
|
|
* @param int $columnIndex |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
22 |
|
public function getColumnsMaxLenght($columnIndex) { |
62
|
22 |
|
if (isset($this->columnsMaxLenght[$columnIndex])) { |
63
|
20 |
|
return $this->columnsMaxLenght[$columnIndex]; |
64
|
|
|
} |
65
|
22 |
|
$width = 0; |
66
|
22 |
|
for ($y = 0; $y < $this->dimensionY; $y++) { |
67
|
22 |
|
$len = strlen($this->getCell($columnIndex, $y)); |
68
|
22 |
|
if ($len > $width) { |
69
|
22 |
|
$width = $len; |
70
|
11 |
|
} |
71
|
11 |
|
} |
72
|
22 |
|
$this->columnsMaxLenght[$columnIndex] = $width; |
73
|
22 |
|
return $width; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the data |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
26 |
|
public function getData() { |
81
|
26 |
|
return $this->data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Calculates the dimensions |
86
|
|
|
*/ |
87
|
38 |
|
private function calculateDimensions() { |
88
|
38 |
|
$this->dimensionY = $this->dimensionX = 0; |
89
|
38 |
|
foreach ($this->data as $row) { |
90
|
38 |
|
$cnt = count($row); |
91
|
38 |
|
if ($cnt > $this->dimensionX) { |
92
|
38 |
|
$this->dimensionX = $cnt; |
93
|
19 |
|
} |
94
|
19 |
|
} |
95
|
38 |
|
$this->dimensionY = count($this->data); |
96
|
38 |
|
$this->columnsMaxLenght = []; |
97
|
38 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets the data and calculates the dimensions (if not specified) |
101
|
|
|
* @param array $data |
102
|
|
|
*/ |
103
|
40 |
|
public function setData($data, $dimensionX = null, $dimensionY = null) { |
104
|
40 |
|
$this->validate($data); |
105
|
40 |
|
$this->data = $data; |
106
|
40 |
|
if (is_null($dimensionX)) { |
107
|
38 |
|
$this->calculateDimensions(); |
108
|
19 |
|
} else { |
109
|
4 |
|
$this->dimensionX = $dimensionX; |
110
|
4 |
|
$this->dimensionY = $dimensionY; |
111
|
|
|
} |
112
|
40 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Validates the data |
116
|
|
|
* @param array $data |
117
|
|
|
* @throws \InvalidArgumentException |
118
|
|
|
*/ |
119
|
40 |
|
protected function validate($data) { |
120
|
40 |
|
$acceptedTypes = ['integer', 'double', 'string']; |
121
|
40 |
|
foreach ($data as $row) { |
122
|
40 |
|
foreach ($row as $cell) { |
123
|
40 |
|
if (!in_array(gettype($cell), $acceptedTypes)) { |
124
|
2 |
|
throw new \InvalidArgumentException( |
125
|
21 |
|
'The values must be one of the following types: ' . implode(',', $acceptedTypes) |
126
|
1 |
|
); |
127
|
|
|
} |
128
|
20 |
|
} |
129
|
20 |
|
} |
130
|
40 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return the length of the dimension X (width of the table) |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
26 |
|
public function getDimensionX() { |
137
|
26 |
|
return $this->dimensionX; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return the length of the dimension Y (height of the table) |
142
|
|
|
* @return int |
143
|
|
|
*/ |
144
|
4 |
|
public function getDimensionY() { |
145
|
4 |
|
return $this->dimensionY; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Rotates the table |
150
|
|
|
* @param int $angle 90, 180, 270, -90, -180, -270 |
151
|
|
|
* @return $this |
152
|
|
|
* @throws \InvalidArgumentException |
153
|
|
|
*/ |
154
|
4 |
|
public function rotate($angle) { |
155
|
4 |
|
if (in_array($angle, [90, -270])) { |
156
|
2 |
|
return $this->rotateClockwise(); |
157
|
4 |
|
} elseif (in_array($angle, [-90, 270])) { |
158
|
2 |
|
return $this->rotateCounterclockwise(); |
159
|
4 |
|
} elseif (in_array($angle, [180, -180])) { |
160
|
2 |
|
return $this->rotateClockwise()->rotateClockwise(); |
161
|
|
|
} |
162
|
2 |
|
throw new \InvalidArgumentException("The angle should be one of the following: 90, 180, 270, -90, -180, -270"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Rotates the data clockwise |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
2 |
|
private function rotateClockwise() { |
170
|
2 |
|
$data = []; |
171
|
2 |
|
for ($i = count($this->data) - 1; $i >= 0; $i--) { |
172
|
2 |
|
foreach ($this->data[$i] as $key => $val) { |
173
|
2 |
|
$data[$key][] = $val; |
174
|
1 |
|
} |
175
|
1 |
|
} |
176
|
2 |
|
$this->setData($data); |
177
|
2 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Rotates the data counterclockwise |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
2 |
|
private function rotateCounterclockwise() { |
185
|
2 |
|
$data = []; |
186
|
2 |
|
for ($i = $this->getDimensionX() - 1; $i >= 0; $i--) { |
187
|
2 |
|
$row = []; |
188
|
2 |
|
foreach ($this->data as $val) { |
189
|
2 |
|
$row[] = $val[$i]; |
190
|
1 |
|
} |
191
|
2 |
|
$data[] = $row; |
192
|
1 |
|
} |
193
|
2 |
|
$this->setData($data); |
194
|
2 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|