Completed
Pull Request — master (#233)
by ignace nyamagana
02:19
created

RFC4180Field::onCreate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 4
nop 0
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
c 1
b 0
f 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;
16
17
use php_user_filter;
18
use Throwable;
19
20
/**
21
 *  A stream filter to conform the written CSV field to RFC4180
22
 *  This stream filter should be attach to a League\Csv\Writer object
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 STREAM_FILTERNAME = 'rfc4180.league.csv';
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 add the stream filter to a Writer object
50
     *
51
     * @param AbstractCsv $csv
52
     */
53 2
    public static function addTo(AbstractCsv $csv)
54
    {
55 2
        if (!in_array(self::STREAM_FILTERNAME, stream_get_filters())) {
56 2
            stream_filter_register(self::STREAM_FILTERNAME, __CLASS__);
57
        }
58
59 2
        $csv->addStreamFilter(self::STREAM_FILTERNAME, [
60 2
            'enclosure' => $csv->getEnclosure(),
61 2
            'escape' => $csv->getEscape(),
62
        ]);
63 2
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 6
    public function onCreate()
69
    {
70 6
        if (!isset($this->params['enclosure'], $this->params['escape'])) {
71 2
            return false;
72
        }
73
74
        try {
75 4
            $enclosure = $this->filterControl($this->params['enclosure'], 'enclosure', __METHOD__);
76 4
            $escape = $this->filterControl($this->params['escape'], 'escape', __METHOD__);
77
78 2
            $this->search = $escape.$enclosure;
79 2
            $this->replace = $escape.$enclosure.$enclosure;
80
81 2
            return true;
82 2
        } catch (Throwable $e) {
83 2
            return false;
84
        }
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 2
    public function filter($in, $out, &$consumed, $closing)
91
    {
92 2
        while ($bucket = stream_bucket_make_writeable($in)) {
93 2
            $bucket->data = str_replace($this->search, $this->replace, $bucket->data);
94 2
            $consumed += $bucket->datalen;
95 2
            stream_bucket_append($out, $bucket);
96
        }
97
98 2
        return PSFS_PASS_ON;
99
    }
100
}
101