Passed
Push — master ( b8f9a8...648cdf )
by Andy
02:22
created

Row::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Palmtree\Csv\Row;
4
5
use Palmtree\Csv\Cell\Cell;
6
use Palmtree\Csv\Reader;
7
8
class Row implements \ArrayAccess, \Countable, \IteratorAggregate
9
{
10
    /** @var Reader $reader */
11
    protected $reader;
12
    /** @var Cell[] $cells */
13
    protected $cells = [];
14
15 6
    public function __construct($cells, Reader $reader)
16
    {
17 6
        $this->setReader($reader);
18 6
        $this->addCells($cells);
19 6
    }
20
21
    /**
22
     * @return Reader
23
     */
24 6
    public function getReader()
25
    {
26 6
        return $this->reader;
27
    }
28
29
    /**
30
     * @param Reader $reader
31
     *
32
     * @return $this
33
     */
34 6
    public function setReader(Reader $reader)
35
    {
36 6
        $this->reader = $reader;
37
38 6
        return $this;
39
    }
40
41
    /**
42
     * @return Cell[]
43
     */
44 5
    public function getCells()
45
    {
46 5
        return $this->cells;
47
    }
48
49
    /**
50
     * @param array $cells
51
     */
52 6
    public function addCells(array $cells)
53
    {
54 6
        foreach ($cells as $key => $value) {
55 6
            $key = $this->getReader()->getHeader($key);
56 6
            $this->addCell($key, $value);
57
        }
58 6
    }
59
60 6
    public function addCell($key, $value)
61
    {
62 6
        $normalizer = $this->getReader()->getNormalizer($key);
63
64 6
        $cell = new Cell($value, $normalizer);
65
66 6
        $this->cells[$key] = $cell;
67 6
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72 1
    public function offsetExists($offset)
73
    {
74 1
        return array_key_exists($offset, $this->getCells());
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 4
    public function offsetGet($offset)
81
    {
82 4
        return $this->getCells()[$offset]->getValue();
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function offsetSet($offset, $value)
89
    {
90
        $this->addCell($offset, $value);
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function offsetUnset($offset)
97
    {
98
        unset($this->cells[$offset]);
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104 1
    public function count()
105
    {
106 1
        return count($this->getCells());
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function getIterator()
113
    {
114
        return new \ArrayIterator($this->getCells());
115
    }
116
117
118
    /**
119
     * @return array
120
     */
121
    public function toArray()
122
    {
123
        $result = [];
124
125
        foreach ($this->getCells() as $key => $cell) {
126
            $result[$key] = $cell->getValue();
127
        }
128
129
        return $result;
130
    }
131
}
132