Completed
Push — master ( 0c90d1...049fd9 )
by Adrien
9s
created

Reader::openReader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
crap 2
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
    /**
36
     * Sets the field delimiter for the CSV.
37
     * Needs to be called before opening the reader.
38
     *
39
     * @param string $fieldDelimiter Character that delimits fields
40
     * @return Reader
41
     */
42 45
    public function setFieldDelimiter($fieldDelimiter)
43
    {
44 45
        $this->fieldDelimiter = $fieldDelimiter;
45 45
        return $this;
46
    }
47
48
    /**
49
     * Sets the field enclosure for the CSV.
50
     * Needs to be called before opening the reader.
51
     *
52
     * @param string $fieldEnclosure Character that enclose fields
53
     * @return Reader
54
     */
55 45
    public function setFieldEnclosure($fieldEnclosure)
56
    {
57 45
        $this->fieldEnclosure = $fieldEnclosure;
58 45
        return $this;
59
    }
60
61
    /**
62
     * Sets the encoding of the CSV file to be read.
63
     * Needs to be called before opening the reader.
64
     *
65
     * @param string $encoding Encoding of the CSV file to be read
66
     * @return Reader
67
     */
68 57
    public function setEncoding($encoding)
69
    {
70 57
        $this->encoding = $encoding;
71 57
        return $this;
72
    }
73
74
    /**
75
     * Sets the EOL for the CSV.
76
     * Needs to be called before opening the reader.
77
     *
78
     * @param string $endOfLineCharacter used to properly get lines from the CSV file.
79
     * @return Reader
80
     */
81 6
    public function setEndOfLineCharacter($endOfLineCharacter)
82
    {
83 6
        $this->endOfLineCharacter = $endOfLineCharacter;
84 6
        return $this;
85
    }
86
87
    /**
88
     * Returns whether stream wrappers are supported
89
     *
90
     * @return bool
91
     */
92 6
    protected function doesSupportStreamWrapper()
93
    {
94 6
        return true;
95
    }
96
97
    /**
98
     * Opens the file at the given path to make it ready to be read.
99
     * If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
100
     *
101
     * @param  string $filePath Path of the CSV file to be read
102
     * @return void
103
     * @throws \Box\Spout\Common\Exception\IOException
104
     */
105 72
    protected function openReader($filePath)
106
    {
107 72
        $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
108 72
        if (!$this->filePointer) {
109 3
            throw new IOException("Could not open file $filePath for reading.");
110
        }
111
112 69
        $this->sheetIterator = new SheetIterator(
113 69
            $this->filePointer,
114 69
            $this->fieldDelimiter,
115 69
            $this->fieldEnclosure,
116 69
            $this->encoding,
117 69
            $this->endOfLineCharacter,
118 69
            $this->globalFunctionsHelper
119 69
        );
120 69
    }
121
122
    /**
123
     * Returns an iterator to iterate over sheets.
124
     *
125
     * @return SheetIterator To iterate over sheets
126
     */
127 69
    public function getConcreteSheetIterator()
128
    {
129 69
        return $this->sheetIterator;
130
    }
131
132
133
    /**
134
     * Closes the reader. To be used after reading the file.
135
     *
136
     * @return void
137
     */
138 69
    protected function closeReader()
139
    {
140 69
        if ($this->filePointer) {
141 69
            $this->globalFunctionsHelper->fclose($this->filePointer);
142 69
        }
143 69
    }
144
}
145