Completed
Pull Request — master (#173)
by
unknown
14:05
created

Reader::closeReader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
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 45
     * @param string $fieldDelimiter Character that delimits fields
40
     * @return Reader
41 45
     */
42 45
    public function setFieldDelimiter($fieldDelimiter)
43
    {
44
        $this->fieldDelimiter = $fieldDelimiter;
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 45
     * @param string $fieldEnclosure Character that enclose fields
53
     * @return Reader
54 45
     */
55 45
    public function setFieldEnclosure($fieldEnclosure)
56
    {
57
        $this->fieldEnclosure = $fieldEnclosure;
58
        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 57
     * @param string $encoding Encoding of the CSV file to be read
66
     * @return Reader
67 57
     */
68 57
    public function setEncoding($encoding)
69
    {
70
        $this->encoding = $encoding;
71
        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 63
     * @return Reader
80
     */
81 63
    public function setEndOfLineCharacter($endOfLineCharacter)
82 63
    {
83 3
        $this->endOfLineCharacter = $endOfLineCharacter;
84
        return $this;
85
    }  
86 60
87 60
    /**
88 60
     * Opens the file at the given path to make it ready to be read.
89 60
     * If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
90 60
     *
91 60
     * @param  string $filePath Path of the CSV file to be read
92 60
     * @return void
93 60
     * @throws \Box\Spout\Common\Exception\IOException
94
     */
95
    protected function openReader($filePath)
96
    {
97
        $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
98
        if (!$this->filePointer) {
99
            throw new IOException("Could not open file $filePath for reading.");
100 60
        }
101
102 60
        $this->sheetIterator = new SheetIterator(
103
            $this->filePointer,
104
            $this->fieldDelimiter,
105
            $this->fieldEnclosure,
106
            $this->encoding,
107
            $this->endOfLineCharacter,
108
            $this->globalFunctionsHelper
109
        );
110
    }
111 60
112
    /**
113 60
     * Returns an iterator to iterate over sheets.
114 60
     *
115 60
     * @return SheetIterator To iterate over sheets
116 60
     */
117
    public function getConcreteSheetIterator()
118
    {
119
        return $this->sheetIterator;
120
    }
121
122
123
    /**
124
     * Closes the reader. To be used after reading the file.
125
     *
126
     * @return void
127
     */
128
    protected function closeReader()
129
    {
130
        if ($this->filePointer) {
131
            $this->globalFunctionsHelper->fclose($this->filePointer);
132
        }
133
    }
134
}
135