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

StreamTrait::appendStreamFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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 4
    public function isStream(): bool
57
    {
58 4
        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 4
    public function hasStreamFilter(string $filter_name): bool
67
    {
68 4
        return isset($this->stream_filters[$filter_name]);
69
    }
70
71
    /**
72
     * append a stream filter
73
     *
74
     * @param string $filter_name a string or an object that implements the '__toString' method
75
     *
76
     * @return $this
77
     */
78 12
    public function addStreamFilter(string $filter_name): self
79
    {
80 12
        if (!$this->document instanceof StreamIterator) {
81 2
            throw new LogicException('The stream filter API can not be used');
82
        }
83
84 10
        $this->stream_filters[$filter_name][] = $this->document->appendFilter(
85
            $filter_name,
86 10
            $this->stream_filter_mode
87
        );
88
89 10
        return $this;
90
    }
91
92
    /**
93
     * Remove all registered stream filter
94
     */
95 218
    protected function clearStreamFilter()
96
    {
97 218
        foreach (array_keys($this->stream_filters) as $filter_name) {
98 10
            $this->removeStreamFilter($filter_name);
99
        }
100
101 218
        $this->stream_filters = [];
102 218
    }
103
104
    /**
105
     * Remove all the stream filter with the same name
106
     *
107
     * @param string $filter_name the stream filter name
108
     */
109 10
    protected function removeStreamFilter(string $filter_name)
110
    {
111 10
        foreach ($this->stream_filters[$filter_name] as $filter) {
112 10
            $this->document->removeFilter($filter);
113
        }
114
115 10
        unset($this->stream_filters[$filter_name]);
116 10
    }
117
}
118