ldc-boutique-cnc/app/Jobs/CsvJobs.php
2022-08-25 12:26:43 +02:00

50 lines
1.2 KiB
PHP

<?php
namespace App\Jobs;
abstract class CsvJobs extends XArcadiaFlux
{
/**
* parse csv file
*/
protected function parseCsv($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
*
* @return string
*/
protected function array2csv($data, $delimiter = ',', $enclosure = '"', $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);
}
}