Dispositif /
Wikibot
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * This file is part of dispositif/wikibot application (@github) |
||
| 4 | * 2019-2023 © Philippe M./Irønie <[email protected]> |
||
| 5 | * For the full copyright and MIT license information, 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 | * |
||
| 20 | * @throws Exception |
||
| 21 | */ |
||
| 22 | public function isStringInCSV(string $filename, string $search, ?int $col = 0): bool |
||
| 23 | { |
||
| 24 | return !empty($this->findCSVline($filename, $search, $col)); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @throws Exception |
||
| 29 | */ |
||
| 30 | public function getCSVfirstLine(string $filename): array |
||
| 31 | { |
||
| 32 | if (!file_exists($filename)) { |
||
| 33 | throw new Exception('no file '.$filename); |
||
| 34 | } |
||
| 35 | $f = fopen($filename, 'r'); |
||
| 36 | if ($f === false) { |
||
| 37 | throw new Exception('can not open '.$filename); |
||
| 38 | } |
||
| 39 | $row = fgetcsv($f); |
||
| 40 | fclose($f); |
||
| 41 | |||
| 42 | return (is_array($row)) ? $row : []; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 43 | } |
||
| 44 | |||
| 45 | public function findCSVline(string $filename, string $search, ?int $col = 0): array |
||
| 46 | { |
||
| 47 | if (!file_exists($filename)) { |
||
| 48 | throw new Exception('no file '.$filename); |
||
| 49 | } |
||
| 50 | $f = fopen($filename, 'r'); |
||
| 51 | if ($f === false) { |
||
| 52 | 1 | throw new Exception('can not open '.$filename); |
|
| 53 | } |
||
| 54 | 1 | while ($row = fgetcsv($f)) { |
|
| 55 | if (isset($row[$col]) && $row[$col] === $search) { |
||
| 56 | return $row; |
||
| 57 | 1 | } |
|
| 58 | 1 | } |
|
| 59 | fclose($f); |
||
| 60 | |||
| 61 | 1 | return []; |
|
| 62 | 1 | } |
|
| 63 | 1 | ||
| 64 | /** |
||
| 65 | * todo: Ugly. Memory consuming. |
||
| 66 | * |
||
| 67 | * |
||
| 68 | * @throws Exception |
||
| 69 | */ |
||
| 70 | public function deleteFirstLineCsv(string $filename): void |
||
| 71 | { |
||
| 72 | $data = []; |
||
| 73 | if (!file_exists($filename)) { |
||
| 74 | throw new Exception('no file '.$filename); |
||
| 75 | } |
||
| 76 | $f = fopen($filename, 'r'); |
||
| 77 | if ($f === false) { |
||
| 78 | throw new Exception('can not open '.$filename); |
||
| 79 | } |
||
| 80 | while (false !== ($line = fgetcsv($f))) { |
||
| 81 | $data[] = $line; |
||
| 82 | } |
||
| 83 | fclose($f); |
||
| 84 | if (empty($data)) { |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | array_shift($data); |
||
| 88 | $f = fopen($filename, 'w'); |
||
| 89 | if ($f === false) { |
||
| 90 | throw new Exception('can not open '.$filename); |
||
| 91 | } |
||
| 92 | foreach ($data as $fields) { |
||
| 93 | fputcsv($f, $fields); |
||
| 94 | } |
||
| 95 | fclose($f); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function putArrayInCSV(string $filename, array $array) |
||
| 99 | { |
||
| 100 | // create file if not exists |
||
| 101 | $fp = fopen($filename, 'a+'); |
||
| 102 | if ($fp === false) { |
||
| 103 | throw new Exception('can not open '.$filename); |
||
| 104 | } |
||
| 105 | if (is_array($array[0])) { |
||
| 106 | foreach ($array as $ar) { |
||
| 107 | fputcsv($fp, $ar); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | if (!is_array($array[0])) { |
||
| 111 | fputcsv($fp, $array); |
||
| 112 | } |
||
| 113 | |||
| 114 | fclose($fp); |
||
| 115 | } |
||
| 116 | } |
||
| 117 |