diff --git a/composer.json b/composer.json index 5c02f0e2aaf..2ed8498c30e 100644 --- a/composer.json +++ b/composer.json @@ -139,6 +139,7 @@ "src/Illuminate/Filesystem/functions.php", "src/Illuminate/Foundation/helpers.php", "src/Illuminate/Log/functions.php", + "src/Illuminate/Support/Date/constants.php", "src/Illuminate/Support/functions.php", "src/Illuminate/Support/helpers.php" ], diff --git a/src/Illuminate/Console/Scheduling/ManagesFrequencies.php b/src/Illuminate/Console/Scheduling/ManagesFrequencies.php index d974a91b769..ac6279a1512 100644 --- a/src/Illuminate/Console/Scheduling/ManagesFrequencies.php +++ b/src/Illuminate/Console/Scheduling/ManagesFrequencies.php @@ -2,11 +2,14 @@ namespace Illuminate\Console\Scheduling; +use Illuminate\Console\Scheduling\ValidatesFrequencies; use Illuminate\Support\Carbon; use InvalidArgumentException; trait ManagesFrequencies { + use ValidatesFrequencies; + /** * The Cron expression representing the event's frequency. * @@ -386,6 +389,14 @@ public function twiceDailyAt($first = 1, $second = 13, $offset = 0) */ protected function hourBasedSchedule($minutes, $hours) { + if (is_array($hours) || is_int($hours)) { + array_walk((array)$hours, $this->validateHour(...)); + } + + if (is_array($minutes) || is_int($minutes)) { + array_walk((array)$minutes, $this->validateMinute(...)); + } + $minutes = is_array($minutes) ? implode(',', $minutes) : $minutes; $hours = is_array($hours) ? implode(',', $hours) : $hours; @@ -533,6 +544,8 @@ public function monthlyOn($dayOfMonth = 1, $time = '0:0') { $this->dailyAt($time); + $this->validateDayOfMonth($dayOfMonth); + return $this->spliceIntoPosition(3, $dayOfMonth); } @@ -546,6 +559,8 @@ public function monthlyOn($dayOfMonth = 1, $time = '0:0') */ public function twiceMonthly($first = 1, $second = 16, $time = '0:0') { + array_walk([$first, $second], $this->validateDayOfMonth(...)); + $daysOfMonth = $first.','.$second; $this->dailyAt($time); @@ -619,6 +634,12 @@ public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0') { $this->dailyAt($time); + $this->validateMonth($month); + + if (is_int($dayOfMonth)) { + $this->validateDayOfMonth($dayOfMonth); + } + return $this->spliceIntoPosition(3, $dayOfMonth) ->spliceIntoPosition(4, $month); } @@ -633,6 +654,8 @@ public function days($days) { $days = is_array($days) ? $days : func_get_args(); + array_walk($days, $this->validateDayOfWeek(...)); + return $this->spliceIntoPosition(5, implode(',', $days)); } diff --git a/src/Illuminate/Console/Scheduling/ValidatesFrequencies.php b/src/Illuminate/Console/Scheduling/ValidatesFrequencies.php new file mode 100644 index 00000000000..e036fa17a17 --- /dev/null +++ b/src/Illuminate/Console/Scheduling/ValidatesFrequencies.php @@ -0,0 +1,50 @@ + HOURS_PER_DAY) { + throw new InvalidArgumentException("Hour cron expression component must be between 0 and " . HOURS_PER_DAY . ". [$hour] given"); + } + } + + /** + * @param int $dayOfWeek + */ + protected function validateDayOfWeek(int $dayOfWeek) + { + if ($dayOfWeek < 0 || $dayOfWeek > DAYS_PER_WEEK) { + throw new InvalidArgumentException("Day of week cron expression component must be between 0 and " . DAYS_PER_WEEK . ". [$dayOfWeek] given"); + } + } + + /** + * @param int $month + */ + protected function validateMonth(int $month) + { + if ($month < 0 || $month > MONTHS_PER_YEAR) { + throw new InvalidArgumentException("Month cron expression component must be between 0 and " . MONTHS_PER_YEAR . ". [$month] given"); + } + } + + /** + * @param int $month + */ + protected function validateDayOfMonth(int $dayOfMonth) + { + if ($month < 0 || $month > MONTHS_PER_YEAR) { + throw new InvalidArgumentException("Month cron expression component must be between 0 and " . MONTHS_PER_YEAR . ". [$month] given"); + } + } +} diff --git a/src/Illuminate/Support/Date/constants.php b/src/Illuminate/Support/Date/constants.php new file mode 100644 index 00000000000..9e7b165b491 --- /dev/null +++ b/src/Illuminate/Support/Date/constants.php @@ -0,0 +1,23 @@ +