1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caxy\HtmlDiff\Table; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Table. |
7
|
|
|
*/ |
8
|
|
|
class Table extends AbstractTableElement |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var TableRow[] |
12
|
|
|
*/ |
13
|
|
|
protected $rows = array(); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return TableRow[] |
17
|
|
|
*/ |
18
|
1 |
|
public function getRows() |
19
|
|
|
{ |
20
|
1 |
|
return $this->rows; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param TableRow $row |
25
|
|
|
*/ |
26
|
1 |
|
public function addRow(TableRow $row) |
27
|
|
|
{ |
28
|
1 |
|
$this->rows[] = $row; |
29
|
|
|
|
30
|
1 |
|
if (!$row->getTable()) { |
31
|
1 |
|
$row->setTable($this); |
32
|
|
|
} |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param TableRow $row |
37
|
|
|
*/ |
38
|
|
|
public function removeRow(TableRow $row) |
39
|
|
|
{ |
40
|
|
|
$key = array_search($row, $this->rows, true); |
41
|
|
|
|
42
|
|
|
if ($key !== false) { |
43
|
|
|
unset($this->rows[$key]); |
44
|
|
|
if ($row->getTable()) { |
45
|
|
|
$row->setTable(null); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $index |
52
|
|
|
* |
53
|
|
|
* @return null|TableRow |
54
|
|
|
*/ |
55
|
|
|
public function getRow($index) |
56
|
|
|
{ |
57
|
|
|
return isset($this->rows[$index]) ? $this->rows[$index] : null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param TableRow[] $rows |
62
|
|
|
* @param null|int $position |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function insertRows($rows, $position = null) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if ($position === null) { |
67
|
|
|
$this->rows = array_merge($this->rows, $rows); |
68
|
|
|
} else { |
69
|
|
|
array_splice($this->rows, $position, 0, $rows); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param TablePosition $position |
75
|
|
|
* |
76
|
|
|
* @return null|TableCell |
77
|
|
|
*/ |
78
|
|
|
public function getCellByPosition(TablePosition $position) |
79
|
|
|
{ |
80
|
|
|
$row = $this->getRow($position->getRow()); |
81
|
|
|
|
82
|
|
|
return $row ? $row->getCell($position->getCell()) : null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param TablePosition $position |
87
|
|
|
* @param int $offset |
88
|
|
|
* |
89
|
|
|
* @return TablePosition|null |
90
|
|
|
*/ |
91
|
|
|
public function getPositionBefore(TablePosition $position, $offset = 1) |
92
|
|
|
{ |
93
|
|
|
if ($position->getCell() > ($offset - 1)) { |
94
|
|
|
$newRow = $position->getRow(); |
95
|
|
|
$newCell = $position->getCell() - $offset; |
96
|
|
|
} elseif ($position->getRow() > 0) { |
97
|
|
|
$cellsToMove = $offset; |
98
|
|
|
$newRow = $position->getRow(); |
99
|
|
|
$newCell = $position->getCell(); |
100
|
|
|
|
101
|
|
|
while ($cellsToMove > 0 && $newRow >= 0) { |
102
|
|
|
if ($cellsToMove > $newCell) { |
103
|
|
|
--$newRow; |
104
|
|
|
if ($newRow < 0) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$cellsToMove = $cellsToMove - ($newCell + 1); |
109
|
|
|
$cellCount = count($this->getRow($newRow)->getCells()); |
110
|
|
|
$newCell = $cellCount - 1; |
111
|
|
|
} else { |
112
|
|
|
$newCell = $newCell - $cellsToMove; |
113
|
|
|
$cellsToMove -= $newCell; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} else { |
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($newRow >= 0 && $newCell >= 0) { |
121
|
|
|
return new TablePosition($newRow, $newCell); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param TablePosition $position |
129
|
|
|
* @param int $offset |
130
|
|
|
* |
131
|
|
|
* @return TablePosition|null |
132
|
|
|
*/ |
133
|
|
|
public function getPositionAfter(TablePosition $position, $offset = 1) |
134
|
|
|
{ |
135
|
|
|
$cellsToMove = $offset; |
136
|
|
|
$newRow = $position->getRow(); |
137
|
|
|
$newCell = $position->getCell(); |
138
|
|
|
|
139
|
|
|
while ($cellsToMove > 0 && $newRow < count($this->rows)) { |
140
|
|
|
$cellCount = count($this->getRow($newRow)->getCells()); |
141
|
|
|
|
142
|
|
|
$cellsLeft = $cellCount - $newCell - 1; |
143
|
|
|
|
144
|
|
|
if ($cellsToMove > $cellsLeft) { |
145
|
|
|
++$newRow; |
146
|
|
|
$cellsToMove -= $cellsLeft - 1; |
147
|
|
|
$newCell = 0; |
148
|
|
|
} else { |
149
|
|
|
$newCell = $newCell + $cellsToMove; |
150
|
|
|
$cellsToMove -= $cellsLeft; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($newRow >= 0 && $newCell >= 0) { |
155
|
|
|
return new TablePosition($newRow, $newCell); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.