3 namespace Caxy\HtmlDiff\Table;
8 class Table extends AbstractTableElement
13 protected $rows = array();
18 public function getRows()
24 * @param TableRow $row
26 public function addRow(TableRow $row)
30 if (!$row->getTable()) {
31 $row->setTable($this);
36 * @param TableRow $row
38 public function removeRow(TableRow $row)
40 $key = array_search($row, $this->rows, true);
43 unset($this->rows[$key]);
44 if ($row->getTable()) {
53 * @return null|TableRow
55 public function getRow($index)
57 return isset($this->rows[$index]) ? $this->rows[$index] : null;
61 * @param TableRow[] $rows
62 * @param null|int $position
64 public function insertRows($rows, $position = null)
66 if ($position === null) {
67 $this->rows = array_merge($this->rows, $rows);
69 array_splice($this->rows, $position, 0, $rows);
74 * @param TablePosition $position
76 * @return null|TableCell
78 public function getCellByPosition(TablePosition $position)
80 $row = $this->getRow($position->getRow());
82 return $row ? $row->getCell($position->getCell()) : null;
86 * @param TablePosition $position
89 * @return TablePosition|null
91 public function getPositionBefore(TablePosition $position, $offset = 1)
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();
101 while ($cellsToMove > 0 && $newRow >= 0) {
102 if ($cellsToMove > $newCell) {
108 $cellsToMove = $cellsToMove - ($newCell + 1);
109 $cellCount = count($this->getRow($newRow)->getCells());
110 $newCell = $cellCount - 1;
112 $newCell = $newCell - $cellsToMove;
113 $cellsToMove -= $newCell;
120 if ($newRow >= 0 && $newCell >= 0) {
121 return new TablePosition($newRow, $newCell);
128 * @param TablePosition $position
131 * @return TablePosition|null
133 public function getPositionAfter(TablePosition $position, $offset = 1)
135 $cellsToMove = $offset;
136 $newRow = $position->getRow();
137 $newCell = $position->getCell();
139 while ($cellsToMove > 0 && $newRow < count($this->rows)) {
140 $cellCount = count($this->getRow($newRow)->getCells());
142 $cellsLeft = $cellCount - $newCell - 1;
144 if ($cellsToMove > $cellsLeft) {
146 $cellsToMove -= $cellsLeft - 1;
149 $newCell = $newCell + $cellsToMove;
150 $cellsToMove -= $cellsLeft;
154 if ($newRow >= 0 && $newCell >= 0) {
155 return new TablePosition($newRow, $newCell);