Completed
Pull Request — master (#210)
by ignace nyamagana
02:58 queued 01:13
created

StreamTrait::getStreamFilterMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
     * The CSV stream filter mode
41
     */
42
    protected $stream_filter_mode;
43
44
    /**
45
     * collection of stream filters
46
     *
47
     * @var array
48
     */
49
    protected $stream_filters = [];
50
51
    /**
52
     * Check if the trait methods can be used
53
     *
54
     * @throws LogicException If the API can not be use
55
     */
56
    protected function assertStreamable()
57
    {
58
        if (!$this->isActiveStreamFilter()) {
59
            throw new LogicException('The stream filter API can not be used');
60
        }
61
    }
62
63
    /**
64
     * Tells whether the stream filter capabilities can be used
65
     *
66
     * @return bool
67
     */
68
    public function isActiveStreamFilter(): bool
69
    {
70
        return $this->document instanceof StreamIterator;
71
    }
72
73
    /**
74
     * stream filter mode getter
75
     *
76
     * @return int
77
     */
78
    public function getStreamFilterMode(): int
79
    {
80
        return $this->stream_filter_mode;
81
    }
82
83
    /**
84
     * append a stream filter
85
     *
86
     * @param string $filter_name a string or an object that implements the '__toString' method
87
     *
88
     * @return $this
89
     */
90
    public function appendStreamFilter(string $filter_name): self
91
    {
92
        $this->assertStreamable();
93
        $this->stream_filters[] = $this->document->appendFilter($filter_name, $this->stream_filter_mode);
94
95
        return $this;
96
    }
97
98
    /**
99
     * prepend a stream filter
100
     *
101
     * @param string $filter_name a string or an object that implements the '__toString' method
102
     *
103
     * @return $this
104
     */
105
    public function prependStreamFilter(string $filter_name): self
106
    {
107
        $this->assertStreamable();
108
        $this->stream_filters[] = $this->document->prependFilter($filter_name, $this->stream_filter_mode);
109
110
        return $this;
111
    }
112
113
    /**
114
     * Remove all registered stream filter
115
     *
116
     * @return $this
117
     */
118
    public function clearStreamFilter(): self
119
    {
120
        if (!$this->isActiveStreamFilter()) {
121
            return $this;
122
        }
123
124
        foreach ($this->stream_filters as $filter) {
125
            $this->document->removeFilter($filter);
126
        }
127
128
        $this->stream_filters = [];
129
130
        return $this;
131
    }
132
}
133