30 lines
850 B
PHP
30 lines
850 B
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Admin;
|
||
|
|
|
||
|
|
/** CSV helpers. Guards against spreadsheet formula injection (=, +, -, @, tab, CR prefixes). */
|
||
|
|
final class Csv {
|
||
|
|
|
||
|
|
public static function cell( mixed $v ): string {
|
||
|
|
$s = is_scalar( $v ) || null === $v ? (string) $v : '';
|
||
|
|
if ( '' !== $s && str_contains( "=+-@\t\r", $s[0] ) ) {
|
||
|
|
return "'" . $s;
|
||
|
|
}
|
||
|
|
return $s;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @return string[] */
|
||
|
|
public static function row( array $cells ): array {
|
||
|
|
return array_map( array( self::class, 'cell' ), array_values( $cells ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Render one RFC-4180 line (no WordPress needed; used for tests and streaming). */
|
||
|
|
public static function line( array $cells ): string {
|
||
|
|
$out = array();
|
||
|
|
foreach ( self::row( $cells ) as $c ) {
|
||
|
|
$out[] = '"' . str_replace( '"', '""', $c ) . '"';
|
||
|
|
}
|
||
|
|
return implode( ',', $out ) . "\r\n";
|
||
|
|
}
|
||
|
|
}
|