1 | <?php |
||
31 | class CharsetConverter extends php_user_filter |
||
32 | { |
||
33 | use ValidatorTrait; |
||
34 | |||
35 | const STREAM_FILTERNAME = 'convert.league.csv'; |
||
36 | |||
37 | /** |
||
38 | * The records input encoding charset |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $input_encoding = 'UTF-8'; |
||
43 | |||
44 | /** |
||
45 | * The records output encoding charset |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $output_encoding = 'UTF-8'; |
||
50 | |||
51 | /** |
||
52 | * Static method to add the stream filter to a CSV object |
||
53 | * |
||
54 | * @param AbstractCsv $csv |
||
55 | * @param string $input_encoding |
||
56 | * @param string $output_encoding |
||
57 | */ |
||
58 | 2 | public static function addTo(AbstractCsv $csv, string $input_encoding, string $output_encoding) |
|
59 | { |
||
60 | 2 | if (!in_array(self::STREAM_FILTERNAME, stream_get_filters())) { |
|
61 | 2 | stream_filter_register(self::STREAM_FILTERNAME.'.*', __CLASS__); |
|
62 | } |
||
63 | |||
64 | 2 | $csv->addStreamFilter(sprintf( |
|
65 | 2 | '%s.%s/%s', |
|
66 | 2 | self::STREAM_FILTERNAME, |
|
67 | 2 | self::filterEncoding($input_encoding), |
|
68 | 2 | self::filterEncoding($output_encoding) |
|
69 | )); |
||
70 | 2 | } |
|
71 | |||
72 | /** |
||
73 | * Filter encoding charset |
||
74 | * |
||
75 | * @param string $encoding |
||
76 | * |
||
77 | * @throws OutOfRangeException if the charset is malformed or unsupported |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 4 | protected static function filterEncoding(string $encoding): string |
|
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | 2 | public function filter($in, $out, &$consumed, $closing) |
|
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | 8 | public function onCreate() |
|
134 | |||
135 | /** |
||
136 | * Enable using the class as a formatter for the {@link Writer} |
||
137 | * |
||
138 | * @param array $record CSV record |
||
139 | * |
||
140 | * @return string[] |
||
141 | */ |
||
142 | 2 | public function __invoke(array $record): array |
|
150 | |||
151 | /** |
||
152 | * Walker method to convert the offset and the value of a CSV record field |
||
153 | * |
||
154 | * @param string|null &$value |
||
155 | * @param string|int &$offset |
||
156 | */ |
||
157 | 2 | protected function encodeField(&$value, &$offset) |
|
167 | |||
168 | /** |
||
169 | * Convert Csv file into UTF-8 |
||
170 | * |
||
171 | * @param array|Traversable $records the CSV records collection |
||
172 | * |
||
173 | * @return Iterator |
||
174 | */ |
||
175 | 4 | public function convert($records): Iterator |
|
193 | |||
194 | /** |
||
195 | * Sets the records input encoding charset |
||
196 | * |
||
197 | * @param string $encoding |
||
198 | * |
||
199 | * @return self |
||
200 | */ |
||
201 | 6 | public function inputEncoding(string $encoding): self |
|
213 | |||
214 | /** |
||
215 | * Sets the records output encoding charset |
||
216 | * |
||
217 | * @param string $encoding |
||
218 | * |
||
219 | * @return self |
||
220 | */ |
||
221 | 2 | public function outputEncoding(string $encoding): self |
|
233 | } |
||
234 |