Row::getNumCells()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Box\Spout\Common\Entity;
4
5
use Box\Spout\Common\Entity\Style\Style;
6
7
class Row
8
{
9
    /**
10
     * The cells in this row
11
     * @var Cell[]
12
     */
13
    protected $cells = [];
14
15
    /**
16
     * The row style
17
     * @var Style
18
     */
19
    protected $style;
20
21
    /**
22
     * Row constructor.
23
     * @param Cell[] $cells
24
     * @param Style|null $style
25
     */
26 193
    public function __construct(array $cells, $style)
27
    {
28
        $this
29 193
            ->setCells($cells)
30 193
            ->setStyle($style);
31 193
    }
32
33
    /**
34
     * @return Cell[] $cells
35
     */
36 149
    public function getCells()
37
    {
38 149
        return $this->cells;
39
    }
40
41
    /**
42
     * @param Cell[] $cells
43
     * @return Row
44
     */
45 193
    public function setCells(array $cells)
46
    {
47 193
        $this->cells = [];
48 193
        foreach ($cells as $cell) {
49 141
            $this->addCell($cell);
50
        }
51
52 193
        return $this;
53
    }
54
55
    /**
56
     * @param Cell $cell
57
     * @param int $cellIndex
58
     * @return Row
59
     */
60 40
    public function setCellAtIndex(Cell $cell, $cellIndex)
61
    {
62 40
        $this->cells[$cellIndex] = $cell;
63
64 40
        return $this;
65
    }
66
67
    /**
68
     * @param int $cellIndex
69
     * @return Cell|null
70
     */
71 2
    public function getCellAtIndex($cellIndex)
72
    {
73 2
        return $this->cells[$cellIndex] ?? null;
74
    }
75
76
    /**
77
     * @param Cell $cell
78
     * @return Row
79
     */
80 167
    public function addCell(Cell $cell)
81
    {
82 167
        $this->cells[] = $cell;
83
84 167
        return $this;
85
    }
86
87
    /**
88
     * @return int
89
     */
90 125
    public function getNumCells()
91
    {
92
        // When using "setCellAtIndex", it's possible to
93
        // have "$this->cells" contain holes.
94 125
        if (empty($this->cells)) {
95 9
            return 0;
96
        }
97
98 119
        return \max(\array_keys($this->cells)) + 1;
99
    }
100
101
    /**
102
     * @return Style
103
     */
104 70
    public function getStyle()
105
    {
106 70
        return $this->style;
107
    }
108
109
    /**
110
     * @param Style|null $style
111
     * @return Row
112
     */
113 193
    public function setStyle($style)
114
    {
115 193
        $this->style = $style ?: new Style();
116
117 193
        return $this;
118
    }
119
120
    /**
121
     * @return array The row values, as array
122
     */
123 85
    public function toArray()
124
    {
125
        return \array_map(function (Cell $cell) {
126 85
            return $cell->getValue();
127 85
        }, $this->cells);
128
    }
129
}
130