Completed
Push — master ( 31551e...68bd4b )
by Andy
02:07
created

AbstractCsv::setFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
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 Palmtree\Csv;
4
5
abstract class AbstractCsv
6
{
7
    /** @var string */
8
    protected $openMode;
9
    /** @var string */
10
    protected $file;
11
    /** @var bool */
12
    protected $hasHeaders;
13
    /** @var string */
14
    protected $delimiter;
15
    /** @var string */
16
    protected $enclosure;
17
    /** @var string */
18
    protected $escapeCharacter;
19
    /** @var CsvFileObject */
20
    protected $document;
21
22
    /**
23
     * AbstractCsv constructor.
24
     *
25
     * @param string $file       Path to CSV file.
26
     * @param bool   $hasHeaders Whether the CSV file contains headers.
27
     * @param string $delimiter  Cell delimiter. Default ',' (comma).
28
     * @param string $enclosure  Cell enclosure. Default '"' (double quote)
29
     * @param string $escape     Cell escape character. Default null byte.
30
     */
31 5
    public function __construct($file, $hasHeaders = true, $delimiter = ',', $enclosure = '"', $escape = "\0")
32
    {
33 5
        $this->setFile($file)
34 5
             ->setHasHeaders($hasHeaders)
35 5
             ->setDelimiter($delimiter)
36 5
             ->setEnclosure($enclosure)
37 5
             ->setEscapeCharacter($escape);
38 5
    }
39
40
    /**
41
     * AbstractCsv destructor.
42
     */
43 5
    public function __destruct()
44
    {
45 5
        $this->closeDocument();
46 5
    }
47
48
    /**
49
     * Creates a new SplFileObject instance.
50
     */
51 2
    public function createDocument()
52
    {
53 2
        $this->closeDocument();
54
55 2
        $document = new CsvFileObject($this->getFile(), $this->getOpenMode());
56
57
        $document->setFlags(
58
            CsvFileObject::READ_CSV |
59
            CsvFileObject::READ_AHEAD |
60
            CsvFileObject::SKIP_EMPTY |
61
            CsvFileObject::DROP_NEW_LINE
62
        );
63
64
        $document->setCsvControl($this->getDelimiter(), $this->getEnclosure(), $this->getEscapeCharacter());
65
66
        $this->setDocument($document);
67
    }
68
69
    /**
70
     * Closes the document by setting our reference to null
71
     * to ensure its destructor is called.
72
     */
73 5
    public function closeDocument()
74
    {
75 5
        $this->document = null;
76 5
    }
77
78
    /**
79
     * @param CsvFileObject|null $document
80
     *
81
     * @return AbstractCsv
82
     */
83
    public function setDocument($document)
84
    {
85
        $this->closeDocument();
86
87
        $this->document = $document;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return CsvFileObject
94
     */
95
    public function getDocument()
96
    {
97
        if (!$this->document) {
98
            $this->createDocument();
99
        }
100
101
        return $this->document;
102
    }
103
104
    /**
105
     * @return string
106
     */
107 2
    public function getFile()
108
    {
109 2
        return $this->file;
110
    }
111
112
    /**
113
     * @param string $file
114
     *
115
     * @return $this
116
     */
117 5
    public function setFile($file)
118
    {
119 5
        $this->file = $file;
120
121 5
        return $this;
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function hasHeaders()
128
    {
129
        return $this->hasHeaders;
130
    }
131
132
    /**
133
     * @param bool $hasHeaders
134
     *
135
     * @return AbstractCsv
136
     */
137 5
    public function setHasHeaders($hasHeaders)
138
    {
139 5
        $this->hasHeaders = (bool)$hasHeaders;
140
141 5
        return $this;
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getDelimiter()
148
    {
149
        return $this->delimiter;
150
    }
151
152
    /**
153
     * @param string $delimiter
154
     *
155
     * @return AbstractCsv
156
     */
157 5
    public function setDelimiter($delimiter)
158
    {
159 5
        $this->delimiter = $delimiter;
160
161 5
        return $this;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getEnclosure()
168
    {
169
        return $this->enclosure;
170
    }
171
172
    /**
173
     * @param string $enclosure
174
     *
175
     * @return AbstractCsv
176
     */
177 5
    public function setEnclosure($enclosure)
178
    {
179 5
        $this->enclosure = $enclosure;
180
181 5
        return $this;
182
    }
183
184
    /**
185
     * @return string
186
     */
187 2
    public function getOpenMode()
188
    {
189 2
        return $this->openMode;
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function getEscapeCharacter()
196
    {
197
        return $this->escapeCharacter;
198
    }
199
200
    /**
201
     * @param string $escapeCharacter
202
     *
203
     * @return AbstractCsv
204
     */
205 5
    public function setEscapeCharacter($escapeCharacter)
206
    {
207 5
        $this->escapeCharacter = $escapeCharacter;
208
209 5
        return $this;
210
    }
211
}
212