日付形式と範囲のチェック

This commit is contained in:
shibafu
2020-02-16 14:28:30 +09:00
parent b29a82435c
commit ea59bcf150
4 changed files with 70 additions and 4 deletions

View File

@@ -51,6 +51,7 @@ class CheckinCsvImporter
}
foreach ($csv->getRecords() as $offset => $record) {
$line = $offset + 1;
$ejaculation = new Ejaculation(['user_id' => $this->user->id]);
$validator = Validator::make($record, [
@@ -61,7 +62,7 @@ class CheckinCsvImporter
if ($validator->fails()) {
foreach ($validator->errors()->all() as $message) {
$errors[] = "{$offset} 行 : {$message}";
$errors[] = "{$line} 行 : {$message}";
}
continue;
}
@@ -82,7 +83,7 @@ class CheckinCsvImporter
break;
}
if (mb_strlen($tag) > 255) {
$errors[] = "{$offset} 行 : {$column}列は255文字以内にしてください。";
$errors[] = "{$line} 行 : {$column}列は255文字以内にしてください。";
continue 2;
}

View File

@@ -17,6 +17,12 @@ class CsvDateTime implements Rule
'Y/n/j G:i',
];
const MINIMUM_TIMESTAMP = 946652400; // 2000-01-01 00:00:00 JST
const MAXIMUM_TIMESTAMP = 4102412399; // 2099-12-31 23:59:59 JST
/** @var string Validation error message */
private $message = ':attribute の形式は "年/月/日 時:分" にしてください。';
/**
* Create a new rule instance.
*
@@ -44,8 +50,19 @@ class CsvDateTime implements Rule
foreach (self::VALID_FORMATS as $format) {
$date = \DateTime::createFromFormat('!' . $format, $value);
if (!$date) {
continue;
}
if ($date && $date->format($format) === $value) {
$timestamp = (int) $date->format('U');
if ($timestamp < self::MINIMUM_TIMESTAMP || self::MAXIMUM_TIMESTAMP < $timestamp) {
$this->message = ':attribute は 2000/01/01 00:00 〜 2099/12/31 23:59 の間のみ対応しています。';
return false;
}
$formatted = $date->format($format);
if ($formatted === $value) {
return true;
}
}
@@ -60,6 +77,6 @@ class CsvDateTime implements Rule
*/
public function message()
{
return ':attribute の形式は "年/月/日 時:分" にしてください。';
return $this->message;
}
}