|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of dispositif/wikibot application |
|
4
|
|
|
* 2019 : Philippe M. <[email protected]> |
|
5
|
|
|
* For the full copyright and MIT license information, please view the LICENSE file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace App\Infrastructure; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
|
|
14
|
|
|
trait CsvTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Search string in a simple list CSV. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $filename |
|
20
|
|
|
* @param string $search |
|
21
|
|
|
* @param int|null $col |
|
22
|
|
|
* |
|
23
|
|
|
* @return bool |
|
24
|
|
|
* @throws Exception |
|
25
|
|
|
*/ |
|
26
|
|
|
public function isStringInCSV(string $filename, string $search, ?int $col = 0): bool |
|
27
|
|
|
{ |
|
28
|
|
|
return !empty($this->findCSVline($filename, $search, $col)); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $filename |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
* @throws Exception |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getCSVfirstLine(string $filename): array |
|
38
|
|
|
{ |
|
39
|
|
|
if (!file_exists($filename)) { |
|
40
|
|
|
throw new Exception('no file '.$filename); |
|
41
|
|
|
} |
|
42
|
|
|
$f = fopen($filename, 'r'); |
|
43
|
|
|
if ($f === false) { |
|
44
|
|
|
throw new Exception('can not open '.$filename); |
|
45
|
|
|
} |
|
46
|
|
|
$row = fgetcsv($f); |
|
47
|
|
|
fclose($f); |
|
48
|
|
|
|
|
49
|
|
|
return (is_array($row)) ? $row : []; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function findCSVline(string $filename, string $search, ?int $col = 0): array |
|
53
|
|
|
{ |
|
54
|
1 |
|
if (!file_exists($filename)) { |
|
55
|
|
|
throw new Exception('no file '.$filename); |
|
56
|
|
|
} |
|
57
|
1 |
|
$f = fopen($filename, 'r'); |
|
58
|
1 |
|
if ($f === false) { |
|
59
|
|
|
throw new Exception('can not open '.$filename); |
|
60
|
|
|
} |
|
61
|
1 |
|
while ($row = fgetcsv($f)) { |
|
62
|
1 |
|
if (isset($row[$col]) && $row[$col] === $search) { |
|
63
|
1 |
|
return $row; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
fclose($f); |
|
67
|
|
|
|
|
68
|
|
|
return []; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* todo: Ugly. Memory consuming. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $filename |
|
75
|
|
|
* |
|
76
|
|
|
* @throws Exception |
|
77
|
|
|
*/ |
|
78
|
|
|
public function deleteFirstLineCsv(string $filename): void |
|
79
|
|
|
{ |
|
80
|
|
|
if (!file_exists($filename)) { |
|
81
|
|
|
throw new Exception('no file '.$filename); |
|
82
|
|
|
} |
|
83
|
|
|
$f = fopen($filename, 'r'); |
|
84
|
|
|
if ($f === false) { |
|
85
|
|
|
throw new Exception('can not open '.$filename); |
|
86
|
|
|
} |
|
87
|
|
|
while (false !== ($line = fgetcsv($f))) { |
|
88
|
|
|
$data[] = $line; |
|
89
|
|
|
} |
|
90
|
|
|
fclose($f); |
|
91
|
|
|
if (empty($data)) { |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
array_shift($data); |
|
95
|
|
|
$f = fopen($filename, 'w'); |
|
96
|
|
|
if ($f === false) { |
|
97
|
|
|
throw new Exception('can not open '.$filename); |
|
98
|
|
|
} |
|
99
|
|
|
foreach ($data as $fields) { |
|
100
|
|
|
fputcsv($f, $fields); |
|
101
|
|
|
} |
|
102
|
|
|
fclose($f); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function putArrayInCSV(string $filename, array $array) |
|
106
|
|
|
{ |
|
107
|
|
|
// create file if not exists |
|
108
|
|
|
$fp = fopen($filename, 'a+'); |
|
109
|
|
|
if ($fp === false) { |
|
110
|
|
|
throw new Exception('can not open '.$filename); |
|
111
|
|
|
} |
|
112
|
|
|
if (is_array($array[0])) { |
|
113
|
|
|
foreach ($array as $ar) { |
|
114
|
|
|
fputcsv($fp, $ar); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
if (!is_array($array[0])) { |
|
118
|
|
|
fputcsv($fp, $array); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
fclose($fp); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|