Completed
Push — master ( afa401...19e2a9 )
by Andy
02:13
created

AbstractCsv::getEnclosure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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