1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oliverde8\Component\PhpEtl\Extract\File; |
4
|
|
|
|
5
|
|
|
use Oliverde8\Component\PhpEtl\Model\File\AbstractCsvFile; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Csv |
9
|
|
|
* |
10
|
|
|
* @author de Cramer Oliver<[email protected]> |
11
|
|
|
* @copyright 2018 Oliverde8 |
12
|
|
|
* @package Oliverde8\Component\PhpEtl\Loader\File |
13
|
|
|
*/ |
14
|
|
|
class Csv extends AbstractCsvFile implements \Iterator |
15
|
|
|
{ |
16
|
|
|
/** @var string[] Current line */ |
17
|
|
|
protected $current; |
18
|
|
|
|
19
|
|
|
/** @var int Number of lines read */ |
20
|
|
|
protected $count = 0; |
21
|
|
|
|
22
|
|
|
/** @var string[] CSV file headers */ |
23
|
|
|
protected $headers = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Initialize the read of the file. |
27
|
|
|
*/ |
28
|
|
|
protected function init() |
29
|
|
|
{ |
30
|
|
|
if (is_null($this->file)) { |
|
|
|
|
31
|
|
|
$this->file = fopen($this->filePath, 'r'); |
32
|
|
|
$headers = fgetcsv($this->file, 0, $this->delimiter, $this->enclosure, $this->escape); |
33
|
|
|
|
34
|
|
|
if (is_null($this->headers)) { |
35
|
|
|
$this->headers = $headers; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->next(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return file headers |
44
|
|
|
* |
45
|
|
|
* @return \string[] |
46
|
|
|
*/ |
47
|
|
|
public function getHeaders() |
48
|
|
|
{ |
49
|
|
|
$this->init(); |
50
|
|
|
|
51
|
|
|
return $this->headers; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set file headers |
56
|
|
|
* |
57
|
|
|
* @param array $headers File headers to set |
58
|
|
|
*/ |
59
|
|
|
public function setHeaders($headers) |
60
|
|
|
{ |
61
|
|
|
$this->init(); |
62
|
|
|
|
63
|
|
|
$this->headers = $headers; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return the current element |
68
|
|
|
* |
69
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
70
|
|
|
* @return mixed Can return any type. |
71
|
|
|
* @since 5.0.0 |
72
|
|
|
*/ |
73
|
|
|
public function current() |
74
|
|
|
{ |
75
|
|
|
$this->init(); |
76
|
|
|
|
77
|
|
|
return $this->current; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Move forward to next element |
82
|
|
|
* |
83
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
84
|
|
|
* @return void Any returned value is ignored. |
85
|
|
|
* @since 5.0.0 |
86
|
|
|
*/ |
87
|
|
|
public function next() |
88
|
|
|
{ |
89
|
|
|
$this->init(); |
90
|
|
|
$this->count++; |
91
|
|
|
|
92
|
|
|
$current = fgetcsv($this->file, 0, $this->delimiter, $this->enclosure, $this->escape); |
93
|
|
|
|
94
|
|
|
if ($current) { |
95
|
|
|
$this->current = array_combine($this->headers, $current); |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->current = false; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the key of the current element |
104
|
|
|
* |
105
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
106
|
|
|
* @return mixed scalar on success, or null on failure. |
107
|
|
|
* @since 5.0.0 |
108
|
|
|
*/ |
109
|
|
|
public function key() |
110
|
|
|
{ |
111
|
|
|
$this->init(); |
112
|
|
|
|
113
|
|
|
return $this->count; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Checks if current position is valid |
118
|
|
|
* |
119
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
120
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
121
|
|
|
* Returns true on success or false on failure. |
122
|
|
|
* @since 5.0.0 |
123
|
|
|
*/ |
124
|
|
|
public function valid() |
125
|
|
|
{ |
126
|
|
|
$this->init(); |
127
|
|
|
|
128
|
|
|
return $this->file !== false && $this->current() != false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Rewind the Iterator to the first element |
133
|
|
|
* |
134
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
135
|
|
|
* @return void Any returned value is ignored. |
136
|
|
|
* @since 5.0.0 |
137
|
|
|
*/ |
138
|
|
|
public function rewind() |
139
|
|
|
{ |
140
|
|
|
if (!is_null($this->file)) { |
|
|
|
|
141
|
|
|
fclose($this->file); |
142
|
|
|
$this->count = 0; |
143
|
|
|
$this->file = null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->init(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|