-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCSVReader.php
82 lines (70 loc) · 2.36 KB
/
CSVReader.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
<?php
/**
* @copyright Copyright Victor Demin, 2015
* @license https://github.com/ruskid/yii2-csv-importer/LICENSE
* @link https://github.com/ruskid/yii2-csv-importer#README
*/
namespace ruskid\csvimporter;
use Exception;
/**
* CSV Reader
* @author Victor Demin <[email protected]>
*/
class CSVReader {
/**
* @var string the path of the uploaded CSV file on the server.
*/
public $filename;
/**
* FGETCSV() options: length, delimiter, enclosure, escape.
* @var array
*/
public $fgetcsvOptions = ['length' => 0, 'delimiter' => ',', 'enclosure' => '"', 'escape' => "\\"];
/**
* Start insert from line number. Set 1 if CSV file has header.
* @var integer
*/
public $startFromLine = 1;
/**
* @throws Exception
*/
public function __construct() {
$arguments = func_get_args();
if (!empty($arguments)) {
foreach ($arguments[0] as $key => $property) {
if (property_exists($this, $key)) {
$this->{$key} = $property;
}
}
}
if ($this->filename === null) {
throw new Exception(__CLASS__ . ' filename is required.');
}
}
/**
* Will read CSV file into array
* @throws Exception
* @return $array csv filtered data
*/
public function readFile() {
if (!file_exists($this->filename)) {
throw new Exception(__CLASS__ . ' couldn\'t find the CSV file.');
}
//Prepare fgetcsv parameters
$length = isset($this->fgetcsvOptions['length']) ? $this->fgetcsvOptions['length'] : 0;
$delimiter = isset($this->fgetcsvOptions['delimiter']) ? $this->fgetcsvOptions['delimiter'] : ',';
$enclosure = isset($this->fgetcsvOptions['enclosure']) ? $this->fgetcsvOptions['enclosure'] : '"';
$escape = isset($this->fgetcsvOptions['escape']) ? $this->fgetcsvOptions['escape'] : "\\";
$lines = []; //Clear and set rows
if (($fp = fopen($this->filename, 'r')) !== FALSE) {
while (($line = fgetcsv($fp, $length, $delimiter, $enclosure, $escape)) !== FALSE) {
array_push($lines, $line);
}
}
//Remove unused lines from all lines
for ($i = 0; $i < $this->startFromLine; $i++) {
unset($lines[$i]);
}
return $lines;
}
}