PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.26
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.26
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / vendor / openspout / openspout / src / Reader / CSV / Reader.php

Reader.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.26, at vendor/openspout/openspout/src/Reader/CSV/Reader.php

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