Completed
Pull Request — master (#210)
by ignace nyamagana
02:08
created

ControlsTrait::removeStreamFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
declare(strict_types=1);
14
15
namespace League\Csv;
16
17
use InvalidArgumentException;
18
use LogicException;
19
use SplFileObject;
20
21
/**
22
 *  An abstract class to enable basic CSV manipulation
23
 *
24
 * @package  League.csv
25
 * @since    9.0.0
26
 * @author   Ignace Nyamagana Butera <[email protected]>
27
 * @internal
28
 */
29
trait ControlsTrait
30
{
31
    use ValidatorTrait;
32
33
    /**
34
     * The CSV document
35
     *
36
     * can be a StreamIterator object, a SplFileObject object or the string path to a file
37
     *
38
     * @var StreamIterator|SplFileObject
39
     */
40
    protected $document;
41
42
    /**
43
     * the field delimiter (one character only)
44
     *
45
     * @var string
46
     */
47
    protected $delimiter = ',';
48
49
    /**
50
     * the field enclosure character (one character only)
51
     *
52
     * @var string
53
     */
54
    protected $enclosure = '"';
55
56
    /**
57
     * the field escape character (one character only)
58
     *
59
     * @var string
60
     */
61
    protected $escape = '\\';
62
63
    /**
64
     * The Output file BOM character
65
     * @var string
66
     */
67
    protected $output_bom = '';
68
69
    /**
70
     * collection of stream filters
71
     *
72
     * @var array
73
     */
74
    protected $stream_filters = [];
75
76
    /**
77
     * The stream filter mode (read or write)
78
     */
79
    protected $stream_filter_mode;
80
81
    /**
82
     * Returns the current field delimiter
83
     *
84
     * @return string
85
     */
86 4
    public function getDelimiter(): string
87
    {
88 4
        return $this->delimiter;
89
    }
90
91
    /**
92
     * Returns the current field enclosure
93
     *
94
     * @return string
95
     */
96 4
    public function getEnclosure(): string
97
    {
98 4
        return $this->enclosure;
99
    }
100
101
    /**
102
     * Returns the current field escape character
103
     *
104
     * @return string
105
     */
106 4
    public function getEscape(): string
107
    {
108 4
        return $this->escape;
109
    }
110
111
    /**
112
     * Returns the BOM sequence in use on Output methods
113
     *
114
     * @return string
115
     */
116 2
    public function getOutputBOM(): string
117
    {
118 2
        return $this->output_bom;
119
    }
120
121
    /**
122
     * Returns the BOM sequence of the given CSV
123
     *
124
     * @return string
125
     */
126 144
    public function getInputBOM(): string
127
    {
128
        $bom = [
129 144
            AbstractCsv::BOM_UTF32_BE, AbstractCsv::BOM_UTF32_LE,
130
            AbstractCsv::BOM_UTF16_BE, AbstractCsv::BOM_UTF16_LE, AbstractCsv::BOM_UTF8,
131
        ];
132
133 144
        $this->document->setFlags(SplFileObject::READ_CSV);
134 144
        $this->document->rewind();
135 144
        $line = $this->document->fgets();
136 144
        $res = array_filter($bom, function ($sequence) use ($line) {
137 144
            return strpos($line, $sequence) === 0;
138 144
        });
139
140 144
        return (string) array_shift($res);
141
    }
142
143
    /**
144
     * Tells whether the stream filter capabilities can be used
145
     *
146
     * @return bool
147
     */
148 4
    public function isStream(): bool
149
    {
150 4
        return $this->document instanceof StreamIterator;
151
    }
152
153
    /**
154
     * Tell whether the specify stream filter is attach to the current stream
155
     *
156
     * @return bool
157
     */
158 4
    public function hasStreamFilter(string $filtername): bool
159
    {
160 4
        return isset($this->stream_filters[$filtername]);
161
    }
162
163
    /**
164
     * Sets the field delimiter
165
     *
166
     * @param string $delimiter
167
     *
168
     * @throws InvalidArgumentException If $delimiter is not a single character
169
     *
170
     * @return $this
171
     */
172 192
    public function setDelimiter(string $delimiter): self
173
    {
174 192
        $this->delimiter = $this->filterControl($delimiter, 'delimiter');
175
176 192
        return $this;
177
    }
178
179
    /**
180
     * Sets the field enclosure
181
     *
182
     * @param string $enclosure
183
     *
184
     * @throws InvalidArgumentException If $enclosure is not a single character
185
     *
186
     * @return $this
187
     */
188 192
    public function setEnclosure(string $enclosure): self
189
    {
190 192
        $this->enclosure = $this->filterControl($enclosure, 'enclosure');
191
192 192
        return $this;
193
    }
194
195
    /**
196
     * Sets the field escape character
197
     *
198
     * @param string $escape
199
     *
200
     * @throws InvalidArgumentException If $escape is not a single character
201
     *
202
     * @return $this
203
     */
204 192
    public function setEscape(string $escape): self
205
    {
206 192
        $this->escape = $this->filterControl($escape, 'escape');
207
208 192
        return $this;
209
    }
210
211
    /**
212
     * Sets the BOM sequence to prepend the CSV on output
213
     *
214
     * @param string $str The BOM sequence
215
     *
216
     * @return static
217
     */
218 6
    public function setOutputBOM(string $str): self
219
    {
220 6
        $this->output_bom = $str;
221
222 6
        return $this;
223
    }
224
225
    /**
226
     * append a stream filter
227
     *
228
     * @param string $filtername a string or an object that implements the '__toString' method
229
     *
230
     * @return $this
231
     */
232 12
    public function addStreamFilter(string $filtername): self
233
    {
234 12
        if (!$this->document instanceof StreamIterator) {
235 2
            throw new LogicException('The stream filter API can not be used');
236
        }
237
238 10
        $this->stream_filters[$filtername][] = $this->document->appendFilter($filtername, $this->stream_filter_mode);
239
240 10
        return $this;
241
    }
242
243
    /**
244
     * Remove all registered stream filter
245
     */
246 202
    protected function clearStreamFilter()
247
    {
248 202
        foreach (array_keys($this->stream_filters) as $filtername) {
249 10
            $this->removeStreamFilter($filtername);
250
        }
251
252 202
        $this->stream_filters = [];
253 202
    }
254
255
    /**
256
     * Remove all the stream filter with the same name
257
     *
258
     * @param string $filtername the stream filter name
259
     */
260 10
    protected function removeStreamFilter(string $filtername)
261
    {
262 10
        foreach ($this->stream_filters[$filtername] as $filter) {
263 10
            $this->document->removeFilter($filter);
264
        }
265
266 10
        unset($this->stream_filters[$filtername]);
267 10
    }
268
}
269