Row   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 15
eloc 24
c 4
b 0
f 0
dl 0
loc 110
ccs 32
cts 32
cp 1
rs 10

12 Methods

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