1 | <?php |
||
38 | class CharsetConverter extends php_user_filter |
||
39 | { |
||
40 | const FILTERNAME = 'convert.league.csv'; |
||
41 | |||
42 | /** |
||
43 | * the filter name used to instantiate the class with. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | public $filtername; |
||
48 | |||
49 | /** |
||
50 | * @var mixed value passed to passed to stream_filter_append or stream_filter_prepend functions. |
||
51 | */ |
||
52 | public $params; |
||
53 | |||
54 | /** |
||
55 | * The records input encoding charset. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $input_encoding = 'UTF-8'; |
||
60 | |||
61 | /** |
||
62 | * The records output encoding charset. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $output_encoding = 'UTF-8'; |
||
67 | |||
68 | /** |
||
69 | * Static method to add the stream filter to a {@link AbstractCsv} object. |
||
70 | */ |
||
71 | 3 | public static function addTo(AbstractCsv $csv, string $input_encoding, string $output_encoding): AbstractCsv |
|
77 | |||
78 | /** |
||
79 | * Static method to register the class as a stream filter. |
||
80 | */ |
||
81 | 3 | public static function register(): void |
|
88 | |||
89 | /** |
||
90 | * Static method to return the stream filter filtername. |
||
91 | */ |
||
92 | 3 | public static function getFiltername(string $input_encoding, string $output_encoding): string |
|
101 | |||
102 | /** |
||
103 | * Filter encoding charset. |
||
104 | * |
||
105 | * @throws OutOfRangeException if the charset is malformed or unsupported |
||
106 | */ |
||
107 | 6 | protected static function filterEncoding(string $encoding): string |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 12 | public function onCreate(): bool |
|
147 | |||
148 | /** |
||
149 | * @param resource $in |
||
150 | * @param resource $out |
||
151 | * @param int $consumed |
||
152 | * @param bool $closing |
||
153 | */ |
||
154 | 3 | public function filter($in, $out, &$consumed, $closing): int |
|
164 | |||
165 | /** |
||
166 | * Convert Csv records collection into UTF-8. |
||
167 | */ |
||
168 | 21 | public function convert(iterable $records): iterable |
|
180 | |||
181 | /** |
||
182 | * Enable using the class as a formatter for the {@link Writer}. |
||
183 | */ |
||
184 | 3 | public function __invoke(array $record): array |
|
190 | |||
191 | /** |
||
192 | * Walker method to convert the offset and the value of a CSV record field. |
||
193 | * |
||
194 | * @param mixed $value can be a scalar type or null |
||
195 | * @param mixed $offset can be a string or an int |
||
196 | */ |
||
197 | 15 | protected function encodeField(&$value, &$offset): void |
|
207 | |||
208 | /** |
||
209 | * Sets the records input encoding charset. |
||
210 | */ |
||
211 | 12 | public function inputEncoding(string $encoding): self |
|
223 | |||
224 | /** |
||
225 | * Sets the records output encoding charset. |
||
226 | */ |
||
227 | 3 | public function outputEncoding(string $encoding): self |
|
239 | } |
||
240 |