Passed
Pull Request — master (#80)
by Josh
04:32
created

TableRow::insertCells()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Caxy\HtmlDiff\Table;
4
5
/**
6
 * Class TableRow.
7
 */
8
class TableRow extends AbstractTableElement
9
{
10
    /**
11
     * @var Table
12
     */
13
    protected $table;
14
15
    /**
16
     * @var TableCell[]
17
     */
18
    protected $cells = array();
19
20
    /**
21
     * @return Table
22
     */
23 1
    public function getTable()
24
    {
25 1
        return $this->table;
26
    }
27
28
    /**
29
     * @param Table|null $table
30
     *
31
     * @return $this
32
     */
33 1
    public function setTable(Table $table = null)
34
    {
35 1
        $this->table = $table;
36
37 1
        if ($table && !in_array($this, $table->getRows())) {
38
            $table->addRow($this);
39
        }
40
41 1
        return $this;
42
    }
43
44
    /**
45
     * @return TableCell[]
46
     */
47 1
    public function getCells()
48
    {
49 1
        return $this->cells;
50
    }
51
52
    /**
53
     * @param TableCell $cell
54
     *
55
     * @return $this
56
     */
57 1
    public function addCell(TableCell $cell)
58
    {
59 1
        $this->cells[] = $cell;
60
61 1
        if (!$cell->getRow()) {
62 1
            $cell->setRow($this);
63
        }
64
65 1
        return $this;
66
    }
67
68
    /**
69
     * @param TableCell $cell
70
     */
71
    public function removeCell(TableCell $cell)
72
    {
73
        $key = array_search($cell, $this->cells, true);
74
75
        if ($key !== false) {
76
            unset($this->cells[$key]);
77
            if ($cell->getRow()) {
78
                $cell->setRow(null);
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param int $index
85
     *
86
     * @return TableCell|null
87
     */
88 1
    public function getCell($index)
89
    {
90 1
        return isset($this->cells[$index]) ? $this->cells[$index] : null;
91
    }
92
93
    /**
94
     * @param TableCell[] $cells
95
     * @param null|int    $position
96
     */
97
    public function insertCells($cells, $position = null)
98
    {
99
        if ($position === null) {
100
            $this->cells = array_merge($this->cells, $cells);
101
        } else {
102
            array_splice($this->cells, $position, 0, $cells);
103
        }
104
    }
105
}
106