Row::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 12
    }
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 12
    }
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 12
    }
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 7
    public function offsetGet($offset)
69
    {
70 7
        return $this->cells[$offset]->getValue();
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     *
76
     * @param string|int $offset
77
     * @param string     $value
78
     */
79 1
    public function offsetSet($offset, $value): void
80
    {
81 1
        $this->addCell($offset, $value);
82 1
    }
83
84
    /**
85
     * {@inheritDoc}
86
     *
87
     * @param string|int $offset
88
     */
89 1
    public function offsetUnset($offset): void
90
    {
91 1
        unset($this->cells[$offset]);
92 1
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 1
    public function count(): int
98
    {
99 1
        return \count($this->cells);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 1
    public function getIterator(): \ArrayIterator
106
    {
107 1
        return new \ArrayIterator($this->cells);
108
    }
109
110 5
    public function toArray(): array
111
    {
112 5
        $result = [];
113
114 5
        foreach ($this->cells as $key => $cell) {
115 5
            $result[$key] = $cell->getValue();
116
        }
117
118 5
        return $result;
119
    }
120
}
121