1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Reader\CSV; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Reader\Common\Entity\Options; |
6
|
|
|
use Box\Spout\Reader\CSV\Creator\EntityFactory; |
7
|
|
|
use Box\Spout\Reader\ReaderAbstract; |
8
|
|
|
use Box\Spout\Common\Exception\IOException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Reader |
12
|
|
|
* This class provides support to read data from a CSV file. |
13
|
|
|
* |
14
|
|
|
* @package Box\Spout\Reader\CSV |
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
|
|
|
/** |
28
|
|
|
* Sets the field delimiter for the CSV. |
29
|
|
|
* Needs to be called before opening the reader. |
30
|
|
|
* |
31
|
|
|
* @param string $fieldDelimiter Character that delimits fields |
32
|
|
|
* @return Reader |
33
|
|
|
*/ |
34
|
19 |
|
public function setFieldDelimiter($fieldDelimiter) |
35
|
|
|
{ |
36
|
19 |
|
$this->optionsManager->setOption(Options::FIELD_DELIMITER, $fieldDelimiter); |
37
|
19 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sets the field enclosure for the CSV. |
42
|
|
|
* Needs to be called before opening the reader. |
43
|
|
|
* |
44
|
|
|
* @param string $fieldEnclosure Character that enclose fields |
45
|
|
|
* @return Reader |
46
|
|
|
*/ |
47
|
19 |
|
public function setFieldEnclosure($fieldEnclosure) |
48
|
|
|
{ |
49
|
19 |
|
$this->optionsManager->setOption(Options::FIELD_ENCLOSURE, $fieldEnclosure); |
50
|
19 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets the encoding of the CSV file to be read. |
55
|
|
|
* Needs to be called before opening the reader. |
56
|
|
|
* |
57
|
|
|
* @param string $encoding Encoding of the CSV file to be read |
58
|
|
|
* @return Reader |
59
|
|
|
*/ |
60
|
23 |
|
public function setEncoding($encoding) |
61
|
|
|
{ |
62
|
23 |
|
$this->optionsManager->setOption(Options::ENCODING, $encoding); |
63
|
23 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns whether stream wrappers are supported |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
2 |
|
protected function doesSupportStreamWrapper() |
72
|
|
|
{ |
73
|
2 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Opens the file at the given path to make it ready to be read. |
78
|
|
|
* If setEncoding() was not called, it assumes that the file is encoded in UTF-8. |
79
|
|
|
* |
80
|
|
|
* @param string $filePath Path of the CSV file to be read |
81
|
|
|
* @return void |
82
|
|
|
* @throws \Box\Spout\Common\Exception\IOException |
83
|
|
|
*/ |
84
|
27 |
|
protected function openReader($filePath) |
85
|
|
|
{ |
86
|
27 |
|
$this->originalAutoDetectLineEndings = ini_get('auto_detect_line_endings'); |
87
|
27 |
|
ini_set('auto_detect_line_endings', '1'); |
88
|
|
|
|
89
|
27 |
|
$this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r'); |
90
|
27 |
|
if (!$this->filePointer) { |
91
|
1 |
|
throw new IOException("Could not open file $filePath for reading."); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @var EntityFactory $entityFactory */ |
95
|
26 |
|
$entityFactory = $this->entityFactory; |
96
|
|
|
|
97
|
26 |
|
$this->sheetIterator = $entityFactory->createSheetIterator( |
98
|
26 |
|
$this->filePointer, |
|
|
|
|
99
|
26 |
|
$this->optionsManager, |
100
|
26 |
|
$this->globalFunctionsHelper |
101
|
|
|
); |
102
|
26 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns an iterator to iterate over sheets. |
106
|
|
|
* |
107
|
|
|
* @return SheetIterator To iterate over sheets |
108
|
|
|
*/ |
109
|
26 |
|
protected function getConcreteSheetIterator() |
110
|
|
|
{ |
111
|
26 |
|
return $this->sheetIterator; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Closes the reader. To be used after reading the file. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
26 |
|
protected function closeReader() |
121
|
|
|
{ |
122
|
26 |
|
if ($this->filePointer) { |
123
|
26 |
|
$this->globalFunctionsHelper->fclose($this->filePointer); |
124
|
|
|
} |
125
|
|
|
|
126
|
26 |
|
ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings); |
127
|
26 |
|
} |
128
|
|
|
} |
129
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: