Completed
Push — master ( 71a6f6...e9cd7a )
by Adrien
9s
created

Reader::getConcreteSheetIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
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
use Box\Spout\Common\Helper\EncodingHelper;
8
9
/**
10
 * Class Reader
11
 * This class provides support to read data from a CSV file.
12
 *
13
 * @package Box\Spout\Reader\CSV
14
 */
15
class Reader extends AbstractReader
16
{
17
    /** @var resource Pointer to the file to be written */
18
    protected $filePointer;
19
20
    /** @var SheetIterator To iterator over the CSV unique "sheet" */
21
    protected $sheetIterator;
22
23
    /** @var string Defines the character used to delimit fields (one character only) */
24
    protected $fieldDelimiter = ',';
25
26
    /** @var string Defines the character used to enclose fields (one character only) */
27
    protected $fieldEnclosure = '"';
28
29
    /** @var string Encoding of the CSV file to be read */
30
    protected $encoding = EncodingHelper::ENCODING_UTF8;
31
32
    /** @var string Defines the End of line */
33
    protected $endOfLineCharacter = "\n";
34
35
    /** @var string */
36
    protected $autoDetectLineEndings;
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 57
    public function setFieldDelimiter($fieldDelimiter)
46
    {
47 57
        $this->fieldDelimiter = $fieldDelimiter;
48 57
        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 57
    public function setFieldEnclosure($fieldEnclosure)
59
    {
60 57
        $this->fieldEnclosure = $fieldEnclosure;
61 57
        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 69
    public function setEncoding($encoding)
72
    {
73 69
        $this->encoding = $encoding;
74 69
        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 6
    public function setEndOfLineCharacter($endOfLineCharacter)
85
    {
86 6
        $this->endOfLineCharacter = $endOfLineCharacter;
87 6
        return $this;
88
    }
89
90
    /**
91
     * Returns whether stream wrappers are supported
92
     *
93
     * @return bool
94
     */
95 6
    protected function doesSupportStreamWrapper()
96
    {
97 6
        return true;
98
    }
99
100
    /**
101
     * Opens the file at the given path to make it ready to be read.
102
     * If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
103
     *
104
     * @param  string $filePath Path of the CSV file to be read
105
     * @return void
106
     * @throws \Box\Spout\Common\Exception\IOException
107
     */
108 84
    protected function openReader($filePath)
109
    {
110 84
        $this->autoDetectLineEndings = ini_get('auto_detect_line_endings');
111 84
        ini_set('auto_detect_line_endings', '1');
112
113 84
        $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
114 84
        if (!$this->filePointer) {
115 3
            throw new IOException("Could not open file $filePath for reading.");
116
        }
117
118 81
        $this->sheetIterator = new SheetIterator(
119 81
            $this->filePointer,
120 81
            $this->fieldDelimiter,
121 81
            $this->fieldEnclosure,
122 81
            $this->encoding,
123 81
            $this->endOfLineCharacter,
124 81
            $this->globalFunctionsHelper
125 81
        );
126 81
    }
127
128
    /**
129
     * Returns an iterator to iterate over sheets.
130
     *
131
     * @return SheetIterator To iterate over sheets
132
     */
133 81
    public function getConcreteSheetIterator()
134
    {
135 81
        return $this->sheetIterator;
136
    }
137
138
139
    /**
140
     * Closes the reader. To be used after reading the file.
141
     *
142
     * @return void
143
     */
144 81
    protected function closeReader()
145
    {
146 81
        if ($this->filePointer) {
147 81
            $this->globalFunctionsHelper->fclose($this->filePointer);
148 81
        }
149
150 81
        ini_set('auto_detect_line_endings', $this->autoDetectLineEndings);
151 81
    }
152
}
153