Completed
Push — master ( afa401...19e2a9 )
by Andy
02:13
created

Row::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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
    private $reader;
12
    /** @var Cell[] $cells */
13
    private $cells = [];
14
15 8
    public function __construct(array $cells, Reader $reader)
16
    {
17 8
        $this->setReader($reader);
18 8
        $this->addCells($cells);
19 8
    }
20
21
    /**
22
     * @return Reader
23
     */
24 8
    public function getReader()
25
    {
26 8
        return $this->reader;
27
    }
28
29
    /**
30
     * @param Reader $reader
31
     *
32
     * @return $this
33
     */
34 8
    public function setReader(Reader $reader)
35
    {
36 8
        $this->reader = $reader;
37
38 8
        return $this;
39
    }
40
41
    /**
42
     * @return Cell[]
43
     */
44
    public function getCells()
45
    {
46
        return $this->cells;
47
    }
48
49
    /**
50
     * @param array $cells
51
     */
52 8
    public function addCells(array $cells)
53
    {
54 8
        foreach ($cells as $key => $value) {
55 8
            $key = $this->getReader()->getHeader($key);
56 8
            $this->addCell($key, $value);
57
        }
58 8
    }
59
60 8
    public function addCell($key, $value)
61
    {
62 8
        $normalizer = $this->getReader()->getNormalizer($key);
63
64 8
        $cell = new Cell($value, $normalizer);
65
66 8
        $this->cells[$key] = $cell;
67 8
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72 2
    public function offsetExists($offset)
73
    {
74 2
        return isset($this->cells[$offset]) || \array_key_exists($offset, $this->cells);
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 5
    public function offsetGet($offset)
81
    {
82 5
        return $this->cells[$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->cells);
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function getIterator()
113
    {
114
        return new \ArrayIterator($this->cells);
115
    }
116
117
    /**
118
     * @return array
119
     */
120 2
    public function toArray()
121
    {
122 2
        $result = [];
123
124 2
        foreach ($this->cells as $key => $cell) {
125 2
            $result[$key] = $cell->getValue();
126
        }
127
128 2
        return $result;
129
    }
130
}
131