blob: ed01c2af1a6011652fddb5653ee30d78b1668a69 [file] [log] [blame]
<?php
/*
* hores
* Copyright (c) 2023 Adrià Vilanova Martínez
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program.
* If not, see http://www.gnu.org/licenses/.
*/
class csv {
public static $fields = ["dni", "name", "category", "email", "companies"];
public static function csv2array($filename) {
$file = fopen($filename, "r");
$return = [];
$i = 0;
while (($line = fgetcsv($file, null, ";")) !== false) {
if ($i == 0) {
if (count($line) < count(self::$fields)) return false;
for ($j = 0; $j < count(self::$fields); $j++) {
if ($line[$j] !== self::$fields[$j]) return false;
}
} else {
$return[$i] = [];
foreach (self::$fields as $j => $field) {
$return[$i][$field] = trim($line[$j]);
}
}
$i++;
}
fclose($file);
return $return;
}
}