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