Completed
Push — master ( bb5748...f73a2b )
by Andy
03:16
created

AbstractCsv::setDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Palmtree\Csv;
4
5
abstract class AbstractCsv
6
{
7
    /** @var string */
8
    protected $fopenMode;
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 \SplFileObject */
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
     */
28 5
    public function __construct($file, $hasHeaders = true, $delimiter = ',', $enclosure = '"')
29
    {
30 5
        $this->setFile($file)
31 5
             ->setHasHeaders($hasHeaders)
32 5
             ->setDelimiter($delimiter)
33 5
             ->setEnclosure($enclosure);
34 5
    }
35
36
    /**
37
     * Reader destructor.
38
     */
39 5
    public function __destruct()
40
    {
41 5
        $this->closeDocument();
42 5
    }
43
44
    /**
45
     * @return string
46
     */
47 2
    public function getFile()
48
    {
49 2
        return $this->file;
50
    }
51
52
    /**
53
     * @param string $file
54
     *
55
     * @return $this
56
     */
57 5
    public function setFile($file)
58
    {
59 5
        $this->file = $file;
60
61 5
        return $this;
62
    }
63
64
    /**
65
     * @param string $openMode
66
     */
67 2
    public function createDocument($openMode = '')
68
    {
69 2
        if (!$openMode) {
70 2
            $openMode = $this->fopenMode;
71
        }
72
73 2
        $document = new \SplFileObject($this->getFile(), $openMode);
74
75
        $document->setFlags(
76
            \SplFileObject::READ_CSV |
77
            \SplFileObject::READ_AHEAD |
78
            \SplFileObject::SKIP_EMPTY |
79
            \SplFileObject::DROP_NEW_LINE
80
        );
81
82
        $document->setCsvControl($this->getDelimiter(), $this->getEnclosure());
83
84
        $this->setDocument($document);
85
    }
86
87
    /**
88
     *
89
     */
90 5
    public function closeDocument()
91
    {
92 5
        $this->setDocument(null);
93 5
    }
94
95
    /**
96
     * @return bool
97
     */
98
    public function hasHeaders()
99
    {
100
        return $this->hasHeaders;
101
    }
102
103
    /**
104
     * @param bool $hasHeaders
105
     *
106
     * @return AbstractCsv
107
     */
108 5
    public function setHasHeaders($hasHeaders)
109
    {
110 5
        $this->hasHeaders = (bool)$hasHeaders;
111
112 5
        return $this;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getDelimiter()
119
    {
120
        return $this->delimiter;
121
    }
122
123
    /**
124
     * @param string $delimiter
125
     *
126
     * @return AbstractCsv
127
     */
128 5
    public function setDelimiter($delimiter)
129
    {
130 5
        $this->delimiter = $delimiter;
131
132 5
        return $this;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getEnclosure()
139
    {
140
        return $this->enclosure;
141
    }
142
143
    /**
144
     * @param string $enclosure
145
     *
146
     * @return AbstractCsv
147
     */
148 5
    public function setEnclosure($enclosure)
149
    {
150 5
        $this->enclosure = $enclosure;
151
152 5
        return $this;
153
    }
154
155
    /**
156
     * @param \SplFileObject|null $document
157
     *
158
     * @return AbstractCsv
159
     */
160 5
    public function setDocument($document)
161
    {
162 5
        $this->document = null;
163
164 5
        $this->document = $document;
165
166 5
        return $this;
167
    }
168
169
    /**
170
     * @return \SplFileObject
171
     */
172
    public function getDocument()
173
    {
174
        return $this->document;
175
    }
176
}
177