Completed
Pull Request — master (#210)
by ignace nyamagana
03:05
created

StreamTrait::getStreamFilterPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
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 18
    public function isActiveStreamFilter(): bool
57
    {
58 18
        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 2
    public function hasStreamFilter(string $filter_name): bool
67
    {
68 2
        return isset($this->stream_filters[$filter_name]);
69
    }
70
71
    /**
72
     * Remove all registered stream filter
73
     *
74
     * @return $this
75
     */
76 226
    public function clearStreamFilter(): self
77
    {
78 226
        foreach (array_keys($this->stream_filters) as $filter_name) {
79 14
            $this->removeStreamFilter($filter_name);
80
        }
81
82 226
        $this->stream_filters = [];
83
84 226
        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 16
    public function removeStreamFilter(string $filter_name): self
95
    {
96 16
        if (!isset($this->stream_filters[$filter_name])) {
97 2
            return $this;
98
        }
99
100 14
        foreach ($this->stream_filters[$filter_name] as $filter) {
101 14
            $this->document->removeFilter($filter);
102
        }
103
104 14
        unset($this->stream_filters[$filter_name]);
105 14
        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 16
    public function appendStreamFilter(string $filter_name): self
116
    {
117 16
        $this->assertStreamable();
118 14
        $this->stream_filters[$filter_name][] = $this->document->appendFilter(
119
            $filter_name,
120 14
            $this->stream_filter_mode
121
        );
122
123 14
        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 18
    protected function assertStreamable()
132
    {
133 18
        if (!$this->isActiveStreamFilter()) {
134 4
            throw new LogicException('The stream filter API can not be used');
135
        }
136 14
    }
137
138
    /**
139
     * prepend a stream filter
140
     *
141
     * @param string $filter_name the stream filter name
142
     *
143
     * @return $this
144
     */
145 4
    public function prependStreamFilter(string $filter_name): self
146
    {
147 4
        $this->assertStreamable();
148 2
        $this->stream_filters[$filter_name][] = $this->document->prependFilter(
149
            $filter_name,
150 2
            $this->stream_filter_mode
151
        );
152
153 2
        return $this;
154
    }
155
}
156