-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv-reader.php
155 lines (135 loc) · 3.22 KB
/
csv-reader.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
include 'csv-line.php';
class csv_reader implements \Iterator
{
/** @var resource */
private $handle;
/** @var string */
private $delimiter;
/** @var string */
private $enclosure;
/** @var string */
private $escape;
/** @var boolean */
private $parseHeaders;
/** @var bool */
private $clearBom;
/** @var string[] */
private $current;
/** @var integer */
private $position;
/** @var string[] */
private $headers;
/**
* @param resource $handle
* @param bool|string[] $headers
* @param string $delimiter
* @param string $enclosure
* @param string $escape
*/
public function __construct($handle, $headers = true, $delimiter = ',', $enclosure = '"', $escape = '\\', $clearBom = false)
{
if (!is_resource($handle)) {
throw new \LogicException('Handler is not resource');
}
$this->handle = $handle;
$this->parseHeaders = $headers === true;
if (is_array($headers)) {
$this->headers = $headers;
} elseif ($headers === false) {
$this->headers = [];
}
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
$this->escape = $escape;
$this->clearBom = $clearBom;
$this->rewind();
}
/**
* @param array $headers
*/
public function replaceHeaders(array $headers)
{
$this->headers = $headers;
}
/**
* @return string[]
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @param $header
*
* @return bool
*/
public function hasHeader($header)
{
return in_array($header, $this->headers);
}
/**
* @return csv_line
*/
public function current()
{
return $this->current ? new csv_line($this->current, $this->headers) : false;
}
/**
*
*/
public function next()
{
$this->current = $this->readLine();
if ($this->current !== false) {
$this->position++;
}
}
/**
* @return int
*/
public function key()
{
return $this->position;
}
/**
* @return bool
*/
public function valid()
{
return $this->current !== false;
}
/**
*
*/
public function rewind()
{
rewind($this->handle);
$this->position = -1;
if ($this->parseHeaders) {
$parsedHeaders = $this->readLine();
if (is_null($this->headers)) {
$this->headers = $parsedHeaders;
}
}
$this->next();
}
/**
* @return array|false|null
*/
protected function readLine()
{
if ($this->clearBom && $this->position < 0) {
$line = fgets($this->handle);
$line = str_getcsv($this->removeBom($line), $this->delimiter, $this->enclosure, $this->escape);
} else {
$line = fgetcsv($this->handle, 0, $this->delimiter, $this->enclosure, $this->escape);
}
return $line;
}
private function removeBom($string)
{
$bom = pack('H*', 'EFBBBF');
return preg_replace('/^' . $bom . '/', '', $string);
}
}