Completed
Push — develop_3.0 ( 4d6437...b7e467 )
by Adrien
02:48
created

Reader::setEndOfLineCharacter()   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\Common\Entity\Options;
6
use Box\Spout\Reader\CSV\Creator\EntityFactory;
7
use Box\Spout\Reader\ReaderAbstract;
8
use Box\Spout\Common\Exception\IOException;
9
10
/**
11
 * Class Reader
12
 * This class provides support to read data from a CSV file.
13
 *
14
 * @package Box\Spout\Reader\CSV
15
 */
16
class Reader extends ReaderAbstract
17
{
18
    /** @var resource Pointer to the file to be written */
19
    protected $filePointer;
20
21
    /** @var SheetIterator To iterator over the CSV unique "sheet" */
22
    protected $sheetIterator;
23
24
    /** @var string Original value for the "auto_detect_line_endings" INI value */
25
    protected $originalAutoDetectLineEndings;
26
27
    /**
28
     * Sets the field delimiter for the CSV.
29
     * Needs to be called before opening the reader.
30
     *
31
     * @param string $fieldDelimiter Character that delimits fields
32
     * @return Reader
33
     */
34 19
    public function setFieldDelimiter($fieldDelimiter)
35
    {
36 19
        $this->optionsManager->setOption(Options::FIELD_DELIMITER, $fieldDelimiter);
37 19
        return $this;
38
    }
39
40
    /**
41
     * Sets the field enclosure for the CSV.
42
     * Needs to be called before opening the reader.
43
     *
44
     * @param string $fieldEnclosure Character that enclose fields
45
     * @return Reader
46
     */
47 19
    public function setFieldEnclosure($fieldEnclosure)
48
    {
49 19
        $this->optionsManager->setOption(Options::FIELD_ENCLOSURE, $fieldEnclosure);
50 19
        return $this;
51
    }
52
53
    /**
54
     * Sets the encoding of the CSV file to be read.
55
     * Needs to be called before opening the reader.
56
     *
57
     * @param string $encoding Encoding of the CSV file to be read
58
     * @return Reader
59
     */
60 23
    public function setEncoding($encoding)
61
    {
62 23
        $this->optionsManager->setOption(Options::ENCODING, $encoding);
63 23
        return $this;
64
    }
65
66
    /**
67
     * Returns whether stream wrappers are supported
68
     *
69
     * @return bool
70
     */
71 2
    protected function doesSupportStreamWrapper()
72
    {
73 2
        return true;
74
    }
75
76
    /**
77
     * Opens the file at the given path to make it ready to be read.
78
     * If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
79
     *
80
     * @param  string $filePath Path of the CSV file to be read
81
     * @return void
82
     * @throws \Box\Spout\Common\Exception\IOException
83
     */
84 27
    protected function openReader($filePath)
85
    {
86 27
        $this->originalAutoDetectLineEndings = ini_get('auto_detect_line_endings');
87 27
        ini_set('auto_detect_line_endings', '1');
88
89 27
        $this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
90 27
        if (!$this->filePointer) {
91 1
            throw new IOException("Could not open file $filePath for reading.");
92
        }
93
94
        /** @var EntityFactory $entityFactory */
95 26
        $entityFactory = $this->entityFactory;
96
97 26
        $this->sheetIterator = $entityFactory->createSheetIterator(
98 26
            $this->filePointer,
99 26
            $this->optionsManager,
100 26
            $this->globalFunctionsHelper
101
        );
102 26
    }
103
104
    /**
105
     * Returns an iterator to iterate over sheets.
106
     *
107
     * @return SheetIterator To iterate over sheets
108
     */
109 26
    protected function getConcreteSheetIterator()
110
    {
111 26
        return $this->sheetIterator;
112
    }
113
114
115
    /**
116
     * Closes the reader. To be used after reading the file.
117
     *
118
     * @return void
119
     */
120 26
    protected function closeReader()
121
    {
122 26
        if ($this->filePointer) {
123 26
            $this->globalFunctionsHelper->fclose($this->filePointer);
124
        }
125
126 26
        ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings);
127 26
    }
128
}
129