|
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.1 |
|
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 InvalidArgumentException; |
|
18
|
|
|
use php_user_filter; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A stream filter to conform the CSV field to RFC4180 |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://tools.ietf.org/html/rfc4180#section-2 |
|
24
|
|
|
* |
|
25
|
|
|
* @package League.csv |
|
26
|
|
|
* @since 9.0.0 |
|
27
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class RFC4180Field extends php_user_filter |
|
30
|
|
|
{ |
|
31
|
|
|
const FILTERNAME = 'convert.league.csv.rfc4180'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* the filter name used to instantiate the class with |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $filtername; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Contents of the params parameter passed to stream_filter_append |
|
42
|
|
|
* or stream_filter_prepend functions |
|
43
|
|
|
* |
|
44
|
|
|
* @var mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
public $params; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The value being search for |
|
50
|
|
|
* |
|
51
|
|
|
* @var string[] |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $search; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The replacement value that replace found $search values |
|
57
|
|
|
* |
|
58
|
|
|
* @var string[] |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $replace; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Characters that triggers enclosure with PHP fputcsv |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected static $force_enclosure = "\n\r\t "; |
|
68
|
6 |
|
|
|
69
|
|
|
/** |
|
70
|
6 |
|
* Static method to add the stream filter to a {@link AbstractCsv} object |
|
71
|
|
|
* |
|
72
|
6 |
|
* @param AbstractCsv $csv |
|
73
|
6 |
|
* @param string $whitespace_replace |
|
74
|
6 |
|
* |
|
75
|
6 |
|
* @return AbstractCsv |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function addTo(AbstractCsv $csv, string $whitespace_replace = ''): AbstractCsv |
|
78
|
|
|
{ |
|
79
|
|
|
self::register(); |
|
80
|
|
|
|
|
81
|
|
|
$params = [ |
|
82
|
6 |
|
'enclosure' => $csv->getEnclosure(), |
|
83
|
|
|
'escape' => $csv->getEscape(), |
|
84
|
6 |
|
'mode' => $csv->getStreamFilterMode(), |
|
85
|
2 |
|
]; |
|
86
|
|
|
|
|
87
|
6 |
|
if ($csv instanceof Writer && '' != $whitespace_replace) { |
|
88
|
|
|
self::addFormatterTo($csv, $whitespace_replace); |
|
89
|
|
|
$params['whitespace_replace'] = $whitespace_replace; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $csv->addStreamFilter(self::FILTERNAME, $params); |
|
93
|
|
|
} |
|
94
|
4 |
|
|
|
95
|
|
|
/** |
|
96
|
4 |
|
* Add a formatter to the {@link Writer} object to format the record |
|
97
|
|
|
* field to avoid enclosure around a field with an empty space |
|
98
|
|
|
* |
|
99
|
|
|
* @param Writer $csv |
|
100
|
|
|
* @param string $whitespace_replace |
|
101
|
|
|
* |
|
102
|
18 |
|
* @return Writer |
|
103
|
|
|
*/ |
|
104
|
18 |
|
public static function addFormatterTo(Writer $csv, string $whitespace_replace): Writer |
|
105
|
10 |
|
{ |
|
106
|
|
|
if ('' == $whitespace_replace || strlen($whitespace_replace) != strcspn($whitespace_replace, self::$force_enclosure)) { |
|
107
|
|
|
throw new InvalidArgumentException('The sequence contains a character that enforces enclosure or is a CSV control character or is the empty string.'); |
|
108
|
6 |
|
} |
|
109
|
6 |
|
|
|
110
|
6 |
|
$mapper = function ($value) use ($whitespace_replace) { |
|
111
|
4 |
|
if (is_string($value)) { |
|
112
|
|
|
return str_replace(' ', $whitespace_replace, $value); |
|
113
|
|
|
} |
|
114
|
6 |
|
|
|
115
|
|
|
return $value; |
|
116
|
|
|
}; |
|
117
|
|
|
|
|
118
|
|
|
$formatter = function (array $record) use ($mapper): array { |
|
119
|
|
|
return array_map($mapper, $record); |
|
120
|
|
|
}; |
|
121
|
|
|
|
|
122
|
|
|
return $csv->addFormatter($formatter); |
|
123
|
16 |
|
} |
|
124
|
|
|
|
|
125
|
16 |
|
/** |
|
126
|
|
|
* Static method to register the class as a stream filter |
|
127
|
16 |
|
*/ |
|
128
|
16 |
|
public static function register() |
|
129
|
16 |
|
{ |
|
130
|
|
|
if (!in_array(self::FILTERNAME, stream_get_filters())) { |
|
131
|
|
|
stream_filter_register(self::FILTERNAME, __CLASS__); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
6 |
|
/** |
|
136
|
|
|
* Static method to return the stream filter filtername |
|
137
|
6 |
|
* |
|
138
|
6 |
|
* @return string |
|
139
|
6 |
|
*/ |
|
140
|
6 |
|
public static function getFiltername(): string |
|
141
|
|
|
{ |
|
142
|
|
|
return self::FILTERNAME; |
|
143
|
6 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @inheritdoc |
|
147
|
|
|
*/ |
|
148
|
|
|
public function filter($in, $out, &$consumed, $closing) |
|
149
|
|
|
{ |
|
150
|
|
|
while ($bucket = stream_bucket_make_writeable($in)) { |
|
151
|
|
|
$bucket->data = str_replace($this->search, $this->replace, $bucket->data); |
|
152
|
|
|
$consumed += $bucket->datalen; |
|
153
|
|
|
stream_bucket_append($out, $bucket); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return PSFS_PASS_ON; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @inheritdoc |
|
161
|
|
|
*/ |
|
162
|
|
|
public function onCreate() |
|
163
|
|
|
{ |
|
164
|
|
|
if (!$this->isValidParams($this->params)) { |
|
165
|
|
|
return false; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$this->search = [$this->params['escape'].$this->params['enclosure']]; |
|
169
|
|
|
$this->replace = [$this->params['enclosure'].$this->params['enclosure']]; |
|
170
|
|
|
if (STREAM_FILTER_WRITE != $this->params['mode']) { |
|
171
|
|
|
return true; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$this->search = [$this->params['escape'].$this->params['enclosure']]; |
|
175
|
|
|
$this->replace = [$this->params['escape'].$this->params['enclosure'].$this->params['enclosure']]; |
|
176
|
|
|
if ($this->isValidSequence($this->params)) { |
|
177
|
|
|
$this->search[] = $this->params['whitespace_replace']; |
|
178
|
|
|
$this->replace[] = ' '; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return true; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Validate params property |
|
186
|
|
|
* |
|
187
|
|
|
* @param array $params |
|
188
|
|
|
* |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function isValidParams(array $params): bool |
|
192
|
|
|
{ |
|
193
|
|
|
static $mode_list = [STREAM_FILTER_READ => 1, STREAM_FILTER_WRITE => 1]; |
|
194
|
|
|
|
|
195
|
|
|
return isset($params['enclosure'], $params['escape'], $params['mode'], $mode_list[$params['mode']]) |
|
196
|
|
|
&& 1 == strlen($params['enclosure']) |
|
197
|
|
|
&& 1 == strlen($params['escape']); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Is Valid White space replaced sequence |
|
202
|
|
|
* |
|
203
|
|
|
* @param array $params |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function isValidSequence(array $params) |
|
208
|
|
|
{ |
|
209
|
|
|
return isset($params['whitespace_replace']) |
|
210
|
|
|
&& strlen($params['whitespace_replace']) == strcspn($params['whitespace_replace'], self::$force_enclosure); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|