|
7 | 7 |
|
8 | 8 | namespace Safe;
|
9 | 9 |
|
| 10 | +use Safe\Exceptions\FilesystemException; |
10 | 11 | use const PREG_NO_ERROR;
|
11 | 12 |
|
12 | 13 | use Safe\Exceptions\MiscException;
|
@@ -363,3 +364,44 @@ function posix_getpgid(int $process_id): int
|
363 | 364 | }
|
364 | 365 | return $result;
|
365 | 366 | }
|
| 367 | + |
| 368 | + |
| 369 | +/** |
| 370 | + * fputcsv formats a line (passed as a |
| 371 | + * fields array) as CSV and writes it (terminated by a |
| 372 | + * newline) to the specified file stream. |
| 373 | + * |
| 374 | + * @param resource $stream The file pointer must be valid, and must point to |
| 375 | + * a file successfully opened by fopen or |
| 376 | + * fsockopen (and not yet closed by |
| 377 | + * fclose). |
| 378 | + * @phpstan-param (scalar|\Stringable|null)[] $fields |
| 379 | + * @param array $fields An array of strings. |
| 380 | + * @param string $separator The optional separator parameter sets the field |
| 381 | + * delimiter (one single-byte character only). |
| 382 | + * @param string $enclosure The optional enclosure parameter sets the field |
| 383 | + * enclosure (one single-byte character only). |
| 384 | + * @param string $escape The optional escape parameter sets the |
| 385 | + * escape character (at most one single-byte character). |
| 386 | + * An empty string ("") disables the proprietary escape mechanism. |
| 387 | + * @param string $eol The optional eol parameter sets |
| 388 | + * a custom End of Line sequence. |
| 389 | + * @return int Returns the length of the written string. |
| 390 | + * @throws FilesystemException |
| 391 | + * |
| 392 | + */ |
| 393 | +function fputcsv($stream, array $fields, string $separator = ",", string $enclosure = "\"", string $escape = "\\", string $eol = "\n"): int |
| 394 | +{ |
| 395 | + error_clear_last(); |
| 396 | + if (PHP_VERSION_ID >= 80100) { |
| 397 | + /** @phpstan-ignore-next-line */ |
| 398 | + $result = \fputcsv($stream, $fields, $separator, $enclosure, $escape, $eol); |
| 399 | + } else { |
| 400 | + $result = \fputcsv($stream, $fields, $separator, $enclosure, $escape); |
| 401 | + } |
| 402 | + |
| 403 | + if ($result === false) { |
| 404 | + throw FilesystemException::createFromPhpError(); |
| 405 | + } |
| 406 | + return $result; |
| 407 | +} |
0 commit comments