-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #10 add reservation time service
- Loading branch information
Showing
2 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Reservation; | ||
use Carbon\Carbon; | ||
|
||
/** | ||
* 予約時刻作成Service | ||
* | ||
* Class ReserveTimeOptionService | ||
* @package App\Services | ||
*/ | ||
class ReserveTimeOptionService | ||
{ | ||
public function makeTimeOptions(Carbon $time) | ||
{ | ||
$timeOptions = []; | ||
$targetTime = $time; | ||
|
||
// 24時間後までの予約時間の選択肢を作成する | ||
for ($index = 0; $index < 24; $index++) { | ||
if ($index !== 0) { | ||
$targetTime = $time->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; | ||
} | ||
} |