Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | <?php |
Adrià Vilanova Martínez | 5af8651 | 2023-12-02 20:44:16 +0100 | [diff] [blame] | 2 | /* |
| 3 | * hores |
| 4 | * Copyright (c) 2023 Adrià Vilanova Martínez |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as |
| 8 | * published by the Free Software Foundation, either version 3 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public |
| 17 | * License along with this program. |
| 18 | * If not, see http://www.gnu.org/licenses/. |
| 19 | */ |
| 20 | |
Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 21 | class csv { |
| 22 | public static $fields = ["dni", "name", "category", "email", "companies"]; |
| 23 | |
| 24 | public static function csv2array($filename) { |
| 25 | $file = fopen($filename, "r"); |
| 26 | |
| 27 | $return = []; |
| 28 | |
| 29 | $i = 0; |
| 30 | while (($line = fgetcsv($file, null, ";")) !== false) { |
| 31 | if ($i == 0) { |
| 32 | if (count($line) < count(self::$fields)) return false; |
| 33 | |
| 34 | for ($j = 0; $j < count(self::$fields); $j++) { |
| 35 | if ($line[$j] !== self::$fields[$j]) return false; |
| 36 | } |
| 37 | } else { |
| 38 | $return[$i] = []; |
| 39 | foreach (self::$fields as $j => $field) { |
| 40 | $return[$i][$field] = trim($line[$j]); |
| 41 | } |
| 42 | } |
| 43 | $i++; |
| 44 | } |
| 45 | |
| 46 | fclose($file); |
| 47 | |
| 48 | return $return; |
| 49 | } |
| 50 | } |