1
|
|
|
<?php |
2
|
|
|
namespace Mathielen\ReportWriteEngine\Engine; |
3
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
5
|
|
|
use Psr\Log\NullLogger; |
6
|
|
|
|
7
|
|
|
class Canvas extends \ArrayObject |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private $pointer = ['X' => 0, 'Y' => 0]; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var LoggerInterface |
14
|
|
|
*/ |
15
|
|
|
private $logger; |
16
|
|
|
|
17
|
7 |
|
public function __construct(LoggerInterface $logger = null, array $input = []) |
18
|
|
|
{ |
19
|
7 |
|
parent::__construct($input); |
20
|
7 |
|
$this->logger = $logger ? $logger : new NullLogger(); |
21
|
7 |
|
} |
22
|
|
|
|
23
|
7 |
|
public function getHeight() |
24
|
|
|
{ |
25
|
7 |
|
return $this->count() === 0 ? 0 : $this->getHighestRow() - $this->getLowestRow() + 1; |
26
|
|
|
} |
27
|
|
|
|
28
|
7 |
|
public function getHighestRow() |
29
|
|
|
{ |
30
|
7 |
|
$highestRow = $this->count() === 0 ? 0 : max(array_keys($this->getArrayCopy())); |
31
|
|
|
|
32
|
7 |
|
return $highestRow; |
33
|
|
|
} |
34
|
|
|
|
35
|
7 |
|
public function getLowestRow() |
36
|
|
|
{ |
37
|
7 |
|
return $this->count() === 0 ? 0 : min(array_keys($this->getArrayCopy())); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getHighestCol() |
41
|
|
|
{ |
42
|
|
|
$highestCol = 0; |
43
|
|
|
foreach ($this as $row) { |
44
|
|
|
foreach ($row as $c => $col) { |
45
|
|
|
$highestCol = max($highestCol, $c); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $highestCol; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setPointer($x, $y = null) |
53
|
|
|
{ |
54
|
|
|
$this->pointer['X'] = is_null($x) ? $this->pointer['X'] : $x; |
55
|
|
|
$this->pointer['Y'] = is_null($y) ? $this->pointer['Y'] : $y; |
56
|
|
|
|
57
|
|
|
$this->logger->debug("Setting pointer to X:" . $this->pointer['X'] . ", Y:" . $this->pointer['Y']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function inc($direction = 'Y', $increment = 1) |
61
|
|
|
{ |
62
|
|
|
$this->pointer[$direction] += $increment; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function write(array $data) |
66
|
|
|
{ |
67
|
|
|
$this->logger->debug("Writing array to canvas", ['array' => $data]); |
68
|
|
|
|
69
|
|
|
$y = $this->pointer['Y']; |
70
|
|
|
|
71
|
|
|
foreach ($data as $row) { |
72
|
|
|
|
73
|
|
|
foreach ($row as $x => $col) { |
74
|
|
|
$this->set($x + $this->pointer['X'], $y, $col); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$y++; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->sort(); |
81
|
|
|
} |
82
|
|
|
|
83
|
7 |
|
private function set($x, $y, $data) |
84
|
|
|
{ |
85
|
7 |
|
$this->logger->debug("Write value to canvas [$x,$y] = " . (is_array($data) ? $data['value'] : $data)); |
86
|
|
|
|
87
|
7 |
|
$this[$y][$x] = $data; |
88
|
7 |
|
} |
89
|
|
|
|
90
|
7 |
|
private function sort() |
91
|
|
|
{ |
92
|
7 |
|
$this->ksort(); |
93
|
7 |
|
foreach ($this as &$row) { |
94
|
7 |
|
ksort($row); |
95
|
|
|
} |
96
|
7 |
|
} |
97
|
|
|
|
98
|
7 |
|
public function insert(Canvas $canvas, $y, $x = 0, $h = 0, $w = 0) |
99
|
|
|
{ |
100
|
7 |
|
$this->logger->debug("Inserting canvas at X:$x, Y:$y. Replacing W:$w, H:$h", ['canvas' => $canvas]); |
101
|
|
|
|
102
|
|
|
//remove height |
103
|
7 |
|
for ($i = $y; $i < $y + $h; $i++) { |
104
|
3 |
|
if (isset($this[$i])) { |
105
|
3 |
|
if ($x > 0) { |
106
|
1 |
|
foreach ($this[$i] as $ci => $col) { |
107
|
1 |
|
if ($ci > $x) { |
108
|
1 |
|
unset($this[$i][$x]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} else { |
112
|
2 |
|
unset($this[$i]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
//make space |
118
|
7 |
|
$insertHeight = $canvas->getHeight(); |
119
|
7 |
|
foreach ($this->getArrayCopy() as $thisRowNum => $row) { |
120
|
6 |
|
if ($thisRowNum >= $y + $h && $x === 0) { |
121
|
2 |
|
$this->logger->debug("Shifting row $thisRowNum to " . ($thisRowNum + $insertHeight) . " due to >= Y:$y + H:$h = " . ($y + $h) . " and X === 0. Insertheight is: $insertHeight", ['row' => $row]); |
122
|
|
|
|
123
|
|
|
//unset($this[$thisRowNum]); |
|
|
|
|
124
|
6 |
|
$this[$thisRowNum + $insertHeight - $h] = $row; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/*$startRowNum = $canvas->getLowestRow(); |
|
|
|
|
129
|
|
|
$insertWidth = $canvas->getHighestCol() - $w; |
130
|
|
|
foreach ($this->getArrayCopy() as $thisRowNum => $row) { |
131
|
|
|
if ($thisRowNum == $startRowNum) { |
132
|
|
|
foreach ($row as $thisColNum => $col) { |
133
|
|
|
if ($thisColNum > $x + $w && $col != '') { |
134
|
|
|
$this[$thisRowNum][$thisColNum + $insertWidth] = $col; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
}*/ |
139
|
|
|
|
140
|
7 |
|
$r = $c = 0; |
141
|
7 |
|
foreach ($canvas as $row) { |
142
|
7 |
|
foreach ($row as $c => $col) { |
143
|
7 |
|
$this->set($x + $c, $y + $r, $col); |
144
|
|
|
} |
145
|
|
|
|
146
|
7 |
|
$r++; |
147
|
|
|
} |
148
|
|
|
|
149
|
7 |
|
$this->sort(); |
150
|
7 |
|
} |
151
|
|
|
|
152
|
|
|
public function __toString() |
153
|
|
|
{ |
154
|
|
|
return json_encode($this->getArrayCopy(), JSON_PRETTY_PRINT); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.