| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\File; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\FileInterface; |
| 6 |
|
| 7 |
class CSVFile extends AbstractIndexedFile implements FileInterface |
| 8 |
{ |
| 9 |
|
| 10 |
protected $enclosure = '"'; |
| 11 |
protected $delimiter = ','; |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* Set Enclosure |
| 16 |
* |
| 17 |
* @param string $enclosure |
| 18 |
*/ |
| 19 |
public function setEnclosure($enclosure) |
| 20 |
{ |
| 21 |
$this->enclosure = $enclosure; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Set Delimiter |
| 26 |
* |
| 27 |
* @param string $delimiter |
| 28 |
*/ |
| 29 |
public function setDelimiter($delimiter) |
| 30 |
{ |
| 31 |
$this->delimiter = $delimiter; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get Delimiter |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function getDelimiter() |
| 40 |
{ |
| 41 |
return $this->delimiter; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get enclosure |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function getEnclosure() |
| 50 |
{ |
| 51 |
return $this->enclosure; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Generate record file positions |
| 56 |
* |
| 57 |
* Loop through each record and save each position |
| 58 |
*/ |
| 59 |
protected function generateIndex() |
| 60 |
{ |
| 61 |
$record = 0; |
| 62 |
rewind($this->getFileHandle()); |
| 63 |
while (!feof($this->getFileHandle())) { |
| 64 |
|
| 65 |
$startIndex = ftell($this->getFileHandle()); |
| 66 |
$row = fgetcsv($this->getFileHandle(), 0, $this->getDelimiter(), $this->getEnclosure()); |
| 67 |
|
| 68 |
if (!empty($row)) { |
| 69 |
$this->setIndex($record, $startIndex, ftell($this->getFileHandle())); |
| 70 |
$record++; |
| 71 |
} |
| 72 |
|
| 73 |
if ($this->is_processing && ($record >= 2 || $startIndex > $this->process_max_size)) { |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|