1 | <?php |
||
13 | class Reader extends AbstractCsv implements \Iterator |
||
14 | { |
||
15 | /** @var string */ |
||
16 | protected $defaultNormalizer = NullNormalizer::class; |
||
17 | /** @var string */ |
||
18 | protected $openMode = 'r'; |
||
19 | /** @var NormalizerInterface[] */ |
||
20 | protected $normalizers = []; |
||
21 | /** @var Row */ |
||
22 | protected $headers; |
||
23 | /** @var Row */ |
||
24 | protected $row; |
||
25 | |||
26 | /** |
||
27 | * @param string $file |
||
28 | * |
||
29 | * @return Reader |
||
30 | */ |
||
31 | public static function read($file) |
||
37 | |||
38 | /** |
||
39 | * @return Row |
||
40 | */ |
||
41 | public function getHeaders() |
||
42 | { |
||
43 | return $this->headers; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param $key |
||
48 | * |
||
49 | * @return mixed |
||
50 | */ |
||
51 | 3 | public function getHeader($key) |
|
59 | |||
60 | /** |
||
61 | * @param mixed $key |
||
62 | * @param NormalizerInterface $normalizer Normalizer instance. |
||
63 | * |
||
64 | * @return $this |
||
65 | */ |
||
66 | public function addNormalizer($key, NormalizerInterface $normalizer) |
||
67 | { |
||
68 | $this->normalizers[$key] = $normalizer; |
||
69 | |||
70 | return $this; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param array|\Traversable $normalizers |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function addNormalizers($normalizers) |
||
79 | { |
||
80 | foreach ($normalizers as $key => $normalizer) { |
||
81 | $this->addNormalizer($key, $normalizer); |
||
82 | } |
||
83 | |||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param mixed $key |
||
89 | * |
||
90 | * @return NormalizerInterface |
||
91 | */ |
||
92 | 3 | public function getNormalizer($key) |
|
93 | { |
||
94 | 3 | if (!isset($this->normalizers[$key])) { |
|
95 | 3 | $class = $this->getDefaultNormalizer(); |
|
96 | |||
97 | 3 | $this->normalizers[$key] = new $class(); |
|
98 | } |
||
99 | |||
100 | 3 | return $this->normalizers[$key]; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Reads the next line in the CSV file |
||
105 | * and returns a Row object from it. |
||
106 | * |
||
107 | * @return Row|null |
||
108 | */ |
||
109 | protected function getCurrentRow() |
||
121 | |||
122 | /** |
||
123 | * @inheritDoc |
||
124 | */ |
||
125 | public function current() |
||
129 | |||
130 | /** |
||
131 | * @inheritDoc |
||
132 | */ |
||
133 | public function next() |
||
137 | |||
138 | /** |
||
139 | * @inheritDoc |
||
140 | */ |
||
141 | public function key() |
||
145 | |||
146 | /** |
||
147 | * @inheritDoc |
||
148 | */ |
||
149 | public function valid() |
||
155 | |||
156 | /** |
||
157 | * @inheritDoc |
||
158 | */ |
||
159 | public function rewind() |
||
167 | |||
168 | /** |
||
169 | * @param string $defaultNormalizer |
||
170 | * |
||
171 | * @return Reader |
||
172 | */ |
||
173 | public function setDefaultNormalizer($defaultNormalizer) |
||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | 3 | public function getDefaultNormalizer() |
|
187 | } |
||
188 |