1 | <?php |
||
30 | class CharsetConverter extends php_user_filter |
||
31 | { |
||
32 | use ValidatorTrait; |
||
33 | |||
34 | const STREAM_FILTERNAME = 'convert.league.csv'; |
||
35 | |||
36 | /** |
||
37 | * The records input encoding charset |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $input_encoding = 'UTF-8'; |
||
42 | |||
43 | /** |
||
44 | * The records output encoding charset |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $output_encoding = 'UTF-8'; |
||
49 | |||
50 | /** |
||
51 | * Static method to register the class as a PHP stream filter |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | 4 | public static function registerStreamFilter(): bool |
|
65 | |||
66 | /** |
||
67 | * Static method to format the filtername to be used with stream_filter_append |
||
68 | * |
||
69 | * @param string $input_encoding |
||
70 | * @param string $output_encoding |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 2 | public static function getFiltername(string $input_encoding, string $output_encoding): string |
|
83 | |||
84 | /** |
||
85 | * Filter encoding charset |
||
86 | * |
||
87 | * @param string $encoding |
||
88 | * |
||
89 | * @throws InvalidArgumentException if the charset is malformed or unsupported |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 10 | protected static function filterEncoding(string $encoding): string |
|
108 | |||
109 | /** |
||
110 | * @inherit |
||
111 | */ |
||
112 | 2 | public function filter($in, $out, &$consumed, $closing) |
|
122 | |||
123 | /** |
||
124 | * @inherit |
||
125 | */ |
||
126 | 4 | public function onCreate() |
|
137 | |||
138 | /** |
||
139 | * Enable using the class as a formatter for the {@link Writer} |
||
140 | * |
||
141 | * @param array $record CSV record |
||
142 | * |
||
143 | * @return string[] |
||
144 | */ |
||
145 | 2 | public function __invoke(array $record): array |
|
153 | |||
154 | /** |
||
155 | * Walker method to convert the offset and the value of a CSV record field |
||
156 | * |
||
157 | * @param string|null &$value |
||
158 | * @param string|int &$offset |
||
159 | */ |
||
160 | 4 | protected function encodeField(&$value, &$offset) |
|
170 | |||
171 | /** |
||
172 | * Convert Csv file into UTF-8 |
||
173 | * |
||
174 | * @param array|Traversable $records the CSV records collection |
||
175 | * |
||
176 | * @return Iterator |
||
177 | */ |
||
178 | 4 | public function convert($records): Iterator |
|
196 | |||
197 | /** |
||
198 | * Sets the records input encoding charset |
||
199 | * |
||
200 | * @param string $encoding |
||
201 | * |
||
202 | * @return self |
||
203 | */ |
||
204 | 6 | public function inputEncoding(string $encoding): self |
|
216 | |||
217 | /** |
||
218 | * Sets the records output encoding charset |
||
219 | * |
||
220 | * @param string $encoding |
||
221 | * |
||
222 | * @return self |
||
223 | */ |
||
224 | 4 | public function outputEncoding(string $encoding): self |
|
236 | } |
||
237 |