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

StreamTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 110
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isActiveStreamFilter() 0 4 1
A hasStreamFilter() 0 4 1
A clearStreamFilter() 0 10 2
A removeStreamFilter() 0 13 3
A assertStreamable() 0 6 2
A addStreamFilter() 0 10 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 12
    public function isActiveStreamFilter(): bool
57
    {
58 12
        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
     * Remove all registered stream filter
73
     *
74
     * @return $this
75
     */
76 214
    public function clearStreamFilter(): self
77
    {
78 214
        foreach (array_keys($this->stream_filters) as $filter_name) {
79 10
            $this->removeStreamFilter($filter_name);
80
        }
81
82 214
        $this->stream_filters = [];
83
84 214
        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 12
    public function removeStreamFilter(string $filter_name): self
95
    {
96 12
        if (!isset($this->stream_filters[$filter_name])) {
97 2
            return $this;
98
        }
99
100 10
        foreach ($this->stream_filters[$filter_name] as $filter) {
101 10
            $this->document->removeFilter($filter);
102
        }
103
104 10
        unset($this->stream_filters[$filter_name]);
105 10
        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 12
    public function addStreamFilter(string $filter_name): self
116
    {
117 12
        $this->assertStreamable();
118 10
        $this->stream_filters[$filter_name][] = $this->document->appendFilter(
119
            $filter_name,
120 10
            $this->stream_filter_mode
121
        );
122
123 10
        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 12
    protected function assertStreamable()
132
    {
133 12
        if (!$this->isActiveStreamFilter()) {
134 2
            throw new LogicException('The stream filter API can not be used');
135
        }
136 10
    }
137
}
138