diff --git a/app/Io/CheckinCsvImporter.php b/app/Io/CheckinCsvImporter.php index b02d309..1fd2f0d 100644 --- a/app/Io/CheckinCsvImporter.php +++ b/app/Io/CheckinCsvImporter.php @@ -4,6 +4,7 @@ namespace App\Io; use App\Ejaculation; use App\Exceptions\CsvImportException; +use App\Tag; use Carbon\Carbon; use Illuminate\Support\Facades\DB; use League\Csv\Reader; @@ -51,20 +52,64 @@ class CheckinCsvImporter $errors[] = "{$offset} 行 : 日時列は必須です。"; continue; } - if (preg_match('/\A20\d{2}[-/](1[0-2]|0?\d)[-/](0?\d|[1-2]\d|3[01]) (0?\d|1\d|2[0-4]):(0?\d|[1-5]\d)(?P:(0?\d|[1-5]\d))?\z/', $checkinAt, $checkinAtMatches) !== 1) { + if (preg_match('/\A20\d{2}[-/](1[0-2]|0?\d)[-/](0?\d|[1-2]\d|3[01]) (0?\d|1\d|2[0-4]):(0?\d|[1-5]\d)(:(0?\d|[1-5]\d))?\z/', $checkinAt) !== 1) { $errors[] = "{$offset} 行 : 日時列の書式が正しくありません。"; continue; } - if (empty($checkinAtMatches['second'])) { - $checkinAt .= ':00'; - } $checkinAt = str_replace('/', '-', $checkinAt); try { - $ejaculation->ejaculated_date = Carbon::createFromFormat('Y-m-d H:i:s', $checkinAt); + $ejaculation->ejaculated_date = Carbon::createFromFormat('!Y-m-d H:i+', $checkinAt); } catch (\InvalidArgumentException $e) { $errors[] = "{$offset} 行 : 日時列に不正な値が入力されています。"; + continue; } + if (!empty($record['ノート'])) { + $note = $record['ノート']; + + if (mb_strlen($note) > 500) { + $errors[] = "{$offset} 行 : ノート列は500文字以内にしてください。"; + continue; + } + + $ejaculation->note = $note; + } + + if (!empty($record['オカズリンク'])) { + $link = $record['オカズリンク']; + + if (mb_strlen($link) > 2000) { + $errors[] = "{$offset} 行 : オカズリンク列は500文字以内にしてください。"; + continue; + } + + // TODO: URL Validation + + $ejaculation->link = $link; + } + + $tagIds = []; + for ($i = 1; $i <= 32; $i++) { + $column = 'タグ' . $i; + if (empty($record[$column])) { + break; + } else { + $tag = trim($record[$column]); + + if (empty($tag)) { + break; + } + if (mb_strlen($tag) > 255) { + $errors[] = "{$offset} 行 : {$column}列は255文字以内にしてください。"; + continue 2; + } + + $tag = Tag::firstOrCreate(['name' => $tag]); + $tagIds[] = $tag->id; + } + } + $ejaculation->tags()->sync($tagIds); + $ejaculation->save(); }