|
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
|
|
|
if (is_string($this->filePath)) { |
|
32
|
|
|
$this->setStream(fopen($this->filePath, 'r')); |
|
33
|
|
|
} else { |
|
34
|
|
|
$this->setStream($this->filePath); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setStream($file) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!is_null($this->file)) { |
|
|
|
|
|
|
42
|
|
|
throw new \LogicException("Can't set a stream, stream already open!"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->file = $file; |
|
46
|
|
|
$headers = fgetcsv($this->file, 0, $this->delimiter, $this->enclosure, $this->escape); |
|
47
|
|
|
|
|
48
|
|
|
if (is_null($this->headers)) { |
|
49
|
|
|
$this->headers = $headers; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->next(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return file headers |
|
57
|
|
|
* |
|
58
|
|
|
* @return \string[] |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getHeaders() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->init(); |
|
63
|
|
|
|
|
64
|
|
|
return $this->headers; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set file headers |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $headers File headers to set |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setHeaders($headers) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->init(); |
|
75
|
|
|
|
|
76
|
|
|
$this->headers = $headers; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return the current element |
|
81
|
|
|
* |
|
82
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
|
83
|
|
|
* @return mixed Can return any type. |
|
84
|
|
|
* @since 5.0.0 |
|
85
|
|
|
*/ |
|
86
|
|
|
public function current() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->init(); |
|
89
|
|
|
|
|
90
|
|
|
return $this->current; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Move forward to next element |
|
95
|
|
|
* |
|
96
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
|
97
|
|
|
* @return void Any returned value is ignored. |
|
98
|
|
|
* @since 5.0.0 |
|
99
|
|
|
*/ |
|
100
|
|
|
public function next() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->init(); |
|
103
|
|
|
$this->count++; |
|
104
|
|
|
|
|
105
|
|
|
$current = fgetcsv($this->file, 0, $this->delimiter, $this->enclosure, $this->escape); |
|
106
|
|
|
|
|
107
|
|
|
if ($current) { |
|
|
|
|
|
|
108
|
|
|
$this->current = array_combine($this->headers, $current); |
|
109
|
|
|
return; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->current = false; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Return the key of the current element |
|
117
|
|
|
* |
|
118
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
|
119
|
|
|
* @return mixed scalar on success, or null on failure. |
|
120
|
|
|
* @since 5.0.0 |
|
121
|
|
|
*/ |
|
122
|
|
|
public function key() |
|
123
|
|
|
{ |
|
124
|
|
|
$this->init(); |
|
125
|
|
|
|
|
126
|
|
|
return $this->count; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Checks if current position is valid |
|
131
|
|
|
* |
|
132
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
|
133
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
|
134
|
|
|
* Returns true on success or false on failure. |
|
135
|
|
|
* @since 5.0.0 |
|
136
|
|
|
*/ |
|
137
|
|
|
public function valid() |
|
138
|
|
|
{ |
|
139
|
|
|
$this->init(); |
|
140
|
|
|
|
|
141
|
|
|
return $this->file !== false && $this->current() != false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Rewind the Iterator to the first element |
|
146
|
|
|
* |
|
147
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
|
148
|
|
|
* @return void Any returned value is ignored. |
|
149
|
|
|
* @since 5.0.0 |
|
150
|
|
|
*/ |
|
151
|
|
|
public function rewind() |
|
152
|
|
|
{ |
|
153
|
|
|
if (!is_null($this->file)) { |
|
|
|
|
|
|
154
|
|
|
fclose($this->file); |
|
155
|
|
|
$this->count = 0; |
|
156
|
|
|
$this->file = null; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->init(); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|