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

StreamTrait::getStreamFilterPrefix()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
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\Config;
16
17
use League\Csv\StreamIterator;
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
 * @internal
27
 */
28
trait StreamTrait
29
{
30
    /**
31
     * The CSV document
32
     *
33
     * can be a StreamIterator object, a SplFileObject object or the string path to a file
34
     *
35
     * @var StreamIterator|SplFileObject
36
     */
37
    protected $document;
38
39
    /**
40
     * collection of stream filters
41
     *
42
     * @var array
43
     */
44
    protected $stream_filters = [];
45
46
    /**
47
     * The stream filter mode (read or write)
48
     */
49
    protected $stream_filter_mode;
50
51
    /**
52
     * Tells whether the stream filter capabilities can be used
53
     *
54
     * @return bool
55
     */
56
    public function isActiveStreamFilter(): bool
57
    {
58
        return $this->document instanceof StreamIterator;
59
    }
60
61
    /**
62
     * Tell whether the specify stream filter is attach to the current stream
63
     *
64
     * @return bool
65
     */
66
    public function hasStreamFilter(string $filter_name): bool
67
    {
68
        return isset($this->stream_filters[$filter_name]);
69
    }
70
71
    /**
72
     * Remove all registered stream filter
73
     *
74
     * @return $this
75
     */
76
    public function clearStreamFilter(): self
77
    {
78
        foreach (array_keys($this->stream_filters) as $filter_name) {
79
            $this->removeStreamFilter($filter_name);
80
        }
81
82
        $this->stream_filters = [];
83
84
        return $this;
85
    }
86
87
    /**
88
     * Remove all the stream filter with the same name
89
     *
90
     * @param string $filter_name the stream filter name
91
     *
92
     * @return $this
93
     */
94
    public function removeStreamFilter(string $filter_name): self
95
    {
96
        if (!isset($this->stream_filters[$filter_name])) {
97
            return $this;
98
        }
99
100
        foreach ($this->stream_filters[$filter_name] as $filter) {
101
            $this->document->removeFilter($filter);
102
        }
103
104
        unset($this->stream_filters[$filter_name]);
105
        return $this;
106
    }
107
108
    /**
109
     * append a stream filter
110
     *
111
     * @param string $filter_name a string or an object that implements the '__toString' method
112
     *
113
     * @return $this
114
     */
115
    public function appendStreamFilter(string $filter_name): self
116
    {
117
        $this->assertStreamable();
118
        $this->stream_filters[$filter_name][] = $this->document->appendFilter(
119
            $filter_name,
120
            $this->stream_filter_mode
121
        );
122
123
        return $this;
124
    }
125
126
    /**
127
     * Check if the trait methods can be used
128
     *
129
     * @throws LogicException If the API can not be use
130
     */
131
    protected function assertStreamable()
132
    {
133
        if (!$this->isActiveStreamFilter()) {
134
            throw new LogicException('The stream filter API can not be used');
135
        }
136
    }
137
138
    /**
139
     * prepend a stream filter
140
     *
141
     * @param string $filter_name the stream filter name
142
     *
143
     * @return $this
144
     */
145
    public function prependStreamFilter(string $filter_name): self
146
    {
147
        $this->assertStreamable();
148
        $this->stream_filters[$filter_name][] = $this->document->prependFilter(
149
            $filter_name,
150
            $this->stream_filter_mode
151
        );
152
153
        return $this;
154
    }
155
}
156