Passed
Pull Request — develop_3.0 (#512)
by Adrien
02:47
created

Row::setCells()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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 181
    public function __construct(array $cells, $style)
27
    {
28
        $this
29 181
            ->setCells($cells)
30 181
            ->setStyle($style);
31 181
    }
32
33
    /**
34
     * @return Cell[] $cells
35
     */
36 139
    public function getCells()
37
    {
38 139
        return $this->cells;
39
    }
40
41
    /**
42
     * @param Cell[] $cells
43
     * @return Row
44
     */
45 181
    public function setCells(array $cells)
46
    {
47 181
        $this->cells = [];
48 181
        foreach ($cells as $cell) {
49 130
            $this->addCell($cell);
50
        }
51
52 181
        return $this;
53
    }
54
55
    /**
56
     * @param Cell $cell
57
     * @param mixed $cellIndex
58
     * @parma int $cellIndex
59
     * @return Row
60
     */
61 35
    public function setCellAtIndex(Cell $cell, $cellIndex)
62
    {
63 35
        $this->cells[$cellIndex] = $cell;
64
65 35
        return $this;
66
    }
67
68
    /**
69
     * @param Cell $cell
70
     * @return Row
71
     */
72 156
    public function addCell(Cell $cell)
73
    {
74 156
        $this->cells[] = $cell;
75
76 156
        return $this;
77
    }
78
79
    /**
80
     * @return int
81
     */
82 115
    public function getNumCells()
83
    {
84 115
        return count($this->cells);
85
    }
86
87
    /**
88
     * @return Style
89
     */
90 63
    public function getStyle()
91
    {
92 63
        return $this->style;
93
    }
94
95
    /**
96
     * @param Style|null $style
97
     * @return Row
98
     */
99 181
    public function setStyle($style)
100
    {
101 181
        $this->style = $style ?: new Style();
102
103 181
        return $this;
104
    }
105
106
    /**
107
     * @return array The row values, as array
108
     */
109
    public function toArray()
110
    {
111 82
        return array_map(function (Cell $cell) {
112 82
            return !$cell->isError() ? $cell->getValue() : null;
113 82
        }, $this->cells);
114
    }
115
}
116