-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsv.php
More file actions
297 lines (233 loc) · 8.12 KB
/
Csv.php
File metadata and controls
297 lines (233 loc) · 8.12 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// csv
// class with static methods to easily work with CSV files
final class Csv extends File
{
// config
protected static array $config = [
'mimeGroup'=>'csv', // mime groupe de la classe
'format'=>['delimiter'=>';','enclosure'=>'"','escape'=>'\\'],
'load'=>'csv', // extension permise pour la méthode csv::load
'option'=>['csv'=>true], // option pour la classe
'prefix'=>[ // option csv file::temp
'extension'=>'csv']
];
// getFormat
// retourne les configuration de format pour csv
final public static function getFormat():array
{
return self::$config['format'];
}
// same
// retourne vrai si toutes les colonnes du tableau csv ont le même count et les mêmes clés
final public static function same(array $value):bool
{
return Column::same($value);
}
// clean
// efface toutes les colonnes qui n'ont pas la même longueur et les mêmes clés que la première
// si removeEmpty est true, une colonne dont toutes les valeurs sont vides est éliminé
final public static function clean(array $return,bool $removeEmpty=true):array
{
$return = Column::clean($return);
if($removeEmpty === true && !empty($return))
{
foreach ($return as $key => $value)
{
$remove = true;
if(!empty($value) && is_array($value))
{
foreach ($value as $k => $v)
{
if(!empty($v))
{
$remove = false;
break;
}
}
}
if($remove === true)
unset($return[$key]);
}
}
return $return;
}
// assoc
// la première colonne contient les headers
// le nom des headers est appliqué comme clé à chaque colonne
final public static function assoc(array $array,bool $clean=false,bool $removeEmpty=true):array
{
$return = [];
if($clean === true)
$array = self::clean($array,$removeEmpty);
if(!empty($array) && self::same($array))
{
$header = array_shift($array);
if(!empty($header) && !empty($array))
{
foreach ($array as $key => $value)
{
foreach (array_values($value) as $k => $v)
{
$newKey = Arr::index($k,$header);
if(Arr::isKey($newKey))
$return[$key][$newKey] = $v;
}
}
}
}
return $return;
}
// list
// inverse de assoc
// prend un tableau sans headers mais avec clés associatives
// retourne un tableau avec headers et des colonnes indexés
final public static function list(array $array):array
{
$return = [];
if(self::same($array))
{
$return[0] = [];
$first = current($array);
foreach ($first as $key => $value)
{
$return[0][] = $key;
}
foreach ($array as $key => $value)
{
$newKey = $key + 1;
$return[$newKey] = array_values($value);
}
}
return $return;
}
// strToArr
// parse une string ou un tableau de strings csv et retourne un tableau uni ou multi-dimensionnel
// utilise une resource temporaire pour gérer les enclosures
final public static function strToArr($value,?array $option=null):?array
{
$return = null;
$option = Arr::plus(self::getFormat(),['removeBom'=>false],$option);
if(is_array($value))
{
$value = Arr::clean($value);
$value = Str::lineImplode($value);
}
if(is_string($value) && !empty($value))
{
if($option['removeBom'] === true)
$value = Str::removeBom($value);
$temp = Res::temp('csv');
Res::write($value,$temp);
$return = Res::lines(true,true,$temp,Arr::plus($option,['csv'=>true]));
}
return $return;
}
// arrToStr
// parse un tableau uni ou multi-dimensionnel csv et retourne une string
// utilise une ressource php temp
final public static function arrToStr(array $array,?array $option=null):?string
{
$return = null;
if(!empty($array))
{
$temp = Res::temp('csv');
self::resWrite($array,$temp,$option);
$return = Res::get($temp);
Res::close($temp);
}
return $return;
}
// prepareContent
// méthode utilisé pour préparer le contenu avant écriture dans une resource csv
// peut retourner un tableau ou null
final public static function prepareContent($value):?array
{
$return = null;
if(is_string($value))
$return = [[$value]];
elseif(is_array($value))
{
if(Arrs::is($value))
$return = $value;
else
$return = [$value];
}
return $return;
}
// prepareContentPrepend
// méthode utilisé pour préparer le contenu à ajouter avant le contenu de la resource
// peut retourner un tableau ou null
final public static function prepareContentPrepend(array $prepend,$value,?array $option=null):?array
{
$return = null;
if(self::is($value))
{
$append = self::getLines($value,true,true,$option);
if(is_array($append))
$return = Arr::merge($prepend,$append);
}
return $return;
}
// resLine
// permet de lire une ligne d'un fichier csv, au pointeur
final public static function resLine($value,?array $option=null):?array
{
$return = null;
$option = Arr::plus(self::getFormat(),$option);
if(self::isResource($value))
{
$return = fgetcsv($value,0,$option['delimiter'],$option['enclosure'],$option['escape']);
if($return === false)
$return = null;
}
return $return;
}
// resWrite
// écrit dans une ressource fichier csv, content doit être array uni ou multidimensionnel
// retourne vrai si du contenu a été écrit
// possible d'ajouter le bom si c'est un fichier utf8 (détecte automatique par excel)
// cellSeparator permet de normaliser le caractère newline à l'intérieur d'une cellule
final public static function resWrite(array $content,$value,?array $option=null):bool
{
$return = false;
$option = Arr::plus(self::getFormat(),['latin1'=>false,'separator'=>"\n",'cellSeparator'=>"\n",'bom'=>false],$option);
if(self::isResource($value) && Res::isWritable($value))
{
$put = null;
if(!Arrs::is($content))
$content = [$content];
if($option['bom'] === true && $option['latin1'] !== true && Res::isStart($value))
Res::writeBom($value);
foreach ($content as $write)
{
if(is_array($write))
{
foreach ($write as $k => $v)
{
if(is_string($v))
{
$v = Str::normalizeLine($v,$option['cellSeparator']);
if($option['latin1'] === true)
$v = Encoding::fromUtf8($v);
$write[$k] = $v;
}
}
$put = fputcsv($value,$write,$option['delimiter'],$option['enclosure'],$option['escape'],$option['separator']);
}
}
$return = (is_int($put));
}
return $return;
}
}
// init
Csv::__init();
?>