Skip to content

Commit

Permalink
Add validation to cron expression components
Browse files Browse the repository at this point in the history
  • Loading branch information
shaedrich authored Dec 20, 2024
1 parent cf340a4 commit 4e113d4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
23 changes: 23 additions & 0 deletions src/Illuminate/Console/Scheduling/ManagesFrequencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -533,6 +544,8 @@ public function monthlyOn($dayOfMonth = 1, $time = '0:0')
{
$this->dailyAt($time);

$this->validateDayOfMonth($dayOfMonth);

return $this->spliceIntoPosition(3, $dayOfMonth);
}

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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));
}

Expand Down
50 changes: 50 additions & 0 deletions src/Illuminate/Console/Scheduling/ValidatesFrequencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Illuminate\Console\Scheduling;

use InvalidArgumentException;

use const Illuminate\Support\Date\{DAYS_PER_WEEK, HOURS_PER_DAY, MONTHS_PER_YEAR};

trait ValidatesFrequencies
{
/**
* @param int $hours
*/
protected function validateHour(int $hour)
{
if ($hour < 0 || $hour > 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");
}
}
}
23 changes: 23 additions & 0 deletions src/Illuminate/Support/Date/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Support\Date;

if (! defined(__NAMESPACE__.'\\'.($constant = 'SECONDS_PER_MINUTE'))) {
define(__NAMESPACE__.'\\'.$constant, 60);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'MINUTES_PER_HOUR'))) {
define(__NAMESPACE__.'\\'.$constant, 60);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'HOURS_PER_DAY'))) {
define(__NAMESPACE__.'\\'.$constant, 24);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'DAYS_PER_WEEK'))) {
define(__NAMESPACE__.'\\'.$constant, 7);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'MONTHS_PER_YEAR'))) {
define(__NAMESPACE__.'\\'.$constant, 12);
}

0 comments on commit 4e113d4

Please sign in to comment.