Repository navigation
Expand file tree
/
Copy pathCSVReader.php
More file actions
124 lines (104 loc) · 2.51 KB
/
Copy pathCSVReader.php
File metadata and controls
124 lines (104 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace Example\Model;
/**
* A simple CSV file reader.
*
* Emulates an array of records (arrays), implimented as an Iterator, so can be used in a foreach statements.
*
* - If your CSV file has headers (the default), then the keys of the returned array will be the header values.
* - You can also specify a different field delimiter, for example ("\t") for tabs.
* - Use rewind to reset to the top of the file.
* - The header record is NEVER returned as a record. The first iteration will be the first record in the file, excluding the header record if specified.
*
* @implements \Iterator<array<string, string>>
*/
class CSVReader implements \Iterator
{
/** @var array<string, string> */
private array $current = [];
private $fh = false; // @phpstan-ignore-line
/** @var array<string> */
private array $headers = [];
private int $index = 0;
public function __construct(private string $fileName, private bool $headerRow = true, private string $delimiter = ',')
{
$this->rewind();
}
public function __destruct()
{
$this->closeFile();
}
/** @return array<string, string> */
public function current() : array
{
return $this->current;
}
public function key() : int
{
return $this->index;
}
public function next() : void
{
$this->current = [];
if ($this->fh)
{
$array = \fgetcsv($this->fh, 0, $this->delimiter, escape:"\\");
if ($array)
{
++$this->index;
if ($this->headers)
{
foreach ($this->headers as $index => $header)
{
if (isset($array[$index]))
{
$this->current[$header] = $array[$index];
}
else
{
break;
}
}
}
else
{
$this->current = $array;
}
}
}
}
public function rewind() : void
{
$this->index = -1;
$this->closeFile();
$this->fh = @\fopen($this->fileName, 'r');
if ($this->fh && $this->headerRow)
{
$this->headers = \fgetcsv($this->fh, 0, $this->delimiter, escape:"\\");
}
$this->next();
}
/**
* You can specify headers if your file does not include them. The headers will be used as the key in the returned associative array for each record.
*
* @param array<string> $headers of strings
*/
public function setHeaders(array $headers) : static
{
$this->headers = $headers;
return $this;
}
public function valid() : bool
{
return $this->current && $this->fh;
}
private function closeFile() : static
{
if ($this->fh)
{
\fclose($this->fh);
$this->fh = false;
}
return $this;
}
}