Completed
Push — master ( 4bfe5f...40575e )
by ignace nyamagana
05:42 queued 04:25
created

RFC4180Field::init()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 12
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 4
rs 9.2
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;
16
17
use php_user_filter;
18
19
/**
20
 *  A stream filter to conform the CSV field to RFC4180
21
 *
22
 * @package League.csv
23
 * @since   9.0.0
24
 * @author  Ignace Nyamagana Butera <[email protected]>
25
 */
26
class RFC4180Field extends php_user_filter
27
{
28
    const FILTERNAME = 'convert.league.csv.rfc4180';
29
30
    /**
31
     * the filter name used to instantiate the class with
32
     *
33
     * @var string
34
     */
35
    public $filtername;
36
37
    /**
38
     * Contents of the params parameter passed to stream_filter_append
39
     * or stream_filter_prepend functions
40
     *
41
     * @var mixed
42
     */
43
    public $params;
44
45
    /**
46
     * The value being search for
47
     *
48
     * @var string
49
     */
50
    protected $search;
51
52
    /**
53
     * The replacement value that replace found $search values
54
     *
55
     * @var string
56
     */
57
    protected $replace;
58
59
    /**
60
     * Static method to register the class as a stream filter
61
     */
62 6
    public static function register()
63
    {
64 6
        if (!in_array(self::FILTERNAME, stream_get_filters())) {
65 2
            stream_filter_register(self::FILTERNAME, __CLASS__);
66
        }
67 6
    }
68
69
    /**
70
     * Static method to return the stream filter filtername
71
     *
72
     * @return string
73
     */
74 4
    public static function getFiltername(): string
75
    {
76 4
        return self::FILTERNAME;
77
    }
78
79
    /**
80
     * Static method to add the stream filter to a {@link AbstractCsv} object
81
     *
82
     * @param AbstractCsv $csv
83
     *
84
     * @return AbstractCsv
85
     */
86 6
    public static function addTo(AbstractCsv $csv): AbstractCsv
87
    {
88 6
        self::register();
89
90 6
        return $csv->addStreamFilter(self::FILTERNAME, [
91 6
            'enclosure' => $csv->getEnclosure(),
92 6
            'escape' => $csv->getEscape(),
93 6
            'mode' => $csv->getStreamFilterMode(),
94
        ]);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 16
    public function onCreate()
101
    {
102 16
        if (!$this->isValidParams()) {
103 10
            return false;
104
        }
105
106 6
        $this->search = $this->params['escape'].$this->params['enclosure'];
107 6
        $this->replace = $this->params['enclosure'].$this->params['enclosure'];
108 6
        if (STREAM_FILTER_WRITE === $this->params['mode']) {
109 4
            $this->replace = $this->search.$this->params['enclosure'];
110
        }
111
112 6
        return true;
113
    }
114
115
    /**
116
     * Validate params property
117
     *
118
     * @return bool
119
     */
120 14
    protected function isValidParams(): bool
121
    {
122 14
        static $mode_list = [STREAM_FILTER_READ => 1, STREAM_FILTER_WRITE => 1];
123
124 14
        return isset($this->params['enclosure'],
125 14
            $this->params['escape'],
126 14
            $this->params['mode'],
127 13
            $mode_list[$this->params['mode']]
128 14
        ) && 1 == strlen($this->params['enclosure']) && 1 == strlen($this->params['escape']);
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134 6
    public function filter($in, $out, &$consumed, $closing)
135
    {
136 6
        while ($bucket = stream_bucket_make_writeable($in)) {
137 6
            $bucket->data = str_replace($this->search, $this->replace, $bucket->data);
138 6
            $consumed += $bucket->datalen;
139 6
            stream_bucket_append($out, $bucket);
140
        }
141
142 6
        return PSFS_PASS_ON;
143
    }
144
}
145