diff --git a/app/Reservation.php b/app/Reservation.php index 246e381..ac852e5 100644 --- a/app/Reservation.php +++ b/app/Reservation.php @@ -2,6 +2,7 @@ namespace App; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; /** @@ -22,12 +23,18 @@ class Reservation extends Model 'user_id', 'reserve_time', ]; + // 7時〜11時台は20分単位の予約 + const SHORT_RESERVATION_TIME_START_HOUR = 7; + const SHORT_RESERVATION_TIME_END_HOUR = 11; + // 予約する時間枠の長さ + const DEFAULT_RESERVATION_TIME_SPAN = 30; + const SHORT_RESERVATION_TIME_SPAN = 20; + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function user() { - return $this->belongsTo('App\User', 'user_id', 'id'); + return $this->belongsTo('App\User', 'user_id', 'id'); } - } diff --git a/app/Services/ReserveTimeOptionService.php b/app/Services/ReserveTimeOptionService.php new file mode 100644 index 0000000..e69e31b --- /dev/null +++ b/app/Services/ReserveTimeOptionService.php @@ -0,0 +1,53 @@ +copy()->addHour($index)->setMinute(0); + } + $timeOptions = array_merge($timeOptions, $this->makeTimeOptionsForTargetHour($targetTime)); + } + + return $timeOptions; + } + + public function makeTimeOptionsForTargetHour(Carbon $targetTime) + { + $timeOptions = []; + + if ($targetTime->hour >= Reservation::SHORT_RESERVATION_TIME_START_HOUR || + $targetTime->hour <= Reservation::SHORT_RESERVATION_TIME_END_HOUR) { + // 作成しようとした予約開始時刻が朝の時間帯の範囲の場合は、20分単位で選択肢を作成 + $startMinutes = [0, 20, 40]; + } else { + $startMinutes = [0, 30]; + } + + foreach ($startMinutes as $startMinute) { + if ($targetTime->minute <= $startMinute) { + $timeOptions[] = $targetTime->format('Y-m-d') . ' ' + . sprintf('%2d', $targetTime->hour) . ':' + . sprintf('%2d', $startMinute) . ':00'; + } + } + return $timeOptions; + } +}