1 | <?php |
||
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) |
|
144 | } |
||
145 |