| 1 |
<?php |
| 2 |
|
| 3 |
namespace OpenSpout\Reader\CSV; |
| 4 |
|
| 5 |
use OpenSpout\Common\Exception\IOException; |
| 6 |
use OpenSpout\Common\Helper\GlobalFunctionsHelper; |
| 7 |
use OpenSpout\Common\Manager\OptionsManagerInterface; |
| 8 |
use OpenSpout\Reader\Common\Creator\InternalEntityFactoryInterface; |
| 9 |
use OpenSpout\Reader\Common\Entity\Options; |
| 10 |
use OpenSpout\Reader\CSV\Creator\InternalEntityFactory; |
| 11 |
use OpenSpout\Reader\ReaderAbstract; |
| 12 |
|
| 13 |
/** |
| 14 |
* This class provides support to read data from a CSV file. |
| 15 |
*/ |
| 16 |
class Reader extends ReaderAbstract |
| 17 |
{ |
| 18 |
/** @var resource Pointer to the file to be written */ |
| 19 |
protected $filePointer; |
| 20 |
|
| 21 |
/** @var SheetIterator To iterator over the CSV unique "sheet" */ |
| 22 |
protected $sheetIterator; |
| 23 |
|
| 24 |
/** @var string Original value for the "auto_detect_line_endings" INI value */ |
| 25 |
protected $originalAutoDetectLineEndings; |
| 26 |
|
| 27 |
/** @var bool Whether the code is running with PHP >= 8.1 */ |
| 28 |
private $isRunningAtLeastPhp81; |
| 29 |
|
| 30 |
public function __construct( |
| 31 |
OptionsManagerInterface $optionsManager, |
| 32 |
GlobalFunctionsHelper $globalFunctionsHelper, |
| 33 |
InternalEntityFactoryInterface $entityFactory |
| 34 |
) { |
| 35 |
parent::__construct($optionsManager, $globalFunctionsHelper, $entityFactory); |
| 36 |
$this->isRunningAtLeastPhp81 = version_compare(PHP_VERSION, '8.1.0') >= 0; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Sets the field delimiter for the CSV. |
| 41 |
* Needs to be called before opening the reader. |
| 42 |
* |
| 43 |
* @param string $fieldDelimiter Character that delimits fields |
| 44 |
* |
| 45 |
* @return Reader |
| 46 |
*/ |
| 47 |
public function setFieldDelimiter($fieldDelimiter) |
| 48 |
{ |
| 49 |
$this->optionsManager->setOption(Options::FIELD_DELIMITER, $fieldDelimiter); |
| 50 |
|
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Sets the field enclosure for the CSV. |
| 56 |
* Needs to be called before opening the reader. |
| 57 |
* |
| 58 |
* @param string $fieldEnclosure Character that enclose fields |
| 59 |
* |
| 60 |
* @return Reader |
| 61 |
*/ |
| 62 |
public function setFieldEnclosure($fieldEnclosure) |
| 63 |
{ |
| 64 |
$this->optionsManager->setOption(Options::FIELD_ENCLOSURE, $fieldEnclosure); |
| 65 |
|
| 66 |
return $this; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Sets the encoding of the CSV file to be read. |
| 71 |
* Needs to be called before opening the reader. |
| 72 |
* |
| 73 |
* @param string $encoding Encoding of the CSV file to be read |
| 74 |
* |
| 75 |
* @return Reader |
| 76 |
*/ |
| 77 |
public function setEncoding($encoding) |
| 78 |
{ |
| 79 |
$this->optionsManager->setOption(Options::ENCODING, $encoding); |
| 80 |
|
| 81 |
return $this; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns whether stream wrappers are supported. |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
protected function doesSupportStreamWrapper() |
| 90 |
{ |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Opens the file at the given path to make it ready to be read. |
| 96 |
* If setEncoding() was not called, it assumes that the file is encoded in UTF-8. |
| 97 |
* |
| 98 |
* @param string $filePath Path of the CSV file to be read |
| 99 |
* |
| 100 |
* @throws \OpenSpout\Common\Exception\IOException |
| 101 |
*/ |
| 102 |
protected function openReader($filePath) |
| 103 |
{ |
| 104 |
// "auto_detect_line_endings" is deprecated in PHP 8.1 |
| 105 |
if (!$this->isRunningAtLeastPhp81) { |
| 106 |
$this->originalAutoDetectLineEndings = ini_get('auto_detect_line_endings'); |
| 107 |
ini_set('auto_detect_line_endings', '1'); |
| 108 |
} |
| 109 |
|
| 110 |
$this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r'); |
| 111 |
if (!$this->filePointer) { |
| 112 |
throw new IOException("Could not open file {$filePath} for reading."); |
| 113 |
} |
| 114 |
|
| 115 |
/** @var InternalEntityFactory $entityFactory */ |
| 116 |
$entityFactory = $this->entityFactory; |
| 117 |
|
| 118 |
$this->sheetIterator = $entityFactory->createSheetIterator( |
| 119 |
$this->filePointer, |
| 120 |
$this->optionsManager, |
| 121 |
$this->globalFunctionsHelper |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns an iterator to iterate over sheets. |
| 127 |
* |
| 128 |
* @return SheetIterator To iterate over sheets |
| 129 |
*/ |
| 130 |
protected function getConcreteSheetIterator() |
| 131 |
{ |
| 132 |
return $this->sheetIterator; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Closes the reader. To be used after reading the file. |
| 137 |
*/ |
| 138 |
protected function closeReader() |
| 139 |
{ |
| 140 |
if (\is_resource($this->filePointer)) { |
| 141 |
$this->globalFunctionsHelper->fclose($this->filePointer); |
| 142 |
} |
| 143 |
|
| 144 |
// "auto_detect_line_endings" is deprecated in PHP 8.1 |
| 145 |
if (!$this->isRunningAtLeastPhp81) { |
| 146 |
ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|