Iterator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
}