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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| <?php
namespace model;
class CSVParser
{
/**
* @var string|false CSV field separator
*/
private $separator = false;
/**
* @var string|false CSV Field enclosure
*/
private $enclosure = false;
/**
* @var resource CSV file handle
*/
private $handle;
/**
* @var int
*/
private $buffer = 4096;
/**
* @var array CSV columns header
*/
private $header = [];
/**
* @var array [[header => value]]
*/
private $data = [];
/**
* @var string|false
*/
private $csv_type = false;
/**
* @var array
*/
private $errors = [];
/**
* @return array [header]
*/
public function header(): array
{
return $this->header;
}
/**
* @return array [[header => value]]
*/
public function data(): array
{
return $this->data;
}
/**
* @return string|false
*/
public function CSVType()
{
return $this->csv_type;
}
/**
* @return bool
*/
public function isValid(): bool
{
return ($this->separator !== false) && ($this->enclosure !== false) && ($this->csv_type !== false);
}
/**
* @return array
*/
public function errors(): array
{
return $this->errors;
}
/**
* @param string $filepath
* @param int $buffer
* @return array
*/
public function parse(string $filepath, int $buffer = 4096)
{
$this->handle = $this->getFileHandle($filepath);
$this->buffer = $buffer;
if (empty($this->errors)) {
$this->detectSyntaxFromHeader();
$this->detectCSVType();
} else {
return;
}
if ($this->isValid()) {
// les en-têtes ont été passés par la fonction detectSyntaxFromHeader()
// on récupère toutes les données
while (($row = fgetcsv($this->handle, $this->buffer, $this->separator, $this->enclosure)) !== false) {
$this->data[] = array_combine($this->header, $row);
}
}
}
/**
* @param $filepath
* @return resource
*/
private function getFileHandle($filepath)
{
$handle = is_file($filepath) ? fopen($filepath, ('r')) : false;
if ($handle === false) {
$this->errors[] = "File {$filepath} doesn't exist or unable to open";
} else {
return $handle;
}
}
private function detectSyntaxFromHeader()
{
// on lit jusqu'à la fin de la première ligne
$row = fgets($this->handle); // doit contenir les en-têtes des colonnes
if (empty($row)) {
$this->errors[] = 'The file is empty';
}
// séparateur
if (mb_stripos($row, ',') !== false) {
$this->separator = ',';
} elseif (mb_stripos($row, ';') === false) {
$this->separator = ';';
} else {
$this->errors[] = 'Unable to detect the CSV field separator';
return;
}
// délimiteur de valeur (ce qu'il y a entre 2 noms de colonne) : "," ou ";" ou ',' ou ';' etc
$char = mb_substr($row, 0, 1); // premier caractère
if (($char === '"') && (mb_stripos($row, '"'.$this->separator.'"') !== false)) {
$this->enclosure = '"';
} elseif (($char === "'") && (mb_stripos($row, "'".$this->separator."'") !== false)) {
$this->enclosure = "'";
} elseif (ctype_alpha($char)) {
// pas de délimiteur de texte
// on vérifie que le premier séparateur trouvé est bien entouré de lettres ASCII
$i = mb_stripos($row, $this->separator);
if ($i) {
$fc = mb_substr($row, $i - 1, 1);
$lc = mb_substr($row, $i + 1, 1);
if (ctype_alpha($fc) && ctype_alpha($lc)) {
$this->enclosure = '';
}
}
}
if ($this->enclosure === false) {
$this->errors[] = 'Unable to detect the CSV text enclosure';
return;
}
$this->header = explode($this->separator, str_replace($this->enclosure, '', $row));
}
private function detectCSVType()
{
if ( ! empty($this->header)) {
$col = $this->header[0];
if (mb_stripos($col, 'SESA') !== false) {
$this->csv_type = 'licenses';
} elseif (mb_stripos($col, 'Ticket') !== false) {
$this->csv_type = 'tickets';
} else {
$this->errors[] = 'This file is neither a licence nor a ticket';
}
}
}
} |