Spaces:
No application file
No application file
File size: 1,398 Bytes
d2897cd |
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 |
<?php
namespace Mautic\CoreBundle\Helper;
class CsvHelper
{
/**
* @param string $filename
* @param string $delimiter
*
* @return array
*/
public static function csv_to_array($filename = '', $delimiter = ',')
{
if (!file_exists($filename) || !is_readable($filename)) {
return false;
}
$header = null;
$data = [];
if (false !== ($handle = fopen($filename, 'r'))) {
while (false !== ($row = fgetcsv($handle, 1000, $delimiter))) {
if (!$header) {
$header = $row;
} else {
$data[] = array_combine($header, $row);
}
}
fclose($handle);
}
return $data;
}
public static function sanitizeHeaders(array $headers): array
{
return array_map('trim', $headers);
}
public static function convertHeadersIntoFields(array $headers): array
{
sort($headers);
$importedFields = [];
foreach ($headers as $header) {
$fieldName = strtolower(InputHelper::alphanum($header, false, '_'));
// Skip columns with empty names as they cannot be mapped.
if (!empty($fieldName)) {
$importedFields[$fieldName] = $header;
}
}
return $importedFields;
}
}
|