-
Notifications
You must be signed in to change notification settings - Fork 1
/
Csv.php
196 lines (156 loc) · 3.53 KB
/
Csv.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Contains class.
*
* PHP version 5
*
* Copyright (c) 2013, Mike Pretzlaw
* All rights reserved.
*
* @category tndb
* @package Csv.php
* @author Mike Pretzlaw <[email protected]>
* @copyright 2013 Mike Pretzlaw
* @license http://github.com/sourcerer-mike/tndb/blob/master/License.md BSD 3-Clause ("BSD New")
* @link http://github.com/sourcerer-mike/tndb
* @since $VERSION$
*/
namespace NoSaladNet;
/**
* Class Csv.
*
* @category tndb
* @author Mike Pretzlaw <[email protected]>
* @copyright 2013 Mike Pretzlaw
* @license http://github.com/sourcerer-mike/tndb/blob/master/License.md BSD 3-Clause ("BSD New")
* @link http://github.com/sourcerer-mike/tndb
* @since $VERSION$
*/
class Csv
{
public $fieldMap;
protected $_delimiter;
protected $_enclosure;
protected $_escape;
protected $_handle;
protected $_fields;
function __construct($file, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
$this->_file = $file;
$this->_delimiter = $delimiter;
$this->_enclosure = $enclosure;
$this->_escape = $escape;
}
/**
* @return mixed
*/
public function getDelimiter()
{
return $this->_delimiter;
}
/**
* @return mixed
*/
public function getEnclosure()
{
return $this->_enclosure;
}
/**
* @return mixed
*/
public function getEscape()
{
return $this->_escape;
}
function getFields()
{
if (null == $this->_fields || !empty($remap))
{ // $this->_fields is null: create
$currentPos = ftell($this->getHandle());
rewind($this->getHandle());
$this->_fields = $this->_getRow();
fseek($this->getHandle(), $currentPos);
foreach ($this->fieldMap as $origin => $new)
{
$pos = array_search($origin, $this->_fields);
if (false !== $pos)
{ // found: change field
$this->_fields[$pos] = $new;
}
}
}
return $this->_fields;
}
function open($mode = 'r')
{
$this->_handle = fopen($this->getFileName(), $mode);
}
/**
* .
*
* @return array
*/
protected function _getRow()
{
return fgetcsv(
$this->getHandle(),
null,
$this->getDelimiter(),
$this->getEnclosure(),
$this->getEscape()
);
}
/**
* .
*
* @return mixed
*/
protected function getFileName()
{
return $this->_file;
}
/**
* .
*
* @return mixed
*/
protected function getHandle()
{
return $this->_handle;
}
public function position($new = null)
{
if (null !== $new)
{
fseek($this->getHandle(), $new);
}
return ftell($this->getHandle());
}
/**
* .
*
* @return array
*/
public function getRow()
{
if ($this->position() == 0)
{ // still beginning: throw away head line
$this->_getRow();
}
$data = $this->_getRow();
if (null === $data || false === $data)
{
return false;
}
return array_combine($this->getFields(), $data);
}
public function getData()
{
$data = array();
while ($row = $this->getRow())
{
$data[] = $row;
}
return $data;
}
}