AbstractBase::open()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-05-06 
5
 */
6
7
namespace Net\Bazzline\Component\Csv;
8
9
use SplFileObject;
10
11
abstract class AbstractBase implements BaseInterface
12
{
13
    /** @var string */
14
    private $delimiter = ',';
15
16
    /** @var string */
17
    private $enclosure = '"';
18
19
    /** @var string */
20
    private $escapeCharacter = '\\';
21
22
    /** @var false|array */
23
    private $headline = false;
24
25
    /** @var SplFileObject */
26
    private $handler;
27
28
    /** @var string */
29
    private $path;
30
31
    /**
32
     * @return bool
33
     */
34
    public function hasHeadline()
35
    {
36
        return ($this->headline !== false);
37
    }
38
39
    /**
40
     * @param string $delimiter
41
     * @throws InvalidArgumentException
42
     */
43
    public function setDelimiter($delimiter)
44
    {
45
        $this->assertIsASingleCharacterString($delimiter, 'delimiter');
46
        $this->delimiter = $delimiter;
47
        $this->updateCsvControl();
48
    }
49
50
    /**
51
     * @param string $enclosure
52
     * @throws InvalidArgumentException
53
     */
54
    public function setEnclosure($enclosure)
55
    {
56
        $this->assertIsASingleCharacterString($enclosure, 'enclosure');
57
        $this->enclosure = $enclosure;
58
        $this->updateCsvControl();
59
    }
60
61
    /**
62
     * @param string $escapeCharacter
63
     * @throws InvalidArgumentException
64
     */
65
    public function setEscapeCharacter($escapeCharacter)
66
    {
67
        $this->assertIsASingleCharacterString($escapeCharacter, 'escapeCharacter');
68
        $this->escapeCharacter = $escapeCharacter;
69
        $this->updateCsvControl();
70
    }
71
72
    /**
73
     * @param string $path
74
     * @return $this
75
     * @throws InvalidArgumentException
76
     * @todo implement validation
77
     */
78
    public function setPath($path)
79
    {
80
        $this->path     = $path;
81
        $this->handler  = $this->open($path);
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    protected function getDelimiter()
90
    {
91
        return $this->delimiter;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    protected function getEnclosure()
98
    {
99
        return $this->enclosure;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    protected function getEscapeCharacter()
106
    {
107
        return $this->escapeCharacter;
108
    }
109
110
    /**
111
     * @return SplFileObject|resource
112
     */
113
    protected function getFileHandler()
114
    {
115
        return $this->handler;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    abstract protected function getFileHandlerOpenMode();
122
123
    /**
124
     * @return array|false
125
     */
126
    protected function getHeadline()
127
    {
128
        return $this->headline;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    protected function getPath()
135
    {
136
        return $this->path;
137
    }
138
139
    /**
140
     * @return $this
141
     */
142
    protected function resetHeadline()
143
    {
144
        $this->headline = false;
145
146
        return $this;
147
    }
148
149
    /**
150
     * @param array $headline
151
     * @return $this
152
     */
153
    protected function setHeadline(array $headline)
154
    {
155
        $this->headline = $headline;
156
157
        return $this;
158
    }
159
160
    protected function close()
161
    {
162
        if (!is_null($this->handler)) {
163
            $this->headline = null;
164
        }
165
    }
166
167
    /**
168
     * @param string $path
169
     * @return SplFileObject
170
     * @todo inject or inject factory
171
     */
172
    protected function open($path)
173
    {
174
        $file = new SplFileObject($path, $this->getFileHandlerOpenMode());
175
        $file->setFlags(SplFileObject::READ_CSV | SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
176
177
        return $file;
178
    }
179
180
    /**
181
     * @param string $variable
182
     * @param string $name
183
     * @throws InvalidArgumentException
184
     */
185
    private function assertIsASingleCharacterString($variable, $name)
186
    {
187
        if (!is_string($variable)) {
188
            $message = $name . ' must be of type "string"';
189
190
            throw new InvalidArgumentException($message);
191
        }
192
        if (strlen($variable) != 1) {
193
            $message = $name . ' must be a single character';
194
195
            throw new InvalidArgumentException($message);
196
        }
197
    }
198
199
    private function updateCsvControl()
200
    {
201
        $file = $this->getFileHandler();
202
203
        if ($file instanceof SplFileObject) {
204
            $file->setCsvControl(
205
                $this->getDelimiter(),
206
                $this->getEnclosure(),
207
                $this->getEscapeCharacter()
208
            );
209
        }
210
    }
211
}