1 | <?php |
||
31 | class CharsetConverter extends php_user_filter |
||
32 | { |
||
33 | use ValidatorTrait; |
||
34 | |||
35 | const 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 return the stream filter filtername |
||
53 | * |
||
54 | * @param string $input_encoding |
||
55 | * @param string $output_encoding |
||
56 | * @return string |
||
57 | */ |
||
58 | 2 | public static function getFiltername(string $input_encoding, string $output_encoding): string |
|
67 | |||
68 | /** |
||
69 | * Static method to register the class as a stream filter |
||
70 | */ |
||
71 | 2 | public static function register() |
|
78 | |||
79 | /** |
||
80 | * Static method to add the stream filter to a {@link AbstractCsv} object |
||
81 | * |
||
82 | * @param AbstractCsv $csv |
||
83 | * @param string $input_encoding |
||
84 | * @param string $output_encoding |
||
85 | * |
||
86 | * @return AbstractCsv |
||
87 | */ |
||
88 | 2 | public static function addTo(AbstractCsv $csv, string $input_encoding, string $output_encoding): AbstractCsv |
|
94 | |||
95 | /** |
||
96 | * Filter encoding charset |
||
97 | * |
||
98 | * @param string $encoding |
||
99 | * |
||
100 | * @throws OutOfRangeException if the charset is malformed or unsupported |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 4 | protected static function filterEncoding(string $encoding): string |
|
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | 2 | public function filter($in, $out, &$consumed, $closing) |
|
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | 8 | public function onCreate() |
|
157 | |||
158 | /** |
||
159 | * Enable using the class as a formatter for the {@link Writer} |
||
160 | * |
||
161 | * @param array $record CSV record |
||
162 | * |
||
163 | * @return string[] |
||
164 | */ |
||
165 | 2 | public function __invoke(array $record): array |
|
173 | |||
174 | /** |
||
175 | * Walker method to convert the offset and the value of a CSV record field |
||
176 | * |
||
177 | * @param string|null &$value |
||
178 | * @param string|int &$offset |
||
179 | */ |
||
180 | 2 | protected function encodeField(&$value, &$offset) |
|
190 | |||
191 | /** |
||
192 | * Convert Csv file into UTF-8 |
||
193 | * |
||
194 | * @param array|Traversable $records the CSV records collection |
||
195 | * |
||
196 | * @return Iterator |
||
197 | */ |
||
198 | 4 | public function convert($records): Iterator |
|
216 | |||
217 | /** |
||
218 | * Sets the records input encoding charset |
||
219 | * |
||
220 | * @param string $encoding |
||
221 | * |
||
222 | * @return self |
||
223 | */ |
||
224 | 6 | public function inputEncoding(string $encoding): self |
|
236 | |||
237 | /** |
||
238 | * Sets the records output encoding charset |
||
239 | * |
||
240 | * @param string $encoding |
||
241 | * |
||
242 | * @return self |
||
243 | */ |
||
244 | 2 | public function outputEncoding(string $encoding): self |
|
256 | } |
||
257 |