Passed
Push — master ( 1a6394...f0f126 )
by Andy
01:22
created

Row::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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