29 lines
761 B
PHP
29 lines
761 B
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
/**
|
|
* 純粋なCSVユーティリティ
|
|
* - ビジネスロジックは含めない
|
|
*/
|
|
class Csv
|
|
{
|
|
/**
|
|
* 文字列配列をCSV行に変換
|
|
* 備考: 囲い/エスケープは fputcsv に委譲するのが安全だが、ここでは簡易版を提供
|
|
*/
|
|
public static function join(array $fields, string $delimiter = ','): string
|
|
{
|
|
$escaped = array_map(static function ($v) use ($delimiter) {
|
|
$s = (string) $v;
|
|
if (str_contains($s, '"') || str_contains($s, $delimiter) || str_contains($s, "\n")) {
|
|
$s = '"'.str_replace('"', '""', $s).'"';
|
|
}
|
|
return $s;
|
|
}, $fields);
|
|
return implode($delimiter, $escaped);
|
|
}
|
|
}
|
|
|
|
|