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
|
|
|
public function getReader(): Reader |
22
|
|
|
{ |
23
|
|
|
return $this->reader; |
24
|
|
|
} |
25
|
|
|
|
26
|
8 |
|
public function setReader(Reader $reader): self |
27
|
|
|
{ |
28
|
8 |
|
$this->reader = $reader; |
29
|
|
|
|
30
|
8 |
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Cell[] |
35
|
|
|
*/ |
36
|
|
|
public function getCells(): array |
37
|
|
|
{ |
38
|
|
|
return $this->cells; |
39
|
|
|
} |
40
|
|
|
|
41
|
8 |
|
public function addCells(array $cells): void |
42
|
|
|
{ |
43
|
8 |
|
foreach ($cells as $key => $value) { |
44
|
8 |
|
$key = $this->reader->getHeader($key); |
45
|
8 |
|
$this->addCell($key, $value); |
46
|
|
|
} |
47
|
8 |
|
} |
48
|
|
|
|
49
|
8 |
|
public function addCell($key, $value): void |
50
|
|
|
{ |
51
|
8 |
|
$normalizer = $this->reader->getNormalizer($key); |
52
|
|
|
|
53
|
8 |
|
$cell = new Cell($value, $normalizer); |
54
|
|
|
|
55
|
8 |
|
$this->cells[$key] = $cell; |
56
|
8 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
2 |
|
public function offsetExists($offset): bool |
62
|
|
|
{ |
63
|
2 |
|
return isset($this->cells[$offset]) || \array_key_exists($offset, $this->cells); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
5 |
|
public function offsetGet($offset) |
70
|
|
|
{ |
71
|
5 |
|
return $this->cells[$offset]->getValue(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function offsetSet($offset, $value): void |
78
|
|
|
{ |
79
|
|
|
$this->addCell($offset, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function offsetUnset($offset): void |
86
|
|
|
{ |
87
|
|
|
unset($this->cells[$offset]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
1 |
|
public function count(): int |
94
|
|
|
{ |
95
|
1 |
|
return \count($this->cells); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
1 |
|
public function getIterator(): \ArrayIterator |
102
|
|
|
{ |
103
|
1 |
|
return new \ArrayIterator($this->cells); |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
public function toArray(): array |
107
|
|
|
{ |
108
|
2 |
|
$result = []; |
109
|
|
|
|
110
|
2 |
|
foreach ($this->cells as $key => $cell) { |
111
|
2 |
|
$result[$key] = $cell->getValue(); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
return $result; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|