ldc-boutique-cnc/app/Jobs/CsvJobs.php
2022-09-03 16:56:16 +02:00

63 lines
1.5 KiB
PHP

<?php
namespace App\Jobs;
abstract class CsvJobs extends XArcadiaFlux
{
/**
* parse csv file
*
* @param string $csvFile
*
* @return array
*/
protected function parseCsv(string $csvFile)
{
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle)) {
$line_of_text[] = fgetcsv($file_handle, 0, ";");
if (!end($line_of_text)) {
// Supprime les lignes vides du csv.
array_pop($line_of_text);
}
}
fclose($file_handle);
return $line_of_text;
}
/**
* Is the file to be ignored
*
* @param string $filename
* @return boolean
*/
protected function fileHasToBeIgnored(string $filename): bool
{
return in_array($filename, $this->ignoreFiles) or !$this->checkExtension($filename, 'csv');
}
/**
* converts an array to a csv stream
*
* @param array $data
* @param string $delimiter
* @param string $enclosure
* @param string $escape_char
*
* @return string
*/
protected function array2csv(
array $data,
string $delimiter = ',',
string $enclosure = '"',
string $escape_char = "\\"
) {
$f = fopen('php://memory', 'r+');
foreach ($data as $item) {
fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
}
rewind($f);
return stream_get_contents($f);
}
}