Failed Conditions
Push — perf-tests ( 50942d...2fc93e )
by Adrien
14:53
created

Reader::doesSupportStreamWrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    public function setFieldDelimiter($fieldDelimiter)
46
    {
47
        $this->fieldDelimiter = $fieldDelimiter;
48
        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
    public function setFieldEnclosure($fieldEnclosure)
59
    {
60
        $this->fieldEnclosure = $fieldEnclosure;
61
        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
    public function setEncoding($encoding)
72
    {
73
        $this->encoding = $encoding;
74
        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
    public function setEndOfLineCharacter($endOfLineCharacter)
85
    {
86
        $this->endOfLineCharacter = $endOfLineCharacter;
87
        return $this;
88
    }
89
90
    /**
91
     * Returns whether stream wrappers are supported
92
     *
93
     * @return bool
94
     */
95
    protected function doesSupportStreamWrapper()
96
    {
97
        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
    protected function openReader($filePath)
109
    {
110
        $this->autoDetectLineEndings = ini_get('auto_detect_line_endings');
111
        ini_set('auto_detect_line_endings', '1');
112
113
        $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
114
        if (!$this->filePointer) {
115
            throw new IOException("Could not open file $filePath for reading.");
116
        }
117
118
        $this->sheetIterator = new SheetIterator(
119
            $this->filePointer,
120
            $this->fieldDelimiter,
121
            $this->fieldEnclosure,
122
            $this->encoding,
123
            $this->endOfLineCharacter,
124
            $this->globalFunctionsHelper
125
        );
126
    }
127
128
    /**
129
     * Returns an iterator to iterate over sheets.
130
     *
131
     * @return SheetIterator To iterate over sheets
132
     */
133
    public function getConcreteSheetIterator()
134
    {
135
        return $this->sheetIterator;
136
    }
137
138
139
    /**
140
     * Closes the reader. To be used after reading the file.
141
     *
142
     * @return void
143
     */
144
    protected function closeReader()
145
    {
146
        if ($this->filePointer) {
147
            $this->globalFunctionsHelper->fclose($this->filePointer);
148
        }
149
150
        ini_set('auto_detect_line_endings', $this->autoDetectLineEndings);
151
    }
152
}
153