Completed
Pull Request — master (#366)
by Alexander
02:25
created

Reader::setMaxReadBytesPerLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Box\Spout\Reader\CSV;
4
5
use Box\Spout\Reader\AbstractReader;
6
use Box\Spout\Common\Exception\IOException;
7
8
/**
9
 * Class Reader
10
 * This class provides support to read data from a CSV file.
11
 *
12
 * @package Box\Spout\Reader\CSV
13
 */
14
class Reader extends AbstractReader
15
{
16
    /** @var resource Pointer to the file to be written */
17
    protected $filePointer;
18
19
    /** @var SheetIterator To iterator over the CSV unique "sheet" */
20
    protected $sheetIterator;
21
22
    /** @var string Original value for the "auto_detect_line_endings" INI value */
23
    protected $originalAutoDetectLineEndings;
24
25
    /**
26
     * Returns the reader's current options
27
     *
28
     * @return ReaderOptions
29
     */
30 81
    protected function getOptions()
31
    {
32 81
        if (!isset($this->options)) {
33 81
            $this->options = new ReaderOptions();
34 81
        }
35 81
        return $this->options;
36
    }
37
38
    /**
39
     * Sets the field delimiter for the CSV.
40
     * Needs to be called before opening the reader.
41
     *
42
     * @param string $fieldDelimiter Character that delimits fields
43
     * @return Reader
44
     */
45 63
    public function setFieldDelimiter($fieldDelimiter)
46
    {
47 63
        $this->getOptions()->setFieldDelimiter($fieldDelimiter);
48 63
        return $this;
49
    }
50
51
    /**
52
     * Sets the field enclosure for the CSV.
53
     * Needs to be called before opening the reader.
54
     *
55
     * @param string $fieldEnclosure Character that enclose fields
56
     * @return Reader
57
     */
58 63
    public function setFieldEnclosure($fieldEnclosure)
59
    {
60 63
        $this->getOptions()->setFieldEnclosure($fieldEnclosure);
61 63
        return $this;
62
    }
63
64
    /**
65
     * Sets the encoding of the CSV file to be read.
66
     * Needs to be called before opening the reader.
67
     *
68
     * @param string $encoding Encoding of the CSV file to be read
69
     * @return Reader
70
     */
71 75
    public function setEncoding($encoding)
72
    {
73 75
        $this->getOptions()->setEncoding($encoding);
74 75
        return $this;
75
    }
76
77
    /**
78
     * Sets the EOL for the CSV.
79
     * Needs to be called before opening the reader.
80
     *
81
     * @param string $endOfLineCharacter used to properly get lines from the CSV file.
82
     * @return Reader
83
     */
84 63
    public function setEndOfLineCharacter($endOfLineCharacter)
85
    {
86 63
        $this->getOptions()->setEndOfLineCharacter($endOfLineCharacter);
87 63
        return $this;
88
    }
89
90
    /**
91
     * Set maximum bytes to read per line
92
     * @param $maxReadBytesPerLine
93
     * @return Reader
94
     */
95 63
    public function setMaxReadBytesPerLine($maxReadBytesPerLine)
96
    {
97 63
        $this->getOptions()->setMaxReadBytesPerLine($maxReadBytesPerLine);
98 63
        return $this;
99
    }
100
101
    /**
102
     * Returns whether stream wrappers are supported
103
     *
104
     * @return bool
105
     */
106 6
    protected function doesSupportStreamWrapper()
107
    {
108 6
        return true;
109
    }
110
111
    /**
112
     * Opens the file at the given path to make it ready to be read.
113
     * If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
114
     *
115
     * @param  string $filePath Path of the CSV file to be read
116
     * @return void
117
     * @throws \Box\Spout\Common\Exception\IOException
118
     */
119 84
    protected function openReader($filePath)
120
    {
121 84
        $this->originalAutoDetectLineEndings = ini_get('auto_detect_line_endings');
122 84
        ini_set('auto_detect_line_endings', '1');
123
124 84
        $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
125 84
        if (!$this->filePointer) {
126 3
            throw new IOException("Could not open file $filePath for reading.");
127
        }
128
129 81
        $this->sheetIterator = new SheetIterator(
130 81
            $this->filePointer,
131 81
            $this->getOptions(),
132 81
            $this->globalFunctionsHelper
133 81
        );
134 81
    }
135
136
    /**
137
     * Returns an iterator to iterate over sheets.
138
     *
139
     * @return SheetIterator To iterate over sheets
140
     */
141 81
    protected function getConcreteSheetIterator()
142
    {
143 81
        return $this->sheetIterator;
144
    }
145
146
147
    /**
148
     * Closes the reader. To be used after reading the file.
149
     *
150
     * @return void
151
     */
152 81
    protected function closeReader()
153
    {
154 81
        if ($this->filePointer) {
155 81
            $this->globalFunctionsHelper->fclose($this->filePointer);
156 81
        }
157
158 81
        ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings);
159 81
    }
160
}
161