Completed
Push — master ( 4f35ea...a6415f )
by ignace nyamagana
05:31
created

RFC4180Field::onCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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