Iterator::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
/**
4
 * Example:
5
 *
6
 * $csv = new \Csv\Iterator('/path/to/huge/file.csv');
7
 * foreach ($csv->parse() as $row) {
8
 *     // Do something with the row.
9
 * }
10
 */
11
12
namespace AtBase\Csv;
13
14
class Iterator
15
{
16
    protected $file;
17
18
    public function __construct($file)
19
    {
20
        $this->file = fopen($file, 'r');
21
    }
22
23
    public function parse()
24
    {
25
        $headers = array_map('trim', fgetcsv($this->file, 4096));
26
        while (!feof($this->file)) {
27
            $row = array_map('trim', (array)fgetcsv($this->file, 4096));
28
            if (count($headers) !== count($row)) {
29
                continue;
30
            }
31
            $row = array_combine($headers, $row);
32
            yield $row;
33
        }
34
        return;
35
    }
36
}