|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation; |
|
13
|
|
|
|
|
14
|
|
|
use SplFileObject; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* CSV Writer/Reader. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Csv |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* CSV delimiter. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $delimiter = ','; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* CSV enclosure. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $enclosure = '"'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* CSV/SQL columns. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $columns = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $delimiter |
|
46
|
|
|
* @param string $enclosure |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($delimiter = ',', $enclosure = '"') |
|
49
|
|
|
{ |
|
50
|
|
|
$this->delimiter = $delimiter; |
|
51
|
|
|
$this->enclosure = $enclosure; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get list of delimiters. |
|
56
|
|
|
* |
|
57
|
|
|
* @static |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getDelimiters() |
|
62
|
|
|
{ |
|
63
|
|
|
return [ |
|
64
|
|
|
',' => t('Comma'), |
|
65
|
|
|
';' => t('Semi-colon'), |
|
66
|
|
|
'\t' => t('Tab'), |
|
67
|
|
|
'|' => t('Vertical bar'), |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get list of enclosures. |
|
73
|
|
|
* |
|
74
|
|
|
* @static |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function getEnclosures() |
|
79
|
|
|
{ |
|
80
|
|
|
return [ |
|
81
|
|
|
'"' => t('Double Quote'), |
|
82
|
|
|
"'" => t('Single Quote'), |
|
83
|
|
|
'' => t('None'), |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Check boolean field value. |
|
89
|
|
|
* |
|
90
|
|
|
* @static |
|
91
|
|
|
* |
|
92
|
|
|
* @param mixed $value |
|
93
|
|
|
* |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function getBooleanValue($value) |
|
97
|
|
|
{ |
|
98
|
|
|
if (!empty($value)) { |
|
99
|
|
|
$value = trim(strtolower($value)); |
|
100
|
|
|
|
|
101
|
|
|
return $value === '1' || $value[0] |
|
102
|
|
|
=== 't' || $value[0] |
|
103
|
|
|
=== 'y' ? 1 : 0; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return 0; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Output CSV file to standard output. |
|
111
|
|
|
* |
|
112
|
|
|
* @static |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $rows |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function output(array $rows) |
|
117
|
|
|
{ |
|
118
|
|
|
$csv = new static(); |
|
119
|
|
|
$csv->write('php://output', $rows); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Define column mapping between CSV and SQL columns. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $columns |
|
126
|
|
|
* |
|
127
|
|
|
* @return Csv |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setColumnMapping(array $columns) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->columns = $columns; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Read CSV file. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $filename |
|
140
|
|
|
* @param callable $callback Example: function(array $row, $line_number) |
|
141
|
|
|
* |
|
142
|
|
|
* @return Csv |
|
143
|
|
|
*/ |
|
144
|
|
|
public function read($filename, $callback) |
|
145
|
|
|
{ |
|
146
|
|
|
$file = new SplFileObject($filename); |
|
147
|
|
|
$file->setFlags(SplFileObject::READ_CSV); |
|
148
|
|
|
$file->setCsvControl($this->delimiter, $this->enclosure); |
|
149
|
|
|
$line_number = 0; |
|
150
|
|
|
|
|
151
|
|
|
foreach ($file as $row) { |
|
152
|
|
|
$row = $this->filterRow($row); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
if (!empty($row) && $line_number > 0) { |
|
155
|
|
|
call_user_func_array($callback, [$this->associateColumns($row), $line_number]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$line_number++; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Write CSV file. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $filename |
|
168
|
|
|
* @param array $rows |
|
169
|
|
|
* |
|
170
|
|
|
* @return Csv |
|
171
|
|
|
*/ |
|
172
|
|
|
public function write($filename, array $rows) |
|
173
|
|
|
{ |
|
174
|
|
|
$fp = fopen($filename, 'w'); |
|
175
|
|
|
|
|
176
|
|
|
if (is_resource($fp)) { |
|
177
|
|
|
foreach ($rows as $row) { |
|
178
|
|
|
fputcsv($fp, $row, $this->delimiter, $this->enclosure); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
fclose($fp); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Associate columns header with row values. |
|
189
|
|
|
* |
|
190
|
|
|
* @param array $row |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
private function associateColumns(array $row) |
|
195
|
|
|
{ |
|
196
|
|
|
$line = []; |
|
197
|
|
|
$index = 0; |
|
198
|
|
|
|
|
199
|
|
|
foreach ($this->columns as $sql_name => $csv_name) { |
|
200
|
|
|
if (isset($row[$index])) { |
|
201
|
|
|
$line[$sql_name] = $row[$index]; |
|
202
|
|
|
} else { |
|
203
|
|
|
$line[$sql_name] = ''; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$index++; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $line; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Filter empty rows. |
|
214
|
|
|
* |
|
215
|
|
|
* @param array $row |
|
216
|
|
|
* |
|
217
|
|
|
* @return array |
|
218
|
|
|
*/ |
|
219
|
|
|
private function filterRow(array $row) |
|
220
|
|
|
{ |
|
221
|
|
|
return array_filter($row); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.